Ahmadkhan12 commited on
Commit
9ce4eb9
Β·
verified Β·
1 Parent(s): c576b8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -69
app.py CHANGED
@@ -1,90 +1,109 @@
1
  import gradio as gr
2
- import subprocess
3
- import traceback
4
  import speech_recognition as sr
5
- import argostranslate.package
6
- import argostranslate.translate
 
7
 
8
- # -------------------------------
9
- # 1. Extract audio from video
10
- # -------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
11
  def extract_audio(video_path):
12
- audio_path = "audio.wav"
13
- cmd = f"ffmpeg -y -i '{video_path}' -ar 16000 -ac 1 -f wav {audio_path}"
14
- subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
15
  return audio_path
16
 
17
- # -------------------------------
18
- # 2. Transcribe using CMU Sphinx (offline)
19
- # -------------------------------
20
  def transcribe_audio(audio_path):
 
 
 
21
  try:
22
- recognizer = sr.Recognizer()
23
- with sr.AudioFile(audio_path) as source:
24
- audio = recognizer.record(source)
25
- text = recognizer.recognize_sphinx(audio)
26
- return text.strip()
27
- except Exception:
28
- return f"STT Error:\n{traceback.format_exc()}"
29
 
30
- # -------------------------------
31
- # 3. Translate text offline
32
- # -------------------------------
33
- def translate_text_offline(text, target_lang):
34
  if target_lang == "original":
35
  return text
36
- try:
37
- installed_langs = argostranslate.translate.get_installed_languages()
38
- lang_map = {"en":"en","ur":"ur","hi":"hi","ps":"ps","ar":"ar"}
39
- target_code = lang_map.get(target_lang,"en")
40
- target_lang_obj = next((l for l in installed_langs if l.code==target_code), None)
41
- if not installed_langs or not target_lang_obj:
42
- return text
43
- # assume first installed language is source
44
- translated = installed_langs[0].translate(text, target_lang_obj)
45
  return translated
46
- except Exception:
47
- return f"Translation Error:\n{traceback.format_exc()}"
48
-
49
- # -------------------------------
50
- # 4. Main processing
51
- # -------------------------------
52
- def process_video(video, lang):
53
- try:
54
- if not video:
55
- return "", "No video uploaded"
56
-
57
- audio_path = extract_audio(video)
58
- transcription = transcribe_audio(audio_path)
59
- translation = translate_text_offline(transcription, lang)
60
 
61
- logs = f"Audio extracted!\nTranscribed text length: {len(transcription)}"
62
- return translation, logs
63
- except Exception:
64
- return "", f"❌ ERROR:\n{traceback.format_exc()}"
65
-
66
- # -------------------------------
67
- # 5. Gradio UI
68
- # -------------------------------
69
  languages = {
70
- "original":"Original",
71
- "en":"English",
72
- "ur":"Urdu",
73
- "hi":"Hindi",
74
- "ps":"Pashto",
75
- "ar":"Arabic"
76
  }
77
 
78
- with gr.Blocks() as demo:
79
- gr.Markdown("## 🎬 Video β†’ Text β†’ Translation (Offline, CPU, Token-free)")
 
 
 
 
 
80
 
81
- video_input = gr.Video(label="Upload Video")
82
- lang_dropdown = gr.Dropdown(list(languages.keys()), value="original", label="Translate To")
83
- btn = gr.Button("Generate Text")
 
 
 
 
 
 
 
 
 
84
 
85
- text_output = gr.Textbox(label="Transcribed / Translated Text", lines=10)
86
- debug_box = gr.Textbox(label="Debug Logs", lines=8)
87
 
88
- btn.click(process_video, inputs=[video_input, lang_dropdown], outputs=[text_output, debug_box])
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  demo.launch()
 
1
  import gradio as gr
2
+ from moviepy.editor import VideoFileClip
 
3
  import speech_recognition as sr
4
+ from argostranslate import package, translate
5
+ import os
6
+ import tempfile
7
 
