Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,87 @@
|
|
| 1 |
-
import
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
import spaces
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
| 18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
-
print(f"⚙️ Device
|
| 20 |
|
| 21 |
-
|
| 22 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
@spaces.GPU(duration=120)
|
| 26 |
def clone_voice(text, language, speaker_audio):
|
| 27 |
if not text or not speaker_audio:
|
| 28 |
-
return None
|
| 29 |
|
|
|
|
| 30 |
output_path = "output.wav"
|
| 31 |
|
| 32 |
-
# Generate Audio
|
|
|
|
| 33 |
tts.tts_to_file(
|
| 34 |
text=text,
|
| 35 |
file_path=output_path,
|
| 36 |
speaker_wav=speaker_audio,
|
| 37 |
language=language
|
| 38 |
)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
fn=clone_voice,
|
| 44 |
inputs=[
|
| 45 |
-
gr.Textbox(label="Text
|
| 46 |
gr.Dropdown(label="Language", choices=["hi", "en"], value="hi"),
|
| 47 |
-
gr.Audio(label="
|
| 48 |
],
|
| 49 |
-
outputs=
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
import spaces
|
| 5 |
+
from TTS.api import TTS
|
| 6 |
+
from supabase import create_client, Client
|
| 7 |
|
| 8 |
+
# 👇 यहाँ हमने आपकी नई अलग फाइल को इंपोर्ट किया
|
| 9 |
+
import config
|
| 10 |
|
| 11 |
+
# 1. Setup Supabase (Using config file)
|
| 12 |
+
supabase = None
|
| 13 |
+
if config.IS_CONNECTED:
|
| 14 |
+
try:
|
| 15 |
+
supabase: Client = create_client(config.SUPABASE_URL, config.SUPABASE_KEY)
|
| 16 |
+
print("✅ Supabase Client Connected!")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"❌ Connection Error: {e}")
|
| 19 |
|
| 20 |
+
# 2. Terms & Model Setup
|
| 21 |
+
import os
|
| 22 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
print(f"⚙️ Device: {device}")
|
| 25 |
|
| 26 |
+
print("⏳ Loading XTTS Model...")
|
| 27 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
| 28 |
|
| 29 |
+
# 3. Main Logic
|
| 30 |
+
@spaces.GPU(duration=120)
|
| 31 |
def clone_voice(text, language, speaker_audio):
|
| 32 |
if not text or not speaker_audio:
|
| 33 |
+
return None, "Error: Text or Audio missing"
|
| 34 |
|
| 35 |
+
filename = f"generated_{uuid.uuid4()}.wav"
|
| 36 |
output_path = "output.wav"
|
| 37 |
|
| 38 |
+
# A. Generate Audio
|
| 39 |
+
print("🎙️ Generating...")
|
| 40 |
tts.tts_to_file(
|
| 41 |
text=text,
|
| 42 |
file_path=output_path,
|
| 43 |
speaker_wav=speaker_audio,
|
| 44 |
language=language
|
| 45 |
)
|
| 46 |
+
|
| 47 |
+
# B. Upload using Config vars
|
| 48 |
+
storage_url = "Not Saved"
|
| 49 |
+
if supabase:
|
| 50 |
+
try:
|
| 51 |
+
with open(output_path, 'rb') as f:
|
| 52 |
+
supabase.storage.from_("voice-bucket").upload(filename, f)
|
| 53 |
+
|
| 54 |
+
# URL बनाना
|
| 55 |
+
storage_url = f"{config.SUPABASE_URL}/storage/v1/object/public/voice-bucket/{filename}"
|
| 56 |
+
print(f"✅ Uploaded: {storage_url}")
|
| 57 |
|
| 58 |
+
# Database Entry
|
| 59 |
+
data = {
|
| 60 |
+
"name": f"Clone_{language}",
|
| 61 |
+
"file_path": storage_url,
|
| 62 |
+
"ref_text": text
|
| 63 |
+
}
|
| 64 |
+
supabase.table("clones").insert(data).execute()
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"❌ Error: {e}")
|
| 68 |
+
storage_url = f"Error: {e}"
|
| 69 |
+
|
| 70 |
+
return output_path, storage_url
|
| 71 |
+
|
| 72 |
+
# 4. Interface
|
| 73 |
+
with gr.Interface(
|
| 74 |
fn=clone_voice,
|
| 75 |
inputs=[
|
| 76 |
+
gr.Textbox(label="Text", value="Config file test successful!", lines=2),
|
| 77 |
gr.Dropdown(label="Language", choices=["hi", "en"], value="hi"),
|
| 78 |
+
gr.Audio(label="Audio", type="filepath")
|
| 79 |
],
|
| 80 |
+
outputs=[
|
| 81 |
+
gr.Audio(label="Result"),
|
| 82 |
+
gr.Textbox(label="Cloud Link")
|
| 83 |
+
],
|
| 84 |
+
title="🚀 Shubham's Clean Code XTTS",
|
| 85 |
+
description="Running with separate config file."
|
| 86 |
+
) as iface:
|
| 87 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|