Spaces:
Build error
Build error
| import streamlit as st | |
| import os | |
| import time | |
| import logging | |
| from moviepy.video.io.VideoFileClip import VideoFileClip | |
| import whisper | |
| from transformers import pipeline | |
| import cv2 | |
| import numpy as np | |
| # Set up logging configuration | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| logger = logging.getLogger() | |
| # Set up directories | |
| UPLOAD_FOLDER = './uploads' | |
| PROCESSED_FOLDER = './processed' | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| os.makedirs(PROCESSED_FOLDER, exist_ok=True) | |
| # Initialize the summarizer model from Hugging Face | |
| summarizer = pipeline("summarization", model="meta-llama/Llama-3.2-1B") | |
| # Function to extract audio from video using MoviePy | |
| def extract_audio_from_video(video_file_path, audio_file_path): | |
| try: | |
| logger.info(f"Extracting audio from video: {video_file_path}") | |
| video_clip = VideoFileClip(video_file_path) | |
| audio_clip = video_clip.audio | |
| audio_clip.write_audiofile(audio_file_path) | |
| logger.info(f"Audio extraction completed and saved to: {audio_file_path}") | |
| except Exception as e: | |
| logger.error(f"Error extracting audio from video: {e}") | |
| # Function to transcribe audio using Whisper | |
| def transcribe_audio(audio_file_path): | |
| try: | |
| logger.info(f"Transcribing audio: {audio_file_path}") | |
| model = whisper.load_model("base") # Load the Whisper model | |
| result = model.transcribe(audio_file_path) | |
| logger.info("Audio transcription completed.") | |
| return result['text'] | |
| except Exception as e: | |
| logger.error(f"Error transcribing audio: {e}") | |
| return "" | |
| # Function to summarize the transcription text using Hugging Face Transformers | |
| def split_text_into_chunks(text, chunk_size=1000): | |
| chunks = [] | |
| for i in range(0, len(text), chunk_size): | |
| chunks.append(text[i:i+chunk_size]) | |
| return chunks | |
| def summarize_text(text): | |
| try: | |
| chunks = split_text_into_chunks(text) | |
| summaries = [] | |
| for chunk in chunks: | |
| summary = summarizer(chunk, max_new_tokens=500, min_length=50, do_sample=False) | |
| summaries.append(summary[0]['summary_text']) | |
| return " ".join(summaries) | |
| except Exception as e: | |
| st.error(f"Error summarizing text: {e}") | |
| return "Error occurred during summarization." | |
| # Function to detect scene changes in the video using OpenCV (optional) | |
| def detect_scene_changes(video_file_path): | |
| try: | |
| logger.info(f"Detecting scene changes in video: {video_file_path}") | |
| cap = cv2.VideoCapture(video_file_path) | |
| ret, prev_frame = cap.read() | |
| prev_frame_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) | |
| scene_changes = [] | |
| frame_count = 0 | |
| while ret: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| diff = cv2.absdiff(frame_gray, prev_frame_gray) | |
| non_zero_count = np.count_nonzero(diff) | |
| if non_zero_count > 1000000: # Threshold for detecting scene change | |
| scene_changes.append(frame_count) | |
| prev_frame_gray = frame_gray | |
| frame_count += 1 | |
| cap.release() | |
| logger.info(f"Scene changes detected: {scene_changes}") | |
| return scene_changes | |
| except Exception as e: | |
| logger.error(f"Error detecting scene changes: {e}") | |
| return [] | |
| # Streamlit App | |
| st.title("Automated Video Summarization") | |
| # File upload widget | |
| uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"]) | |
| if uploaded_file is not None: | |
| try: | |
| # Save the uploaded file to the upload folder with a timestamp | |
| timestamp = int(time.time()) | |
| filename = f"{timestamp}_{uploaded_file.name}" | |
| video_path = os.path.join(UPLOAD_FOLDER, filename) | |
| # Save the uploaded file | |
| with open(video_path, "wb") as f: | |
| f.write(uploaded_file.getbuffer()) | |
| logger.info(f"File uploaded successfully: {filename}") | |
| st.write(f"File uploaded successfully: {filename}") | |
| # Extract audio from the video | |
| audio_file_path = os.path.join(PROCESSED_FOLDER, f"{filename}.wav") | |
| extract_audio_from_video(video_path, audio_file_path) | |
| st.write("Audio extracted successfully.") | |
| # Transcribe the audio | |
| transcription = transcribe_audio(audio_file_path) | |
| st.write("Transcription completed.") | |
| # Add a spinner while the summarization is happening | |
| with st.spinner('Generating summary, please wait...'): | |
| summary = summarize_text(transcription) | |
| st.subheader("Summary") | |
| st.write(summary) | |
| # Optional: Detect scene changes in the video | |
| scene_changes = detect_scene_changes(video_path) | |
| st.subheader("Scene Changes Detected at Frames") | |
| st.write(scene_changes) | |
| except Exception as e: | |
| logger.error(f"An error occurred: {e}") | |
| st.error(f"An error occurred: {e}") | |