MohammadYaseen commited on
Commit
6bc44ce
·
verified ·
1 Parent(s): a1f1063

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install necessary libraries if running in a fresh environment
2
+ # pip install streamlit gradio openai-whisper youtube-dl moviepy pydub transformers sentencepiece langchain python-magic pillow ffmpeg tqdm pyannote.audio
3
+
4
+ import os
5
+ import streamlit as st
6
+ from pytube import YouTube
7
+ from moviepy.video.io.VideoFileClip import VideoFileClip
8
+ from pydub import AudioSegment
9
+ import whisper
10
+ from transformers import pipeline, MarianMTModel, MarianTokenizer
11
+
12
+
13
+ # App Configuration
14
+ st.set_page_config(page_title="Video-to-Text Summarizer", layout="centered")
15
+
16
+ # Welcome Page
17
+ st.title("🎥 Video-to-Text Summarization App")
18
+ st.markdown("""
19
+ This app helps you:
20
+ - Convert videos into text and summarize them.
21
+ - Extract multilingual transcriptions and translations.
22
+ - Process videos with multiple speakers.
23
+
24
+ Navigate using the sidebar!
25
+ """)
26
+
27
+ # Sidebar for Navigation
28
+ st.sidebar.header("Navigation")
29
+ nav_option = st.sidebar.radio("Go to", ["Welcome", "Upload Video", "Process Video", "Results"])
30
+
31
+ # Global Variables
32
+ video_path = None
33
+ audio_path = None
34
+ transcription = None
35
+ summary = None
36
+ translated_summary = None
37
+
38
+
39
+ # Upload Video Section
40
+ if nav_option == "Upload Video":
41
+ st.header("Upload Your Video")
42
+ upload_option = st.selectbox("Select Upload Method", ["Local", "YouTube URL"])
43
+
44
+ if upload_option == "Local":
45
+ video_file = st.file_uploader("Upload your video file", type=["mp4", "mkv", "avi"])
46
+ if video_file:
47
+ with open("uploaded_video.mp4", "wb") as f:
48
+ f.write(video_file.read())
49
+ video_path = "uploaded_video.mp4"
50
+ st.success("Video uploaded successfully!")
51
+
52
+ elif upload_option == "YouTube URL":
53
+ youtube_url = st.text_input("Enter YouTube URL")
54
+ if youtube_url:
55
+ os.system(f"yt-dlp -o video.mp4 {youtube_url}")
56
+ video_path = "video.mp4"
57
+ st.success("YouTube video downloaded successfully!")
58
+
59
+ # Process Video Section
60
+ if nav_option == "Process Video" and video_path:
61
+ st.header("Process Your Video")
62
+
63
+ # Video Chunking
64
+ def chunk_video(video_path, chunk_duration=300):
65
+ try:
66
+ video = VideoFileClip(video_path)
67
+ total_duration = int(video.duration)
68
+ for start in range(0, total_duration, chunk_duration):
69
+ end = min(start + chunk_duration, total_duration)
70
+ chunk = video.subclip(start, end)
71
+ chunk.write_videofile(f"chunk_{start}-{end}.mp4")
72
+ st.success("Video chunked successfully!")
73
+ except Exception as e:
74
+ st.error(f"Error in chunking video: {str(e)}")
75
+
76
+ # Audio Extraction
77
+ def extract_audio(video_path):
78
+ try:
79
+ audio = AudioSegment.from_file(video_path)
80
+ audio.export("extracted_audio.mp3", format="mp3")
81
+ st.success("Audio extracted successfully!")
82
+ return "extracted_audio.mp3"
83
+ except Exception as e:
84
+ st.error(f"Error in extracting audio: {str(e)}")
85
+ return None
86
+
87
+ st.write("Processing your video...")
88
+ audio_path = extract_audio(video_path)
89
+
90
+ # Transcription
91
+ def transcribe_audio(audio_path):
92
+ try:
93
+ model = whisper.load_model("base")
94
+ result = model.transcribe(audio_path)
95
+ st.text_area("Transcription", result['text'], height=200)
96
+ return result['text']
97
+ except Exception as e:
98
+ st.error(f"Error in transcription: {str(e)}")
99
+ return None
100
+
101
+ if audio_path:
102
+ transcription = transcribe_audio(audio_path)
103
+
104
+ # Summarization Section
105
+ if nav_option == "Results" and transcription:
106
+ st.header("Results")
107
+
108
+ # Summarization
109
+ def summarize_text(text):
110
+ try:
111
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
112
+ summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
113
+ st.text_area("Summary", summary[0]['summary_text'], height=150)
114
+ return summary[0]['summary_text']
115
+ except Exception as e:
116
+ st.error(f"Error in summarization: {str(e)}")
117
+ return None
118
+
119
+ summary = summarize_text(transcription)
120
+
121
+ # Translation
122
+ def translate_text(text, src_lang="en", tgt_lang="es"):
123
+ try:
124
+ model_name = f"Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}"
125
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
126
+ model = MarianMTModel.from_pretrained(model_name)
127
+ translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True))
128
+ translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
129
+ st.text_area("Translated Summary", translated_text, height=150)
130
+ return translated_text
131
+ except Exception as e:
132
+ st.error(f"Error in translation: {str(e)}")
133
+ return None
134
+
135
+ target_language = st.selectbox("Select Translation Language", ["es", "fr", "de", "zh"])
136
+ if target_language:
137
+ translated_summary = translate_text(summary, tgt_lang=target_language)
138
+
139
+ # Error Notifications
140
+ if nav_option in ["Process Video", "Results"] and not video_path:
141
+ st.error("Please upload a video to proceed.")