Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,28 @@
|
|
1 |
-
import streamlit as st
|
2 |
import os
|
|
|
3 |
import moviepy.editor as mp
|
4 |
import whisper
|
5 |
from transformers import pipeline
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
8 |
def extract_audio(video_path, audio_path="audio.wav"):
|
9 |
if os.path.exists(audio_path):
|
10 |
os.remove(audio_path)
|
11 |
-
video = mp.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
|
@@ -26,22 +30,23 @@ def summarize_text(text):
|
|
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
|
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:
|
@@ -51,22 +56,27 @@ if uploaded_file:
|
|
51 |
|
52 |
st.success("β
Video uploaded successfully!")
|
53 |
|
|
|
54 |
st.info("π Extracting audio...")
|
55 |
audio_path = extract_audio(video_path)
|
56 |
st.success("β
Audio extracted!")
|
57 |
|
|
|
58 |
st.info("ποΈ Transcribing audio...")
|
59 |
transcript = transcribe_audio(audio_path)
|
60 |
st.text_area("π Transcript", transcript, height=200)
|
61 |
|
|
|
62 |
st.info("π Summarizing transcript...")
|
63 |
video_summary = summarize_text(transcript)
|
64 |
st.text_area("π Summary", video_summary, height=150)
|
65 |
|
|
|
66 |
st.info("π Generating study notes...")
|
67 |
study_notes = generate_study_notes(video_summary)
|
68 |
st.text_area("π Study Notes", study_notes, height=150)
|
69 |
|
|
|
70 |
question = st.text_input("β Ask a question about the video:")
|
71 |
if question:
|
72 |
answer = answer_question(question, video_summary)
|
|
|
|
|
1 |
import os
|
2 |
+
import streamlit as st
|
3 |
import moviepy.editor as mp
|
4 |
import whisper
|
5 |
from transformers import pipeline
|
6 |
|
7 |
+
# β
Ensure ffmpeg is installed (needed for moviepy)
|
8 |
+
if not os.path.exists("/usr/bin/ffmpeg"):
|
9 |
+
os.system("apt-get update && apt-get install -y ffmpeg")
|
10 |
+
|
11 |
+
# β
Function to extract audio from a video
|
12 |
def extract_audio(video_path, audio_path="audio.wav"):
|
13 |
if os.path.exists(audio_path):
|
14 |
os.remove(audio_path)
|
15 |
+
video = mp.VideoFileClip(video_path) # Use mp.VideoFileClip
|
16 |
video.audio.write_audiofile(audio_path)
|
17 |
return audio_path
|
18 |
|
19 |
+
# β
Function to transcribe audio using Whisper
|
20 |
def transcribe_audio(audio_path):
|
21 |
+
model = whisper.load_model("base", download_root="./models") # Ensure model is downloaded
|
22 |
result = model.transcribe(audio_path)
|
23 |
return result["text"]
|
24 |
|
25 |
+
# β
Function to summarize text
|
26 |
def summarize_text(text):
|
27 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
28 |
max_chunk_size = 1000
|
|
|
30 |
summaries = [summarizer(chunk, max_length=130, min_length=30, do_sample=False)[0]["summary_text"] for chunk in chunks]
|
31 |
return " ".join(summaries)
|
32 |
|
33 |
+
# β
Function to generate study notes
|
34 |
def generate_study_notes(summary):
|
35 |
generator = pipeline("text-generation", model="gpt2")
|
36 |
prompt = f"Create study notes from the following summary:\n{summary}"
|
37 |
study_notes = generator(prompt, max_length=400, max_new_tokens=200, num_return_sequences=1, truncation=True)
|
38 |
return study_notes[0]["generated_text"]
|
39 |
|
40 |
+
# β
Function to answer user questions
|
41 |
def answer_question(question, context):
|
42 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
43 |
result = qa_pipeline(question=question, context=context)
|
44 |
return result["answer"]
|
45 |
|
46 |
+
# β
Streamlit UI
|
47 |
st.title("Lecture Video Processor π₯π")
|
48 |
|
49 |
+
# File uploader
|
50 |
uploaded_file = st.file_uploader("π€ Upload a video file", type=["mp4", "mov", "avi", "mkv"])
|
51 |
|
52 |
if uploaded_file:
|
|
|
56 |
|
57 |
st.success("β
Video uploaded successfully!")
|
58 |
|
59 |
+
# Extract audio
|
60 |
st.info("π Extracting audio...")
|
61 |
audio_path = extract_audio(video_path)
|
62 |
st.success("β
Audio extracted!")
|
63 |
|
64 |
+
# Transcribe audio
|
65 |
st.info("ποΈ Transcribing audio...")
|
66 |
transcript = transcribe_audio(audio_path)
|
67 |
st.text_area("π Transcript", transcript, height=200)
|
68 |
|
69 |
+
# Summarize transcript
|
70 |
st.info("π Summarizing transcript...")
|
71 |
video_summary = summarize_text(transcript)
|
72 |
st.text_area("π Summary", video_summary, height=150)
|
73 |
|
74 |
+
# Generate study notes
|
75 |
st.info("π Generating study notes...")
|
76 |
study_notes = generate_study_notes(video_summary)
|
77 |
st.text_area("π Study Notes", study_notes, height=150)
|
78 |
|
79 |
+
# Q&A Section
|
80 |
question = st.text_input("β Ask a question about the video:")
|
81 |
if question:
|
82 |
answer = answer_question(question, video_summary)
|