Spaces:
Running
Running
added video.py file
Browse files- src/video.py +115 -0
src/video.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Video processing module for Study Companion
|
| 3 |
+
Handles video upload, audio extraction, transcription, and chat functionality
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import tempfile
|
| 8 |
+
import os
|
| 9 |
+
from moviepy.editor import VideoFileClip
|
| 10 |
+
from openai import OpenAI
|
| 11 |
+
|
| 12 |
+
# Initialize OpenAI client
|
| 13 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
+
client = OpenAI(api_key=api_key)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# ---------------------------
|
| 18 |
+
# Core Video Processing Functions
|
| 19 |
+
# ---------------------------
|
| 20 |
+
|
| 21 |
+
def extract_audio(video_path: str) -> str:
|
| 22 |
+
"""Extract audio from the video file and save as MP3."""
|
| 23 |
+
try:
|
| 24 |
+
clip = VideoFileClip(video_path)
|
| 25 |
+
audio_path = video_path.replace(".mp4", ".mp3").replace(".mkv", ".mp3").replace(".webm", ".mp3").replace(".mov", ".mp3").replace(".avi", ".mp3")
|
| 26 |
+
clip.audio.write_audiofile(audio_path, codec='mp3', logger=None)
|
| 27 |
+
clip.close()
|
| 28 |
+
return audio_path
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Error extracting audio: {e}")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def transcribe_audio(audio_path: str) -> str:
|
| 35 |
+
"""Transcribe the audio to text using OpenAI's Whisper API."""
|
| 36 |
+
try:
|
| 37 |
+
with open(audio_path, "rb") as audio_file:
|
| 38 |
+
transcript = client.audio.transcriptions.create(
|
| 39 |
+
model="whisper-1",
|
| 40 |
+
file=audio_file
|
| 41 |
+
)
|
| 42 |
+
return transcript.text
|
| 43 |
+
except Exception as e:
|
| 44 |
+
st.error(f"Error transcribing audio: {e}")
|
| 45 |
+
return ""
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def generate_video_summary(transcript_text: str) -> str:
|
| 49 |
+
"""Generate a concise summary of the video transcript using OpenAI."""
|
| 50 |
+
prompt = (
|
| 51 |
+
f"Summarize the following video transcript in a concise manner, "
|
| 52 |
+
"highlighting the key points that a student should know.\n\n"
|
| 53 |
+
"Feel free to use bullet points, bold, italics and headers to emphasize key points where necessary.\n\n"
|
| 54 |
+
f"Transcript:\n\n{transcript_text}"
|
| 55 |
+
)
|
| 56 |
+
messages = [
|
| 57 |
+
{"role": "system", "content": "You are an educational assistant that creates clear, structured summaries."},
|
| 58 |
+
{"role": "user", "content": prompt}
|
| 59 |
+
]
|
| 60 |
+
completion = client.chat.completions.create(
|
| 61 |
+
model="gpt-4o-mini",
|
| 62 |
+
messages=messages
|
| 63 |
+
)
|
| 64 |
+
return completion.choices[0].message.content.strip()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def chat_with_video(transcript_text: str, conversation_history: list, user_query: str) -> str:
|
| 68 |
+
"""Generate a chat response using the video transcript as context."""
|
| 69 |
+
messages = conversation_history + [
|
| 70 |
+
{"role": "user", "content": f"Based on the following video transcript:\n\n{transcript_text}\n\nQuestion: {user_query}"}
|
| 71 |
+
]
|
| 72 |
+
completion = client.chat.completions.create(
|
| 73 |
+
model="gpt-4o-mini",
|
| 74 |
+
messages=messages
|
| 75 |
+
)
|
| 76 |
+
return completion.choices[0].message.content.strip()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def process_uploaded_video(uploaded_video) -> tuple:
|
| 80 |
+
"""
|
| 81 |
+
Process an uploaded video file: extract audio and transcribe.
|
| 82 |
+
Returns: (transcript_text, video_path) or (None, None) on error
|
| 83 |
+
"""
|
| 84 |
+
# Check file size (200MB limit)
|
| 85 |
+
if uploaded_video.size > 200 * 1024 * 1024:
|
| 86 |
+
st.error("File size exceeds 200MB. Please upload a smaller video.")
|
| 87 |
+
return None, None
|
| 88 |
+
|
| 89 |
+
# Save uploaded video to temporary file
|
| 90 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
| 91 |
+
tmp.write(uploaded_video.read())
|
| 92 |
+
video_path = tmp.name
|
| 93 |
+
|
| 94 |
+
# Extract audio
|
| 95 |
+
with st.spinner("🎵 Extracting audio from video..."):
|
| 96 |
+
audio_path = extract_audio(video_path)
|
| 97 |
+
|
| 98 |
+
if not audio_path:
|
| 99 |
+
return None, None
|
| 100 |
+
|
| 101 |
+
# Transcribe audio
|
| 102 |
+
with st.spinner("📝 Transcribing audio... This may take a few minutes."):
|
| 103 |
+
transcript_text = transcribe_audio(audio_path)
|
| 104 |
+
|
| 105 |
+
# Clean up audio file
|
| 106 |
+
try:
|
| 107 |
+
os.unlink(audio_path)
|
| 108 |
+
except:
|
| 109 |
+
pass
|
| 110 |
+
|
| 111 |
+
if not transcript_text:
|
| 112 |
+
st.error("Failed to transcribe audio.")
|
| 113 |
+
return None, None
|
| 114 |
+
|
| 115 |
+
return transcript_text, video_path
|