Salman11223 commited on
Commit
c7cb1f7
·
verified ·
1 Parent(s): 02405e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -46
app.py CHANGED
@@ -2,42 +2,33 @@ import os
2
  import requests
3
  import gradio as gr
4
  import moviepy.editor as mp
 
5
  import torch
6
  import assemblyai as aai
7
- from xtts_v2 import model.pth
8
-
9
- # Import specific model components
10
- from TTS.tts.configs.xtts_config import XttsConfig
11
- from TTS.tts.models.xtts import Xtts
12
-
13
- # Define paths for model and configuration files
14
- # model_path = "/xtts_v2"
15
- #config_path = os.path.join(model_path, "/config.json")
16
- # checkpoint_path = model_path
17
-
18
- # Initialize and load the XTTS model
19
- config = XttsConfig("/xtts_v2/config.json")
20
- model = Xtts.init_from_config(config)
21
- model.load_checkpoint(config, checkpoint_dir="/xtts_v2", 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
@@ -78,18 +69,8 @@ class Translation:
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}")
92
- raise
93
 
94
  def translate_video(self):
95
  audio_path = self.extract_audio()
@@ -106,7 +87,7 @@ class Translation:
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,4 +101,4 @@ interface = gr.Interface(
120
  outputs=gr.Video(label="Translated Video")
121
  )
122
 
123
- interface.launch(share=True) # Optional: Set share=True to create a public link
 
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
+ for filename, url in model_files.items():
19
+ file_path = os.path.join("checkpoints" if "pth" in filename else "face_detection", filename)
20
+ if not os.path.exists(file_path):
21
+ print(f"Downloading {filename}...")
22
+ r = requests.get(url)
23
+ with open(file_path, 'wb') as f:
24
+ f.write(r.content)
25
+
26
+ # Initialize TTS model without prompts
27
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=True, progress_bar=False)
 
 
 
 
 
 
 
 
 
 
28
 
29
  # Translation class
30
+ class translation:
31
+ def init(self, video_path, original_language, target_language):
32
  self.video_path = video_path
33
  self.original_language = original_language
34
  self.target_language = target_language
 
69
  return translation
70
 
71
  def generate_audio(self, translated_text):
72
+ tts.tts_to_file(text=translated_text, speaker_wav='output_audio.wav', file_path="output_synth.wav", language=self.tran_code)
73
+ return "output_synth.wav"
 
 
 
 
 
 
 
 
 
 
74
 
75
  def translate_video(self):
76
  audio_path = self.extract_audio()
 
87
 
88
  # Gradio Interface
89
  def app(video_path, original_language, target_language):
90
+ translator = translation(video_path, original_language, target_language)
91
  video_file = translator.translate_video()
92
  return video_file
93
 
 
101
  outputs=gr.Video(label="Translated Video")
102
  )
103
 
104
+ interface.launch()