sg123321 commited on
Commit
c0b8023
·
verified ·
1 Parent(s): 4b3c8a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -29
app.py CHANGED
@@ -1,55 +1,87 @@
1
- import os
2
  import gradio as gr
3
  import torch
4
- import spaces # ZeroGPU Magic
 
 
5
 
6
- # 1. Terms Accept
7
- os.environ["COQUI_TOS_AGREED"] = "1"
8
 
9
- # 2. Model Installation Check
10
- print("⏳ Importing TTS...")
11
- try:
12
- from TTS.api import TTS
13
- except ImportError:
14
- print(" TTS Install Failed. Check requirements.txt")
15
- raise
 
16
 
17
- # 3. Load Model (GPU Support)
 
 
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
- print(f"⚙️ Device Selected: {device}")
20
 
21
- # Model ko global variable mein rakhenge
22
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
23
 
24
- # 4. Voice Cloning Function (With ZeroGPU Decorator)
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
- return output_path
 
 
 
 
 
 
 
 
 
 
40
 
41
- # 5. UI Setup
42
- iface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  fn=clone_voice,
44
  inputs=[
45
- gr.Textbox(label="Text to Speak", value="Namaste, ye meri cloned aawaz hai.", lines=2),
46
  gr.Dropdown(label="Language", choices=["hi", "en"], value="hi"),
47
- gr.Audio(label="Upload Speaker Voice (WAV/MP3)", type="filepath")
48
  ],
49
- outputs=gr.Audio(label="Generated Audio"),
50
- title="🚀 Shubham's Super Fast XTTS (H200 GPU)",
51
- description="ZeroGPU powered Hindi Voice Cloning."
52
- )
53
-
54
- if __name__ == "__main__":
 
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)