sairaarif89 commited on
Commit
f4bbee0
·
verified ·
1 Parent(s): 6ceb191

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -72
app.py CHANGED
@@ -1,85 +1,70 @@
1
- # app.py
2
  import streamlit as st
 
3
  from moviepy.editor import VideoFileClip
4
  import whisper
5
  from transformers import pipeline
6
- import os
7
-
8
- # Set page title and layout
9
- st.set_page_config(page_title="Video Summarizer", layout="wide")
10
-
11
- # Title
12
- st.title("Video Summarizer 🎥📝")
13
- st.write("Upload a video, and get a transcript, summary, study notes, and answers to your questions!")
14
 
15
- # Step 1: Upload Video
16
- uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
17
- if uploaded_file is not None:
18
- # Save the uploaded video to a temporary file
19
- video_path = "uploaded_video.mp4"
20
- with open(video_path, "wb") as f:
21
- f.write(uploaded_file.getbuffer())
22
- st.success("Video uploaded successfully!")
23
-
24
- # Step 2: Extract Audio
25
- st.header("Step 1: Extract Audio")
26
- audio_path = "audio.wav"
27
  if os.path.exists(audio_path):
28
  os.remove(audio_path)
29
  video = VideoFileClip(video_path)
30
  video.audio.write_audiofile(audio_path)
31
- st.audio(audio_path, format="audio/wav")
32
- st.success("Audio extracted successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Step 3: Transcribe Audio
35
- st.header("Step 2: Transcribe Audio")
36
- if st.button("Transcribe Audio"):
37
- with st.spinner("Transcribing audio..."):
38
- model = whisper.load_model("base")
39
- result = model.transcribe(audio_path)
40
- transcript = result["text"]
41
- st.text_area("Transcript", transcript, height=200)
42
- st.success("Transcription complete!")
43
 
44
- # Step 4: Summarize Transcript
45
- st.header("Step 3: Summarize Transcript")
46
- if st.button("Summarize Transcript"):
47
- with st.spinner("Summarizing transcript..."):
48
- summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
49
- max_chunk_size = 1000
50
- chunks = [transcript[i:i + max_chunk_size] for i in range(0, len(transcript), max_chunk_size)]
51
- summaries = []
52
- for chunk in chunks:
53
- summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
54
- summaries.append(summary[0]["summary_text"])
55
- video_summary = " ".join(summaries)
56
- st.text_area("Summary", video_summary, height=200)
57
- st.success("Summarization complete!")
58
 
59
- # Step 5: Generate Study Notes
60
- st.header("Step 4: Generate Study Notes")
61
- if st.button("Generate Study Notes"):
62
- with st.spinner("Generating study notes..."):
63
- generator = pipeline("text-generation", model="gpt2")
64
- prompt = f"Create study notes from the following summary:\n{video_summary}"
65
- study_notes = generator(
66
- prompt,
67
- max_length=400,
68
- max_new_tokens=200,
69
- num_return_sequences=1,
70
- truncation=True
71
- )
72
- st.text_area("Study Notes", study_notes[0]["generated_text"], height=200)
73
- st.success("Study notes generated!")
74
 
75
- # Step 6: Question Answering
76
- st.header("Step 5: Ask a Question")
77
- question = st.text_input("Enter your question:")
78
- if st.button("Get Answer"):
79
- with st.spinner("Finding answer..."):
80
- qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
81
- answer = qa_pipeline(question=question, context=video_summary)
82
- st.text_area("Answer", answer["answer"], height=100)
83
- st.success("Answer found!")
84
- else:
85
- st.warning("Please upload a video file to get started.")
 
 
1
  import streamlit as st
2
+ import os
3
  from moviepy.editor import VideoFileClip
4
  import whisper
5
  from transformers import pipeline
 
 
 
 
 
 
 
 
6
 
7
+ # Function to extract audio from video
8
+ def extract_audio(video_path, audio_path="audio.wav"):
 
 
 
 
 
 
 
 
 
 
9
  if os.path.exists(audio_path):
10
  os.remove(audio_path)
11
  video = VideoFileClip(video_path)
12
  video.audio.write_audiofile(audio_path)
13
+ return audio_path
14
+
15
+ # Function to transcribe audio using Whisper
16
+ def transcribe_audio(audio_path):
17
+ model = whisper.load_model("base")
18
+ result = model.transcribe(audio_path)
19
+ return result["text"]
20
+
21
+ # Function to summarize text
22
+ def summarize_text(text):
23
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
24
+ max_chunk_size = 1000
25
+ chunks = [text[i:i + max_chunk_size] for i in range(0, len(text), max_chunk_size)]
26
+ summaries = [summarizer(chunk, max_length=130, min_length=30, do_sample=False)[0]["summary_text"] for chunk in chunks]
27
+ return " ".join(summaries)
28
+
29
+ # Function to generate study notes
30
+ def generate_study_notes(summary):
31
+ generator = pipeline("text-generation", model="gpt2")
32
+ prompt = f"Create study notes from the following summary:\n{summary}"
33
+ study_notes = generator(prompt, max_length=400, max_new_tokens=200, num_return_sequences=1, truncation=True)
34
+ return study_notes[0]["generated_text"]
35
+
36
+ # Function to answer questions
37
+ def answer_question(question, context):
38
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
39
+ result = qa_pipeline(question=question, context=context)
40
+ return result["answer"]
41
+
42
+ # Streamlit App
43
+ st.title("Lecture Video Processor")
44
+
45
+ uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"])
46
+
47
+ if uploaded_file:
48
+ video_path = uploaded_file.name
49
+ with open(video_path, "wb") as f:
50
+ f.write(uploaded_file.read())
51
+
52
+ st.info("Extracting audio...")
53
+ audio_path = extract_audio(video_path)
54
 
55
+ st.info("Transcribing audio...")
56
+ transcript = transcribe_audio(audio_path)
57
+ st.text_area("Transcript", transcript, height=200)
 
 
 
 
 
 
58
 
59
+ st.info("Summarizing transcript...")
60
+ video_summary = summarize_text(transcript)
61
+ st.text_area("Summary", video_summary, height=150)
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ st.info("Generating study notes...")
64
+ study_notes = generate_study_notes(video_summary)
65
+ st.text_area("Study Notes", study_notes, height=150)
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ question = st.text_input("Ask a question about the video:")
68
+ if question:
69
+ answer = answer_question(question, video_summary)
70
+ st.write("Answer:", answer)