younes_edition / app.py
younes21000's picture
Update app.py
3c50ec0 verified
raw
history blame
3.29 kB
import gradio as gr
import whisper
import os
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
from docx import Document
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.pagesizes import A4
import arabic_reshaper
from bidi.algorithm import get_display
from pptx import Presentation
import subprocess
import shlex
# Define available Whisper models
whisper_models = {
"Tiny (Fast, Less Accurate)": "tiny",
"Base (Medium Speed, Medium Accuracy)": "base",
"Small (Good Speed, Good Accuracy)": "small",
"Medium (Slow, High Accuracy)": "medium",
"Large (Very Slow, Highest Accuracy)": "large"
}
# Load M2M100 translation model for different languages
def load_translation_model(target_language):
lang_codes = {
"fa": "fa", # Persian (Farsi)
"es": "es", # Spanish
"fr": "fr", # French
"de": "de", # German
"it": "it", # Italian
"pt": "pt", # Portuguese
"ar": "ar", # Arabic
"zh": "zh", # Chinese
"hi": "hi", # Hindi
"ja": "ja", # Japanese
"ko": "ko", # Korean
"ru": "ru", # Russian
"fi": "fi" # Finnish
}
target_lang_code = lang_codes.get(target_language)
if not target_lang_code:
raise ValueError(f"Translation model for {target_language} not supported")
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
translation_model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
tokenizer.src_lang = "en"
tokenizer.tgt_lang = target_lang_code
return tokenizer, translation_model
def translate_text(text, tokenizer, model):
try:
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
translated = model.generate(**inputs, forced_bos_token_id=tokenizer.get_lang_id(tokenizer.tgt_lang))
return tokenizer.decode(translated[0], skip_special_tokens=True)
except Exception as e:
raise RuntimeError(f"Error during translation: {e}")
# (Other code remains unchanged)
# Gradio Interface setup
iface = gr.Interface(
fn=transcribe_video,
inputs=[
gr.File(label="Upload Video File"),
gr.Dropdown(label="Select Original Video Language", choices=["en", "es", "fr", "de", "it", "pt"], value="en"),
gr.Dropdown(label="Select Subtitle Translation Language", choices=["en", "fa", "es", "de", "fr", "it", "pt", "fi"], value="fa"),
gr.Dropdown(label="Select Whisper Model", choices=list(whisper_models.keys()), value="Tiny (Fast, Less Accurate)"),
gr.Radio(label="Choose Output Format", choices=["SRT", "Video with Hardsub", "Word", "PDF", "PowerPoint"], value="Video with Hardsub")
],
outputs=gr.File(label="Download File"),
title="Video Subtitle Generator with Translation & Multi-Format Output",
description=(
"This tool allows you to generate subtitles from a video file, translate the subtitles into multiple languages using M2M100, "
"and export them in various formats including SRT, hardcoded subtitles in video, Word, PDF, or PowerPoint."
),
theme="compact",
live=False
)
# Run the interface
iface.launch(share=True)