Devity4756 commited on
Commit
3bc4713
·
verified ·
1 Parent(s): f126ecc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -36
app.py CHANGED
@@ -1,40 +1,41 @@
1
- from flask import Flask, request, send_file, jsonify
 
 
2
  from TTS.api import TTS
3
  import os
4
- import tempfile
5
-
6
- # Accept TOS
7
  os.environ["COQUI_TOS_AGREED"] = "1"
8
 
9
- # Initialize TTS model once
10
- tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to("cpu")
11
-
12
- app = Flask(__name__)
13
-
14
- @app.route("/clone", methods=["POST"])
15
- def clone_route():
16
- try:
17
- # Get text and audio file from request
18
- text = request.form.get("text")
19
- audio_file = request.files.get("audio")
20
-
21
- if not text or not audio_file:
22
- return jsonify({"error": "Missing text or audio file"}), 400
23
-
24
- # Save uploaded audio temporarily
25
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio:
26
- audio_path = tmp_audio.name
27
- audio_file.save(audio_path)
28
-
29
- # Generate cloned voice
30
- output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
31
- tts.tts_to_file(text=text, speaker_wav=audio_path, language="en", file_path=output_path)
32
-
33
- # Return generated audio
34
- return send_file(output_path, mimetype="audio/wav", as_attachment=True, download_name="output.wav")
35
-
36
- except Exception as e:
37
- return jsonify({"error": str(e)}), 500
38
-
39
- if __name__ == "__main__":
40
- app.run(host="0.0.0.0", port=5000, debug=True)
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
  from TTS.api import TTS
5
  import os
 
 
 
6
  os.environ["COQUI_TOS_AGREED"] = "1"
7
 
8
+ device = "cuda"
9
+
10
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
11
+
12
+ @spaces.GPU(enable_queue=True)
13
+ def clone(text, audio):
14
+ tts.tts_to_file(text=text, speaker_wav=audio, language="en", file_path="./output.wav")
15
+ return "./output.wav"
16
+
17
+ iface = gr.Interface(fn=clone,
18
+ inputs=[gr.Textbox(label='Text'),gr.Audio(type='filepath', label='Voice reference audio file')],
19
+ outputs=gr.Audio(type='filepath'),
20
+ title='Voice Clone',
21
+ description="""
22
+ by [Tony Assi](https://www.tonyassi.com/)
23
+
24
+ ---
25
+
26
+ ### If you like voice clone then try [Video Face Swap](https://huggingface.co/spaces/tonyassi/video-face-swap)!
27
+
28
+ ---
29
+
30
+ This space uses xtts_v2 model. Non-commercial use only. [Coqui Public Model License](https://coqui.ai/cpml)
31
+
32
+ Please ❤️ this Space. <a href="mailto: tony.assi.media@gmail.com">Email me</a>.
33
+ """,
34
+ theme = gr.themes.Base(primary_hue="teal",secondary_hue="teal",neutral_hue="slate"),
35
+ examples=[["Hey! It's me Dorthy, from the Wizard of Oz. Type in whatever you'd like me to say.","./audio/Wizard-of-Oz-Dorthy.wav"],
36
+ ["It's me Vito Corleone, from the Godfather. Type in whatever you'd like me to say.","./audio/Godfather.wav"],
37
+ ["Hey, it's me Paris Hilton. Type in whatever you'd like me to say.","./audio/Paris-Hilton.mp3"],
38
+ ["Hey, it's me Megan Fox from Transformers. Type in whatever you'd like me to say.","./audio/Megan-Fox.mp3"],
39
+ ["Hey there, it's me Jeff Goldblum. Type in whatever you'd like me to say.","./audio/Jeff-Goldblum.mp3"],
40
+ ["Hey there, it's me Heath Ledger as the Joker. Type in whatever you'd like me to say.","./audio/Heath-Ledger.mp3"],])
41
+ iface.launch()