Salman11223 commited on
Commit
54daa0d
·
verified ·
1 Parent(s): 65495bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -44
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
- # Download necessary models if not already present
10
- model_files = {
11
- "wav2lip.pth": "https://github.com/justinjohn0306/Wav2Lip/releases/download/models/wav2lip.pth",
12
- "wav2lip_gan.pth": "https://github.com/justinjohn0306/Wav2Lip/releases/download/models/wav2lip_gan.pth",
13
- "resnet50.pth": "https://github.com/justinjohn0306/Wav2Lip/releases/download/models/resnet50.pth",
14
- "mobilenet.pth": "https://github.com/justinjohn0306/Wav2Lip/releases/download/models/mobilenet.pth",
15
- "s3fd.pth": "https://www.adrianbulat.com/downloads/python-fan/s3fd-619a316812.pth"
16
- }
17
-
18
-
19
- tts_model_path = "./xtts_v2"
20
-
21
- # Initialize TTS model
22
- # tts = TTS(tts_model_path, gpu=True)
23
-
24
-
25
- # Initialize TTS model with verbose output
26
- try:
27
- print(f"Initializing TTS model from path: {tts_model_path}")
28
- tts = TTS(tts_model_path, gpu=True)
29
- print("TTS model initialized successfully.")
30
- except Exception as e:
31
- print(f"Error initializing TTS model: {e}")
32
-
33
-
34
- for filename, url in model_files.items():
35
- file_path = os.path.join("checkpoints" if "pth" in filename else "face_detection", filename)
36
- if not os.path.exists(file_path):
37
- print(f"Downloading {filename}...")
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 translation:
46
- def __init__(self, video_path, original_language, target_language):
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
- tts.tts_to_file(text=translated_text, speaker_wav='output_audio.wav', file_path="output_synth.wav", language=self.tran_code)
 
 
 
 
 
 
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 = translation(video_path, original_language, target_language)
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