Spaces:
Running
Running
File size: 1,255 Bytes
f905fc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# model_handler.py
from gradio_client import Client, handle_file
import threading
import requests
client = Client("ai4bharat/IndicF5")
def call_model_with_timeout(text, ref_audio_path, ref_text="", timeout=120):
result = {"status": None, "audio_url": None}
def run():
try:
audio_url = client.predict(
text,
handle_file(ref_audio_path),
ref_text,
api_name="/synthesize_speech"
)
result["status"] = "success"
result["audio_url"] = audio_url
except Exception as e:
result["status"] = f"error: {e}"
thread = threading.Thread(target=run)
thread.start()
thread.join(timeout)
if thread.is_alive():
return "⛔ Timed out!", None
if result["status"] == "success":
audio_url = result["audio_url"]
try:
output_path = "output.wav"
audio_response = requests.get(audio_url)
with open(output_path, "wb") as f:
f.write(audio_response.content)
return "✅ Success", output_path
except Exception as e:
return f"⛔ Download error: {e}", None
else:
return result["status"], None
|