Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,109 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import traceback
|
| 4 |
import speech_recognition as sr
|
| 5 |
-
import
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def extract_audio(video_path):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
return audio_path
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
#
|
| 20 |
def transcribe_audio(audio_path):
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return f"STT Error:\n{traceback.format_exc()}"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
#
|
| 33 |
-
def
|
| 34 |
if target_lang == "original":
|
| 35 |
return text
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# assume first installed language is source
|
| 44 |
-
translated = installed_langs[0].translate(text, target_lang_obj)
|
| 45 |
return translated
|
| 46 |
-
|
| 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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return "", f"β ERROR:\n{traceback.format_exc()}"
|
| 65 |
-
|
| 66 |
-
# -------------------------------
|
| 67 |
-
# 5. Gradio UI
|
| 68 |
-
# -------------------------------
|
| 69 |
languages = {
|
| 70 |
-
"original":"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
}
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
debug_box = gr.Textbox(label="Debug Logs", lines=8)
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|