voice-clone2 / app.py
Devity4756's picture
Create app.py
ffb6ef0 verified
from flask import Flask
from flask_socketio import SocketIO, emit
from gradio_client import Client
import base64
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
# Connect to Tony Assi's hosted space
client = Client("https://tonyassi-voice-clone.hf.space/")
@socketio.on("clone_request")
def handle_clone(data):
try:
text = data.get("text")
audio_file = data.get("audio") # expect base64 audio string from frontend
# Save incoming base64 audio to file
with open("input.wav", "wb") as f:
f.write(base64.b64decode(audio_file.split(",")[-1]))
# Call Hugging Face Space
result = client.predict(
text,
"input.wav", # path to saved audio
api_name="/clone"
)
# Read generated output.wav file
with open(result, "rb") as f:
audio_bytes = f.read()
# Send back as base64 so frontend can play without download
audio_base64 = "data:audio/wav;base64," + base64.b64encode(audio_bytes).decode()
emit("clone_response", {"audio": audio_base64})
except Exception as e:
emit("clone_response", {"error": str(e)})
if __name__ == "__main__":
socketio.run(app, host="0.0.0.0", port=5000)