broadfield-dev commited on
Commit
eff56bc
·
verified ·
1 Parent(s): f05e521

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ import shutil
5
+ import uuid
6
+ from transformers import pipeline
7
+ from gtts import gTTS
8
+
9
+
10
+ def translate_video(file_path):
11
+
12
+ try:
13
+ audio_path = os.path.join(file_path, "audio.wav")
14
+
15
+ if not os.path.exists(audio_path):
16
+ raise FileNotFoundError("Audio extraction failed. yt-dlp did not produce a .wav file.")
17
+
18
+ # 3. Translate the audio using the whisper-tiny model
19
+ translator = pipeline(
20
+ "automatic-speech-recognition",
21
+ model="openai/whisper-tiny",
22
+ device="cpu"
23
+ )
24
+
25
+ translation = translator(audio_path, return_timestamps=True, generate_kwargs={"task": "translate"})
26
+
27
+ translated_text = translation["text"]
28
+
29
+ if not translated_text:
30
+ return "No speech was detected in the video.", None, video_path
31
+
32
+ # 4. Convert translated text to speech using gTTS
33
+ tts = gTTS(translated_text.strip(), lang='en')
34
+ translated_audio_path = os.path.join(temp_dir, "translated_audio.mp3")
35
+ tts.save(translated_audio_path)
36
+
37
+ return translated_text, translated_audio_path, video_path
38
+
39
+ except Exception as e:
40
+ gr.Warning(f"An unexpected error occurred: {str(e)}")
41
+ return f"An error occurred: {str(e)}", None, None
42
+
43
+ # Create the Gradio interface
44
+ iface = gr.Interface(
45
+ fn=translate_video,
46
+ inputs=gr.Video(label="Upload your video to translate"),
47
+ outputs=[
48
+ gr.Textbox(label="Translated Text", interactive=False),
49
+ gr.Audio(label="Translated Audio"),
50
+ gr.Video(label="Original Video"),
51
+ ],
52
+ title="Twitter/X Video Translator",
53
+ description="Enter a link to a Twitter/X video to translate its audio to English. Handles videos longer than 30 seconds.",
54
+ allow_flagging="never",
55
+ examples=[["https://x.com/OpenAI/status/1790145460393734444"]]
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ if not os.path.exists("downloads"):
60
+ os.makedirs("downloads")
61
+ iface.launch()