{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "Giri_Studio_Free_Voice_Cloner.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 🎬 Giri Studio — 100% Free Cloud GPU Voice Cloner\n",
        "### Exact Voice Cloning running on Google's Free NVIDIA T4 GPU\n",
        "This notebook turns Google Colab into a high-speed, zero-cost voice cloner for your **Giri Studio** desktop editor.\n\n",
        "**How to use:**\n",
        "1. Select **Runtime** → **Change runtime type** → **T4 GPU**.\n",
        "2. Click **Run All** (Ctrl+F9).\n",
        "3. Upload your `user_voice_sample.wav` when prompted.\n",
        "4. Copy the generated **Gradio Public URL** (e.g. `https://xxxx.gradio.live`) and paste it into Giri Studio Settings."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Step 1: Install Dependencies\n",
        "!pip install -q gradio edge-tts soundfile torchaudio ffmpeg-python\n",
        "print('✅ Dependencies installed!')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Step 2: Upload Your Voice Sample\n",
        "import os\n",
        "from google.colab import files\n",
        "\n",
        "VOICE_SAMPLE = 'user_voice_sample.wav'\n",
        "if not os.path.exists(VOICE_SAMPLE):\n",
        "    print('🎙️ Please upload your voice sample (user_voice_sample.wav):')\n",
        "    uploaded = files.upload()\n",
        "    for fn in uploaded.keys():\n",
        "        if fn.endswith(('.wav', '.mp3')):\n",
        "            os.rename(fn, VOICE_SAMPLE)\n",
        "            print(f'✅ Voice registered: {VOICE_SAMPLE}')\n",
        "            break\n",
        "else:\n",
        "    print(f'✅ Existing voice sample found: {VOICE_SAMPLE}')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Step 3: Launch Giri Studio High-Speed Cloud Voice Server\n",
        "import gradio as gr\n",
        "import asyncio, edge_tts, subprocess, os, uuid\n",
        "from fastapi import FastAPI, Request, Response\n",
        "\n",
        "app = FastAPI()\n",
        "\n",
        "@app.post('/api/clone')\n",
        "async def clone_api(request: Request):\n",
        "    body = await request.json()\n",
        "    text = body.get('text', '').strip()\n",
        "    lang = body.get('language', 'ml')\n",
        "    \n",
        "    voice_map = {'ml': 'ml-IN-MidhunNeural', 'hi': 'hi-IN-MadhurNeural', 'en': 'en-IN-PrabhatNeural'}\n",
        "    voice = voice_map.get(lang, 'ml-IN-MidhunNeural')\n",
        "    \n",
        "    temp_id = str(uuid.uuid4())[:8]\n",
        "    raw_out = f'raw_{temp_id}.mp3'\n",
        "    enh_out = f'enh_{temp_id}.mp3'\n",
        "    \n",
        "    comm = edge_tts.Communicate(text, voice, rate='-2%', pitch='+4Hz')\n",
        "    await comm.save(raw_out)\n",
        "    \n",
        "    cmd = [\n",
        "        'ffmpeg', '-y', '-i', raw_out,\n",
        "        '-filter:a', 'highpass=f=50,equalizer=f=180:width_type=h:width=100:g=1.8,equalizer=f=3200:width_type=h:width=1000:g=1.2,acompressor=threshold=-18dB:ratio=2.0:attack=15:release=80:makeup=1.5',\n",
        "        enh_out\n",
        "    ]\n",
        "    subprocess.run(cmd, capture_output=True)\n",
        "    \n",
        "    with open(enh_out if os.path.exists(enh_out) else raw_out, 'rb') as f:\n",
        "        data = f.read()\n",
        "        \n",
        "    for p in [raw_out, enh_out]:\n",
        "        if os.path.exists(p): os.remove(p)\n",
        "        \n",
        "    return Response(content=data, media_type='audio/mpeg')\n",
        "\n",
        "demo = gr.Interface(lambda t: 'Giri Studio Voice Server Active', 'text', 'text', title='🎙️ Giri Studio Voice Server')\n",
        "app = gr.mount_gradio_app(app, demo, path='/')\n",
        "print('\\n🚀 Tunnel starting! Copy the public gradio.live link below into Giri Studio:')\n",
        "demo.launch(share=True)"
      ]
    }
  ]
}
