File size: 4,517 Bytes
9bce705
 
 
 
 
 
 
 
 
5d9508a
 
9bce705
87ecfda
3a20681
9bce705
86e7999
9bce705
 
0df6496
 
9bce705
5d9508a
9bce705
86e7999
9bce705
86e7999
 
9bce705
 
 
 
 
 
86e7999
 
9bce705
 
 
 
 
 
 
 
86e7999
9bce705
86e7999
0df6496
86e7999
 
 
e357915
 
 
 
 
 
 
 
86e7999
e357915
 
 
 
 
 
0df6496
 
e357915
 
 
 
 
86e7999
e357915
0df6496
9bce705
86e7999
0df6496
9bce705
 
e357915
 
 
9bce705
 
 
 
 
 
 
 
 
 
e357915
 
0df6496
9bce705
 
 
 
 
 
 
 
 
 
 
 
 
 
e357915
 
9bce705
86e7999
9bce705
5d9508a
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import streamlit as st
from moviepy.video.io.VideoFileClip import VideoFileClip
from pydub import AudioSegment
import whisper
from transformers import pipeline, MarianMTModel, MarianTokenizer
import yt_dlp as youtube_dl

# App Configuration
st.set_page_config(page_title="Video-to-Text Summarization", layout="wide")

# Header
st.title("πŸŽ₯ Smart Video-to-Text Summarization App")


# Initialize session state
if "video_path" not in st.session_state:
    st.session_state.video_path = None
if "transcription" not in st.session_state:
    st.session_state.transcription = None

# Upload Video Section
st.header("Upload Your Video")
upload_option = st.radio("Choose upload method:", ["πŸ“ Local File", "πŸ“Ί YouTube URL"])

if upload_option == "πŸ“ Local File":
    video_file = st.file_uploader("Upload your video file", type=["mp4", "mkv", "avi"])
    if video_file:
        with open("uploaded_video.mp4", "wb") as f:
            f.write(video_file.read())
        st.session_state.video_path = "uploaded_video.mp4"
        st.success("Video uploaded successfully!")

elif upload_option == "πŸ“Ί YouTube URL":
    youtube_url = st.text_input("Enter YouTube URL")
    if youtube_url:
        try:
            os.system(f"yt-dlp -o video.mp4 {youtube_url}")
            st.session_state.video_path = "video.mp4"
            st.success("YouTube video downloaded successfully!")
        except Exception as e:
            st.error(f"Error downloading video: {str(e)}")

# Display video
if st.session_state.video_path:
    st.header("Preview & Process Video")
    st.video(st.session_state.video_path)

    process_btn = st.button("πŸš€ Process Video", key="process-video", use_container_width=True)

    if process_btn:
        def extract_audio(video_path):
            try:
                audio = AudioSegment.from_file(video_path)
                audio.export("extracted_audio.mp3", format="mp3")
                st.success("Audio extracted successfully!")
                return "extracted_audio.mp3"
            except Exception as e:
                st.error(f"Error extracting audio: {str(e)}")
                return None

        def transcribe_audio(audio_path):
            try:
                model = whisper.load_model("base")
                result = model.transcribe(audio_path)
                st.session_state.transcription = result['text']
                st.text_area("Transcription", st.session_state.transcription, height=200)
                return result['text']
            except Exception as e:
                st.error(f"Error in transcription: {str(e)}")
                return None

        audio_path = extract_audio(st.session_state.video_path)
        if audio_path:
            st.session_state.transcription = transcribe_audio(audio_path)

# Summarization & Translation
if st.session_state.transcription:
    st.header("Results")

    summarize_btn = st.button("πŸ“ Summarize Text")
    translate_btn = st.button("🌍 Translate Summary")

    def summarize_text(text):
        try:
            summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
            summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
            st.text_area("Summary", summary[0]['summary_text'], height=150)
            return summary[0]['summary_text']
        except Exception as e:
            st.error(f"Error in summarization: {str(e)}")
            return None

    summary = None
    if summarize_btn:
        summary = summarize_text(st.session_state.transcription)

    def translate_text(text, src_lang="en", tgt_lang="es"):
        try:
            model_name = f"Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}"
            tokenizer = MarianTokenizer.from_pretrained(model_name)
            model = MarianMTModel.from_pretrained(model_name)
            translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True))
            translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
            st.text_area("Translated Summary", translated_text, height=150)
            return translated_text
        except Exception as e:
            st.error(f"Error in translation: {str(e)}")
            return None

    if summary and translate_btn:
        target_language = st.selectbox("Select Translation Language", ["es", "fr", "de", "zh"], key="lang-select")
        translated_summary = translate_text(summary, tgt_lang=target_language)

else:
    st.info("Please upload a video to start the process.")