don0726 commited on
Commit
0df6946
·
verified ·
1 Parent(s): b0d2fd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -1,18 +1,18 @@
1
  import os
2
  import uuid
3
- import gradio as gr
4
- import requests
5
  from pathlib import Path
 
 
6
  from TTS.api import TTS
7
 
8
  # ---------------------------
9
- # Create models folder
10
  # ---------------------------
11
  MODEL_DIR = Path("models/xtts_v2")
12
  MODEL_DIR.mkdir(parents=True, exist_ok=True)
13
 
14
  # ---------------------------
15
- # Model files URLs
16
  # ---------------------------
17
  FILES = {
18
  "model.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/main/model.pth",
@@ -22,7 +22,7 @@ FILES = {
22
  }
23
 
24
  # ---------------------------
25
- # Download files if not exist
26
  # ---------------------------
27
  def download_file(url, path):
28
  if path.exists():
@@ -39,55 +39,50 @@ def download_file(url, path):
39
  print(f"\r{path.name}: {downloaded/1024/1024:.2f}/{total/1024/1024:.2f} MB", end="")
40
  print(f"\n✅ Downloaded: {path.name}")
41
 
 
42
  for fname, url in FILES.items():
43
  download_file(url, MODEL_DIR / fname)
44
 
45
  # ---------------------------
46
- # Load XTTS Model (CPU)
47
  # ---------------------------
48
-
49
-
50
-
51
- # Load XTTS Model (CPU)
52
  tts = TTS(
53
- model_path=str(MODEL_DIR / "model.pth"),
54
  config_path=str(MODEL_DIR / "config.json"),
55
- gpu=False # CPU in HF Space
56
  )
57
-
58
-
59
-
60
-
61
-
62
- print("✅ Model loaded and ready!")
63
 
64
  # ---------------------------
65
- # Create outputs folder
66
  # ---------------------------
67
  OUTPUT_DIR = Path("outputs")
68
  OUTPUT_DIR.mkdir(exist_ok=True)
69
 
70
  # ---------------------------
71
- # Generate TTS
72
  # ---------------------------
73
  def generate_tts(text, voice_file):
74
  if not text or voice_file is None:
75
  return None, "❌ Please provide both text and voice sample."
76
 
 
77
  out_name = f"{uuid.uuid4().hex}.wav"
78
  out_path = OUTPUT_DIR / out_name
79
 
 
80
  tts.tts_to_file(
81
  text=text,
82
  speaker_wav=voice_file,
83
- language="hi", # Change if needed
84
  file_path=str(out_path)
85
  )
86
 
87
  return str(out_path), "✅ Audio generated!"
88
 
89
  # ---------------------------
90
- # Gradio interface
91
  # ---------------------------
92
  demo = gr.Interface(
93
  fn=generate_tts,
@@ -103,5 +98,6 @@ demo = gr.Interface(
103
  description="Upload a voice sample + text → get cloned voice audio. Model loads once and is reused."
104
  )
105
 
 
106
  if __name__ == "__main__":
107
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import os
2
  import uuid
 
 
3
  from pathlib import Path
4
+ import requests
5
+ import gradio as gr
6
  from TTS.api import TTS
7
 
8
  # ---------------------------
9
+ # Model directory
10
  # ---------------------------
11
  MODEL_DIR = Path("models/xtts_v2")
12
  MODEL_DIR.mkdir(parents=True, exist_ok=True)
13
 
14
  # ---------------------------
15
+ # Files to download
16
  # ---------------------------
17
  FILES = {
18
  "model.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/main/model.pth",
 
22
  }
23
 
24
  # ---------------------------
25
+ # Download files function
26
  # ---------------------------
27
  def download_file(url, path):
28
  if path.exists():
 
39
  print(f"\r{path.name}: {downloaded/1024/1024:.2f}/{total/1024/1024:.2f} MB", end="")
40
  print(f"\n✅ Downloaded: {path.name}")
41
 
42
+ # Download all model files
43
  for fname, url in FILES.items():
44
  download_file(url, MODEL_DIR / fname)
45
 
46
  # ---------------------------
47
+ # Load TTS model (once)
48
  # ---------------------------
49
+ print("⬇️ Loading XTTS-v2 model...")
 
 
 
50
  tts = TTS(
51
+ model_path=str(MODEL_DIR), # pass folder, NOT file
52
  config_path=str(MODEL_DIR / "config.json"),
53
+ gpu=False
54
  )
55
+ print("✅ Model loaded successfully!")
 
 
 
 
 
56
 
57
  # ---------------------------
58
+ # Outputs directory
59
  # ---------------------------
60
  OUTPUT_DIR = Path("outputs")
61
  OUTPUT_DIR.mkdir(exist_ok=True)
62
 
63
  # ---------------------------
64
+ # TTS generation function
65
  # ---------------------------
66
  def generate_tts(text, voice_file):
67
  if not text or voice_file is None:
68
  return None, "❌ Please provide both text and voice sample."
69
 
70
+ # Generate unique output filename
71
  out_name = f"{uuid.uuid4().hex}.wav"
72
  out_path = OUTPUT_DIR / out_name
73
 
74
+ # Generate audio
75
  tts.tts_to_file(
76
  text=text,
77
  speaker_wav=voice_file,
78
+ language="hi", # You can change language if needed
79
  file_path=str(out_path)
80
  )
81
 
82
  return str(out_path), "✅ Audio generated!"
83
 
84
  # ---------------------------
85
+ # Gradio UI
86
  # ---------------------------
87
  demo = gr.Interface(
88
  fn=generate_tts,
 
98
  description="Upload a voice sample + text → get cloned voice audio. Model loads once and is reused."
99
  )
100
 
101
+ # Launch app
102
  if __name__ == "__main__":
103
  demo.launch(server_name="0.0.0.0", server_port=7860)