Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import traceback
|
| 4 |
-
import
|
| 5 |
-
from transformers import MarianMTModel, MarianTokenizer
|
| 6 |
-
import torch
|
| 7 |
-
import numpy as np
|
| 8 |
-
import wave
|
| 9 |
|
| 10 |
# -------------------------------
|
| 11 |
# 1. Audio extraction
|
|
@@ -17,16 +13,7 @@ def extract_audio(video_path):
|
|
| 17 |
return audio_path
|
| 18 |
|
| 19 |
# -------------------------------
|
| 20 |
-
# 2.
|
| 21 |
-
# -------------------------------
|
| 22 |
-
def read_wav(path):
|
| 23 |
-
with wave.open(path, "rb") as wf:
|
| 24 |
-
frames = wf.readframes(wf.getnframes())
|
| 25 |
-
audio = np.frombuffer(frames, dtype=np.int16)
|
| 26 |
-
return audio
|
| 27 |
-
|
| 28 |
-
# -------------------------------
|
| 29 |
-
# 3. Simple STT using Silero
|
| 30 |
# -------------------------------
|
| 31 |
def stt(audio_path):
|
| 32 |
try:
|
|
@@ -46,31 +33,19 @@ def stt(audio_path):
|
|
| 46 |
return f"STT Error: {traceback.format_exc()}"
|
| 47 |
|
| 48 |
# -------------------------------
|
| 49 |
-
#
|
| 50 |
# -------------------------------
|
| 51 |
def translate_text(text, target_lang):
|
| 52 |
if target_lang == "original":
|
| 53 |
return text
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
"ps": "ps",
|
| 60 |
-
"ar": "ar"
|
| 61 |
-
}
|
| 62 |
-
tgt = lang_map.get(target_lang, "en")
|
| 63 |
-
model_name = f"Helsinki-NLP/opus-mt-en-{tgt}" if tgt != "en" else "Helsinki-NLP/opus-mt-en-en"
|
| 64 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 65 |
-
model = MarianMTModel.from_pretrained(model_name)
|
| 66 |
-
|
| 67 |
-
batch = tokenizer([text], return_tensors="pt", padding=True)
|
| 68 |
-
translated = model.generate(**batch)
|
| 69 |
-
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 70 |
-
return translated_text
|
| 71 |
|
| 72 |
# -------------------------------
|
| 73 |
-
#
|
| 74 |
# -------------------------------
|
| 75 |
def process_video(video, lang):
|
| 76 |
try:
|
|
@@ -88,7 +63,7 @@ def process_video(video, lang):
|
|
| 88 |
return "", f"β ERROR:\n{traceback.format_exc()}"
|
| 89 |
|
| 90 |
# -------------------------------
|
| 91 |
-
#
|
| 92 |
# -------------------------------
|
| 93 |
languages = {
|
| 94 |
"original": "Original",
|
|
@@ -100,7 +75,7 @@ languages = {
|
|
| 100 |
}
|
| 101 |
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
-
gr.Markdown("## π¬ Video β Text β Translation (
|
| 104 |
|
| 105 |
video_input = gr.Video(label="Upload Video")
|
| 106 |
lang_dropdown = gr.Dropdown(list(languages.keys()), value="original", label="Translate To")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import traceback
|
| 4 |
+
from deep_translator import GoogleTranslator
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
# 1. Audio extraction
|
|
|
|
| 13 |
return audio_path
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
+
# 2. STT with Silero
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# -------------------------------
|
| 18 |
def stt(audio_path):
|
| 19 |
try:
|
|
|
|
| 33 |
return f"STT Error: {traceback.format_exc()}"
|
| 34 |
|
| 35 |
# -------------------------------
|
| 36 |
+
# 3. Translation
|
| 37 |
# -------------------------------
|
| 38 |
def translate_text(text, target_lang):
|
| 39 |
if target_lang == "original":
|
| 40 |
return text
|
| 41 |
+
try:
|
| 42 |
+
translated = GoogleTranslator(source='auto', target=target_lang).translate(text)
|
| 43 |
+
return translated
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Translation Error: {traceback.format_exc()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# -------------------------------
|
| 48 |
+
# 4. Main processing
|
| 49 |
# -------------------------------
|
| 50 |
def process_video(video, lang):
|
| 51 |
try:
|
|
|
|
| 63 |
return "", f"β ERROR:\n{traceback.format_exc()}"
|
| 64 |
|
| 65 |
# -------------------------------
|
| 66 |
+
# 5. Gradio UI
|
| 67 |
# -------------------------------
|
| 68 |
languages = {
|
| 69 |
"original": "Original",
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("## π¬ Video β Text β Translation (Token-free, CPU, HF Spaces Free)")
|
| 79 |
|
| 80 |
video_input = gr.Video(label="Upload Video")
|
| 81 |
lang_dropdown = gr.Dropdown(list(languages.keys()), value="original", label="Translate To")
|