Spaces:
Build error
Build error
Commit ·
2cb3d80
1
Parent(s): d8fa3f8
Initial Commit
Browse files- app.py +141 -0
- requiements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import logging
|
| 5 |
+
from moviepy.video.io.VideoFileClip import VideoFileClip
|
| 6 |
+
import whisper
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
import cv2
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
# Set up logging configuration
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
+
logger = logging.getLogger()
|
| 14 |
+
|
| 15 |
+
# Set up directories
|
| 16 |
+
UPLOAD_FOLDER = './uploads'
|
| 17 |
+
PROCESSED_FOLDER = './processed'
|
| 18 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 19 |
+
os.makedirs(PROCESSED_FOLDER, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
# Initialize the summarizer model from Hugging Face
|
| 22 |
+
summarizer = pipeline("summarization", model="meta-llama/Llama-3.2-1B")
|
| 23 |
+
|
| 24 |
+
# Function to extract audio from video using MoviePy
|
| 25 |
+
def extract_audio_from_video(video_file_path, audio_file_path):
|
| 26 |
+
try:
|
| 27 |
+
logger.info(f"Extracting audio from video: {video_file_path}")
|
| 28 |
+
video_clip = VideoFileClip(video_file_path)
|
| 29 |
+
audio_clip = video_clip.audio
|
| 30 |
+
audio_clip.write_audiofile(audio_file_path)
|
| 31 |
+
logger.info(f"Audio extraction completed and saved to: {audio_file_path}")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
logger.error(f"Error extracting audio from video: {e}")
|
| 34 |
+
|
| 35 |
+
# Function to transcribe audio using Whisper
|
| 36 |
+
def transcribe_audio(audio_file_path):
|
| 37 |
+
try:
|
| 38 |
+
logger.info(f"Transcribing audio: {audio_file_path}")
|
| 39 |
+
model = whisper.load_model("base") # Load the Whisper model
|
| 40 |
+
result = model.transcribe(audio_file_path)
|
| 41 |
+
logger.info("Audio transcription completed.")
|
| 42 |
+
return result['text']
|
| 43 |
+
except Exception as e:
|
| 44 |
+
logger.error(f"Error transcribing audio: {e}")
|
| 45 |
+
return ""
|
| 46 |
+
|
| 47 |
+
# Function to summarize the transcription text using Hugging Face Transformers
|
| 48 |
+
def split_text_into_chunks(text, chunk_size=1000):
|
| 49 |
+
chunks = []
|
| 50 |
+
for i in range(0, len(text), chunk_size):
|
| 51 |
+
chunks.append(text[i:i+chunk_size])
|
| 52 |
+
return chunks
|
| 53 |
+
|
| 54 |
+
def summarize_text(text):
|
| 55 |
+
try:
|
| 56 |
+
chunks = split_text_into_chunks(text)
|
| 57 |
+
summaries = []
|
| 58 |
+
for chunk in chunks:
|
| 59 |
+
summary = summarizer(chunk, max_new_tokens=500, min_length=50, do_sample=False)
|
| 60 |
+
summaries.append(summary[0]['summary_text'])
|
| 61 |
+
return " ".join(summaries)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"Error summarizing text: {e}")
|
| 64 |
+
return "Error occurred during summarization."
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# Function to detect scene changes in the video using OpenCV (optional)
|
| 68 |
+
def detect_scene_changes(video_file_path):
|
| 69 |
+
try:
|
| 70 |
+
logger.info(f"Detecting scene changes in video: {video_file_path}")
|
| 71 |
+
cap = cv2.VideoCapture(video_file_path)
|
| 72 |
+
ret, prev_frame = cap.read()
|
| 73 |
+
prev_frame_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
|
| 74 |
+
|
| 75 |
+
scene_changes = []
|
| 76 |
+
frame_count = 0
|
| 77 |
+
|
| 78 |
+
while ret:
|
| 79 |
+
ret, frame = cap.read()
|
| 80 |
+
if not ret:
|
| 81 |
+
break
|
| 82 |
+
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 83 |
+
diff = cv2.absdiff(frame_gray, prev_frame_gray)
|
| 84 |
+
non_zero_count = np.count_nonzero(diff)
|
| 85 |
+
|
| 86 |
+
if non_zero_count > 1000000: # Threshold for detecting scene change
|
| 87 |
+
scene_changes.append(frame_count)
|
| 88 |
+
|
| 89 |
+
prev_frame_gray = frame_gray
|
| 90 |
+
frame_count += 1
|
| 91 |
+
|
| 92 |
+
cap.release()
|
| 93 |
+
logger.info(f"Scene changes detected: {scene_changes}")
|
| 94 |
+
return scene_changes
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.error(f"Error detecting scene changes: {e}")
|
| 97 |
+
return []
|
| 98 |
+
|
| 99 |
+
# Streamlit App
|
| 100 |
+
st.title("Automated Video Summarization")
|
| 101 |
+
|
| 102 |
+
# File upload widget
|
| 103 |
+
uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
|
| 104 |
+
|
| 105 |
+
if uploaded_file is not None:
|
| 106 |
+
try:
|
| 107 |
+
# Save the uploaded file to the upload folder with a timestamp
|
| 108 |
+
timestamp = int(time.time())
|
| 109 |
+
filename = f"{timestamp}_{uploaded_file.name}"
|
| 110 |
+
video_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 111 |
+
|
| 112 |
+
# Save the uploaded file
|
| 113 |
+
with open(video_path, "wb") as f:
|
| 114 |
+
f.write(uploaded_file.getbuffer())
|
| 115 |
+
|
| 116 |
+
logger.info(f"File uploaded successfully: {filename}")
|
| 117 |
+
st.write(f"File uploaded successfully: {filename}")
|
| 118 |
+
|
| 119 |
+
# Extract audio from the video
|
| 120 |
+
audio_file_path = os.path.join(PROCESSED_FOLDER, f"{filename}.wav")
|
| 121 |
+
extract_audio_from_video(video_path, audio_file_path)
|
| 122 |
+
st.write("Audio extracted successfully.")
|
| 123 |
+
|
| 124 |
+
# Transcribe the audio
|
| 125 |
+
transcription = transcribe_audio(audio_file_path)
|
| 126 |
+
st.write("Transcription completed.")
|
| 127 |
+
|
| 128 |
+
# Add a spinner while the summarization is happening
|
| 129 |
+
with st.spinner('Generating summary, please wait...'):
|
| 130 |
+
summary = summarize_text(transcription)
|
| 131 |
+
st.subheader("Summary")
|
| 132 |
+
st.write(summary)
|
| 133 |
+
|
| 134 |
+
# Optional: Detect scene changes in the video
|
| 135 |
+
scene_changes = detect_scene_changes(video_path)
|
| 136 |
+
st.subheader("Scene Changes Detected at Frames")
|
| 137 |
+
st.write(scene_changes)
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
logger.error(f"An error occurred: {e}")
|
| 141 |
+
st.error(f"An error occurred: {e}")
|
requiements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.25.0
|
| 2 |
+
moviepy==1.0.3
|
| 3 |
+
transformers==4.33.0
|
| 4 |
+
torch==2.0.1
|
| 5 |
+
whisper==1.0
|
| 6 |
+
opencv-python-headless==4.8.0
|
| 7 |
+
numpy==1.26.0
|