amit0987 commited on
Commit
f905fc2
·
1 Parent(s): 968bd1e

Create app.py

Browse files
Files changed (3) hide show
  1. app.py +38 -0
  2. model_handler.py +43 -0
  3. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import gradio as gr
4
+ import requests
5
+
6
+ from model_handler import call_model_with_timeout
7
+
8
+
9
+ def synthesize_speech(text, ref_audio, ref_text):
10
+ if not ref_audio:
11
+ return "⛔ Please upload a reference WAV file", None
12
+
13
+ # ref_audio is already a file path
14
+ status, output_path = call_model_with_timeout(text, ref_audio, ref_text)
15
+ return status, output_path
16
+
17
+
18
+ ref_audio_url = "https://github.com/AI4Bharat/IndicF5/raw/refs/heads/main/prompts/PAN_F_HAPPY_00002.wav"
19
+ ref_audio_path = "PAN_F_HAPPY_00002.wav"
20
+ with open(ref_audio_path, "wb") as f:
21
+ f.write(requests.get(ref_audio_url).content)
22
+ iface = gr.Interface(
23
+ fn=synthesize_speech,
24
+ inputs=[
25
+ gr.Textbox(label="Input Text", value="यह एक परीक्षण वाक्य है।"),
26
+ gr.Audio(type="filepath", label="Reference Audio", value="PAN_F_HAPPY_00002.wav"),
27
+ gr.Textbox(label="Reference Text",
28
+ value="ਇੱਕ ਗ੍ਰਾਹਕ ਨੇ ਸਾਡੀ ਬੇਮਿਸਾਲ ਸੇਵਾ ਬਾਰੇ ਦਿਲੋਂਗਵਾਹੀ ਦਿੱਤੀ ਜਿਸ ਨਾਲ ਸਾਨੂੰ ਅਨੰਦ ਮਹਿਸੂਸ ਹੋਇਆ।"),
29
+ ],
30
+ outputs=[
31
+ gr.Textbox(label="Status"),
32
+ gr.Audio(label="Synthesized Output")
33
+ ],
34
+ title="Speech Synthesis Demo",
35
+ description="Provide input text, a reference audio sample, and its corresponding text to synthesize new speech."
36
+ )
37
+
38
+ iface.launch()
model_handler.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # model_handler.py
2
+
3
+ from gradio_client import Client, handle_file
4
+ import threading
5
+ import requests
6
+
7
+ client = Client("ai4bharat/IndicF5")
8
+
9
+ def call_model_with_timeout(text, ref_audio_path, ref_text="", timeout=120):
10
+ result = {"status": None, "audio_url": None}
11
+
12
+ def run():
13
+ try:
14
+ audio_url = client.predict(
15
+ text,
16
+ handle_file(ref_audio_path),
17
+ ref_text,
18
+ api_name="/synthesize_speech"
19
+ )
20
+ result["status"] = "success"
21
+ result["audio_url"] = audio_url
22
+ except Exception as e:
23
+ result["status"] = f"error: {e}"
24
+
25
+ thread = threading.Thread(target=run)
26
+ thread.start()
27
+ thread.join(timeout)
28
+
29
+ if thread.is_alive():
30
+ return "⛔ Timed out!", None
31
+
32
+ if result["status"] == "success":
33
+ audio_url = result["audio_url"]
34
+ try:
35
+ output_path = "output.wav"
36
+ audio_response = requests.get(audio_url)
37
+ with open(output_path, "wb") as f:
38
+ f.write(audio_response.content)
39
+ return "✅ Success", output_path
40
+ except Exception as e:
41
+ return f"⛔ Download error: {e}", None
42
+ else:
43
+ return result["status"], None
requirements.txt ADDED
Binary file (110 Bytes). View file