product2204 commited on
Commit
38282c7
1 Parent(s): d6fefb0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from TTS.api import TTS
4
+ import os
5
+ from datetime import datetime
6
+
7
+ # Function to process text and voice input, then generate speech
8
+ def tts_process(transcript_file, voice_file):
9
+ # Initialize TTS with your model path
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
12
+
13
+ # Read transcript text from uploaded file
14
+ text = transcript_file.read().decode("utf-8")
15
+
16
+ # Generate output file name with timestamp
17
+ timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
18
+ output_file_name = f"Download_{timestamp}.wav"
19
+
20
+ # Assuming the voice cloning model accepts paths, save files temporarily
21
+ transcript_path = f"temp_transcript_{timestamp}.txt"
22
+ voice_path = f"temp_voice_{timestamp}.wav"
23
+ with open(transcript_path, 'w') as f:
24
+ f.write(text)
25
+ with open(voice_path, 'wb') as f:
26
+ f.write(voice_file.read())
27
+
28
+ # Generate speech and save to a file
29
+ tts.tts_to_file(text=text, speaker_wav=voice_path, language="en", file_path=output_file_name)
30
+
31
+ # Cleanup temporary files
32
+ os.remove(transcript_path)
33
+ os.remove(voice_path)
34
+
35
+ return output_file_name
36
+
37
+ # Gradio interface setup
38
+ iface = gr.Interface(fn=tts_process,
39
+ inputs=[gr.inputs.File(label="Upload Transcript Text File"),
40
+ gr.inputs.File(label="Upload Voice File for Cloning")],
41
+ outputs=gr.outputs.File(label="Download Speech Output"),
42
+ title="TTS Voice Cloning",
43
+ description="Upload a transcript text file and a voice file to clone the voice and generate speech.")
44
+
45
+ # Execute only if run as a script
46
+ if __name__ == "__main__":
47
+ iface.launch(share=True)