Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,48 +2,42 @@ import os
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
import moviepy.editor as mp
|
5 |
-
from TTS.api import TTS
|
6 |
import torch
|
7 |
import assemblyai as aai
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
try:
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
r = requests.get(url)
|
39 |
-
with open(file_path, 'wb') as f:
|
40 |
-
f.write(r.content)
|
41 |
-
|
42 |
-
|
43 |
|
44 |
# Translation class
|
45 |
-
class
|
46 |
-
def
|
47 |
self.video_path = video_path
|
48 |
self.original_language = original_language
|
49 |
self.target_language = target_language
|
@@ -83,13 +77,15 @@ class translation:
|
|
83 |
translation = response.json()[0]["translations"][0]["text"]
|
84 |
return translation
|
85 |
|
86 |
-
# def generate_audio(self, translated_text):
|
87 |
-
# tts.tts_to_file(text=translated_text, speaker_wav='output_audio.wav', file_path="output_synth.wav", language=self.tran_code)
|
88 |
-
# return "output_synth.wav"
|
89 |
-
|
90 |
def generate_audio(self, translated_text):
|
91 |
try:
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
return "output_synth.wav"
|
94 |
except Exception as e:
|
95 |
print(f"Error generating audio: {e}")
|
@@ -110,7 +106,7 @@ class translation:
|
|
110 |
|
111 |
# Gradio Interface
|
112 |
def app(video_path, original_language, target_language):
|
113 |
-
translator =
|
114 |
video_file = translator.translate_video()
|
115 |
return video_file
|
116 |
|
@@ -124,4 +120,4 @@ interface = gr.Interface(
|
|
124 |
outputs=gr.Video(label="Translated Video")
|
125 |
)
|
126 |
|
127 |
-
interface.launch()
|
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
import moviepy.editor as mp
|
|
|
5 |
import torch
|
6 |
import assemblyai as aai
|
7 |
|
8 |
+
# Import specific model components
|
9 |
+
from TTS.tts.configs.xtts_config import XttsConfig
|
10 |
+
from TTS.tts.models.xtts import Xtts
|
11 |
+
|
12 |
+
# Define paths for model and configuration files
|
13 |
+
model_path = "./xtts_v2"
|
14 |
+
config_path = os.path.join(model_path, "config.json")
|
15 |
+
checkpoint_path = model_path
|
16 |
+
|
17 |
+
# Initialize and load the XTTS model
|
18 |
+
config = XttsConfig()
|
19 |
+
config.load_json(config_path)
|
20 |
+
model = Xtts.init_from_config(config)
|
21 |
+
model.load_checkpoint(config, checkpoint_dir=checkpoint_path, eval=True)
|
22 |
+
model.cuda() # Move model to GPU if available
|
23 |
+
|
24 |
+
def synthesize_text(text, speaker_wav, language):
|
25 |
+
try:
|
26 |
+
outputs = model.synthesize(
|
27 |
+
text,
|
28 |
+
config,
|
29 |
+
speaker_wav=speaker_wav,
|
30 |
+
gpt_cond_len=3,
|
31 |
+
language=language
|
32 |
+
)
|
33 |
+
return outputs
|
34 |
+
except Exception as e:
|
35 |
+
print(f"Error during synthesis: {e}")
|
36 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Translation class
|
39 |
+
class Translation:
|
40 |
+
def _init_(self, video_path, original_language, target_language):
|
41 |
self.video_path = video_path
|
42 |
self.original_language = original_language
|
43 |
self.target_language = target_language
|
|
|
77 |
translation = response.json()[0]["translations"][0]["text"]
|
78 |
return translation
|
79 |
|
|
|
|
|
|
|
|
|
80 |
def generate_audio(self, translated_text):
|
81 |
try:
|
82 |
+
synthesized_audio = synthesize_text(
|
83 |
+
translated_text,
|
84 |
+
speaker_wav='output_audio.wav',
|
85 |
+
language=self.tran_code
|
86 |
+
)
|
87 |
+
with open("output_synth.wav", "wb") as f:
|
88 |
+
f.write(synthesized_audio)
|
89 |
return "output_synth.wav"
|
90 |
except Exception as e:
|
91 |
print(f"Error generating audio: {e}")
|
|
|
106 |
|
107 |
# Gradio Interface
|
108 |
def app(video_path, original_language, target_language):
|
109 |
+
translator = Translation(video_path, original_language, target_language)
|
110 |
video_file = translator.translate_video()
|
111 |
return video_file
|
112 |
|
|
|
120 |
outputs=gr.Video(label="Translated Video")
|
121 |
)
|
122 |
|
123 |
+
interface.launch(share=True) # Optional: Set share=True to create a public link
|