text_to_speech_hf / model_handler.py
amit0987's picture
Create app.py
f905fc2
# 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