File size: 2,082 Bytes
c9612c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35575b2
c9612c1
35575b2
 
 
 
c9612c1
 
 
 
 
 
 
 
 
 
79fc358
 
 
 
 
 
 
 
 
 
 
 
c9612c1
 
 
 
 
35575b2
 
79fc358
 
 
c9612c1
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import moviepy.editor as mp
from transformers import pipeline

# Load Whisper model for speech-to-text
asr = pipeline("automatic-speech-recognition", model="openai/whisper-large")

# MarianMT or M2M100 for translation (multi-language)
translator = pipeline("translation", model="facebook/m2m100_418M")

def generate_subtitles(video_path, target_language):
    # Extract audio from video
    video = mp.VideoFileClip(video_path)
    audio = video.audio
    audio.write_audiofile("temp_audio.wav", codec='pcm_s16le')

    # Convert speech to text (ASR using Whisper)
    with open("temp_audio.wav", "rb") as audio_file:
        transcription = asr(audio_file)["text"]

    # Translate transcription to the target language using M2M100
    translation_pipeline = pipeline('translation', model='facebook/m2m100_418M')
    translated_subtitles = translation_pipeline(
        transcription, 
        forced_bos_token_id=translation_pipeline.tokenizer.get_lang_id(target_language)
    )[0]["translation_text"]

    # Return subtitles (text for now)
    subtitles = f"Original: {transcription}\nTranslated: {translated_subtitles}"
    return subtitles

# Define Gradio interface
def subtitle_video(video_file, target_language):
    video_path = video_file.name
    return generate_subtitles(video_path, target_language)

# List of supported languages with their codes for M2M100
languages = {
    "Persian (fa)": "fa",
    "French (fr)": "fr",
    "Spanish (es)": "es",
    "German (de)": "de",
    "Chinese (zh)": "zh",
    "Arabic (ar)": "ar",
    "Hindi (hi)": "hi",
    "Russian (ru)": "ru"
}

# Gradio app layout
interface = gr.Interface(
    fn=subtitle_video,
    inputs=[
        gr.Video(label="Upload Video"),
        gr.Dropdown(  # Dropdown for language selection
            label="Choose Target Language",
            choices=list(languages.keys()),  # Display language names in the dropdown
            value="Persian (fa)"  # Default language
        )
    ],
    outputs="text",
    title="Automatic Video Subtitler & Translator"
)

interface.launch()