File size: 1,276 Bytes
ffb6ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)