don0726 commited on
Commit
e645a52
Β·
verified Β·
1 Parent(s): 35995b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -5,8 +5,9 @@ import gradio as gr
5
  from fastapi import FastAPI, UploadFile, File, Form
6
  from fastapi.responses import FileResponse
7
  from TTS.api import TTS
 
8
 
9
- # βœ… Accept license
10
  os.environ["COQUI_TOS_AGREED"] = "1"
11
 
12
  # =========================
@@ -40,11 +41,11 @@ async def clone_voice_api(
40
  input_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_input.wav"
41
  output_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_output.wav"
42
 
43
- # Save uploaded file
44
  with open(input_path, "wb") as f:
45
  f.write(await audio.read())
46
 
47
- # πŸ”₯ USE SAME LOADED MODEL
48
  tts.tts_to_file(
49
  text=text,
50
  speaker_wav=input_path,
@@ -65,10 +66,13 @@ def clone_voice_ui(audio_path, text, language):
65
  if audio_path is None:
66
  return "❌ Upload audio", None
67
 
 
 
 
68
  try:
69
  output_path = f"{OUTPUT_DIR}/{uuid.uuid4()}.wav"
70
 
71
- # πŸ”₯ SAME MODEL USED HERE
72
  tts.tts_to_file(
73
  text=text,
74
  speaker_wav=audio_path,
@@ -79,23 +83,35 @@ def clone_voice_ui(audio_path, text, language):
79
  return "βœ… Done", output_path
80
 
81
  except Exception as e:
82
- return str(e), None
83
 
84
 
85
  with gr.Blocks(title="XTTS Voice Cloning") as demo:
86
  gr.Markdown("# 🎀 XTTS Voice Cloning")
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- audio_input = gr.Audio(type="filepath", label="Speaker Audio")
89
- text_input = gr.Textbox(label="Text")
90
- lang_input = gr.Textbox(value="en", label="Language")
 
91
 
92
- btn = gr.Button("Generate")
93
 
94
- status = gr.Textbox()
95
- output_audio = gr.Audio()
96
 
97
- btn.click(
98
- clone_voice_ui,
99
  inputs=[audio_input, text_input, lang_input],
100
  outputs=[status, output_audio]
101
  )
@@ -104,4 +120,11 @@ with gr.Blocks(title="XTTS Voice Cloning") as demo:
104
  # =========================
105
  # COMBINE GRADIO + FASTAPI
106
  # =========================
107
- app = gr.mount_gradio_app(api, demo, path="/")
 
 
 
 
 
 
 
 
5
  from fastapi import FastAPI, UploadFile, File, Form
6
  from fastapi.responses import FileResponse
7
  from TTS.api import TTS
8
+ import uvicorn
9
 
10
+ # βœ… Accept Coqui license
11
  os.environ["COQUI_TOS_AGREED"] = "1"
12
 
13
  # =========================
 
41
  input_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_input.wav"
42
  output_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_output.wav"
43
 
44
+ # Save uploaded audio
45
  with open(input_path, "wb") as f:
46
  f.write(await audio.read())
47
 
48
+ # πŸ”₯ Use same loaded model
49
  tts.tts_to_file(
50
  text=text,
51
  speaker_wav=input_path,
 
66
  if audio_path is None:
67
  return "❌ Upload audio", None
68
 
69
+ if text.strip() == "":
70
+ return "❌ Enter text", None
71
+
72
  try:
73
  output_path = f"{OUTPUT_DIR}/{uuid.uuid4()}.wav"
74
 
75
+ # πŸ”₯ Use same loaded model
76
  tts.tts_to_file(
77
  text=text,
78
  speaker_wav=audio_path,
 
83
  return "βœ… Done", output_path
84
 
85
  except Exception as e:
86
+ return f"❌ Error: {str(e)}", None
87
 
88
 
89
  with gr.Blocks(title="XTTS Voice Cloning") as demo:
90
  gr.Markdown("# 🎀 XTTS Voice Cloning")
91
+ gr.Markdown("Upload voice sample and generate cloned speech")
92
+
93
+ audio_input = gr.Audio(
94
+ type="filepath",
95
+ label="Upload / Record Speaker Audio"
96
+ )
97
+
98
+ text_input = gr.Textbox(
99
+ label="Text",
100
+ placeholder="Type something..."
101
+ )
102
 
103
+ lang_input = gr.Textbox(
104
+ label="Language Code",
105
+ value="en"
106
+ )
107
 
108
+ generate_btn = gr.Button("πŸš€ Generate")
109
 
110
+ status = gr.Textbox(label="Status")
111
+ output_audio = gr.Audio(label="Generated Audio")
112
 
113
+ generate_btn.click(
114
+ fn=clone_voice_ui,
115
  inputs=[audio_input, text_input, lang_input],
116
  outputs=[status, output_audio]
117
  )
 
120
  # =========================
121
  # COMBINE GRADIO + FASTAPI
122
  # =========================
123
+ app = gr.mount_gradio_app(api, demo, path="/")
124
+
125
+
126
+ # =========================
127
+ # πŸš€ KEEP SERVER RUNNING
128
+ # =========================
129
+ if __name__ == "__main__":
130
+ uvicorn.run(app, host="0.0.0.0", port=7860)