8
+ # ---------------------------
9
+ # Setup Argos Translate (offline)
10
+ # ---------------------------
11
+ def setup_translation():
12
+ # Download English->Urdu model if not exists
13
+ if not os.path.exists("en_ur.argosmodel"):
14
+ import urllib.request
15
+ url = "https://www.argosopentech.com/argospm/models/en_ur.argosmodel"
16
+ urllib.request.urlretrieve(url, "en_ur.argosmodel")
17
+ package.install_from_path("en_ur.argosmodel")
18
+ setup_translation()
19
+
20
+ # ---------------------------
21
+ # Extract audio from video
22
+ # ---------------------------
23
  def extract_audio(video_path):
24
+ clip = VideoFileClip(video_path)
25
+ audio_path = tempfile.mktemp(suffix=".wav")
26
+ clip.audio.write_audiofile(audio_path, fps=16000, codec="pcm_s16le")
27
  return audio_path
28
 
29
+ # ---------------------------
30
+ # Transcribe audio using CMU Sphinx
31
+ # ---------------------------
32
  def transcribe_audio(audio_path):
33
+ r = sr.Recognizer()
34
+ with sr.AudioFile(audio_path) as source:
35
+ audio = r.record(source)
36
  try:
37
+ text = r.recognize_sphinx(audio)
38
+ return text
39
+ except sr.UnknownValueError:
40
+ return "[Could not understand audio]"
41
+ except sr.RequestError as e:
42
+ return f"[Sphinx error: {e}]"
 
43
 
44
+ # ---------------------------
45
+ # Translate text using Argos Translate
46
+ # ---------------------------
47
+ def translate_text(text, target_lang):
48
  if target_lang == "original":
49
  return text
50
+ from_lang = "en"
51
+ to_lang = target_lang
52
+ installed_languages = translate.get_installed_languages()
53
+ from_lang_obj = next((l for l in installed_languages if l.code == from_lang), None)
54
+ to_lang_obj = next((l for l in installed_languages if l.code == to_lang), None)
55
+ if from_lang_obj and to_lang_obj:
56
+ translated = from_lang_obj.get_translation(to_lang_obj).translate(text)
 
 
57
  return translated
58
+ return text
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ # ---------------------------
61
+ # Gradio Interface
62
+ # ---------------------------
 
 
 
 
 
63
  languages = {
64
+ "original": "No Translation",
65
+ "ur": "Urdu",
66
+ "hi": "Hindi",
67
+ "ps": "Pashto",
68
+ "ar": "Arabic",
69
+ "en": "English"
70
  }
71
 
72
+ def process_video(video_file, lang):
73
+ error_log = ""
74
+ try:
75
+ audio_path = extract_audio(video_file.name)
76
+ error_log += "Audio extracted!\n"
77
+ except Exception as e:
78
+ return "", f"Audio extraction error: {e}"
79
 
80
+ try:
81
+ text = transcribe_audio(audio_path)
82
+ error_log += f"Transcribed text length: {len(text)}\n"
83
+ except Exception as e:
84
+ return "", f"STT Error: {e}"
85
+
86
+ try:
87
+ translated = translate_text(text, lang)
88
+ error_log += f"Translation done!\n"
89
+ except Exception as e:
90
+ translated = text
91
+ error_log += f"Translation error: {e}\n"
92
 
93
+ return translated, error_log
 
94
 
95
+ demo = gr.Interface(
96
+ fn=process_video,
97
+ inputs=[
98
+ gr.Video(label="Upload Video"),
99
+ gr.Dropdown(list(languages.keys()), value="original", label="Translate To")
100
+ ],
101
+ outputs=[
102
+ gr.Textbox(label="Transcribed / Translated Text", interactive=False),
103
+ gr.Textbox(label="Debug / Error Log", interactive=False)
104
+ ],
105
+ title="Offline Video Subtitle Generator",
106
+ description="Upload a video β†’ Extract audio β†’ Generate subtitles β†’ Optional translation β†’ All offline, token-free"
107
+ )
108
 
109
  demo.launch()