Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| import torch | |
| import whisper | |
| from transformers import pipeline | |
| from moviepy.editor import VideoFileClip | |
| # Function to extract audio from a video file | |
| def extract_audio(video_path, audio_path="audio.wav"): | |
| if os.path.exists(audio_path): | |
| os.remove(audio_path) | |
| video = VideoFileClip(video_path) | |
| video.audio.write_audiofile(audio_path, codec='pcm_s16le', bitrate='32k') # Lower bitrate for faster processing | |
| return audio_path | |
| # Function to transcribe audio using Whisper | |
| def transcribe_audio(audio_path): | |
| try: | |
| model = whisper.load_model("tiny") # Faster model | |
| result = model.transcribe(audio_path) | |
| return result["text"] | |
| except Exception as e: | |
| return f"Error in transcription: {str(e)}" | |
| # Function to summarize text using a pre-trained transformer model | |
| def summarize_text(text): | |
| try: | |
| summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
| max_chunk_size = 300 # Reduced chunk size for faster processing | |
| chunks = [text[i:i + max_chunk_size] for i in range(0, len(text), max_chunk_size)] | |
| summaries = [summarizer(chunk, max_length=80, min_length=20, do_sample=False)[0]["summary_text"] for chunk in chunks] | |
| return " ".join(summaries) | |
| except Exception as e: | |
| return f"Error in summarization: {str(e)}" | |
| # Function to generate study notes using GPT-2 | |
| def generate_study_notes(summary): | |
| try: | |
| generator = pipeline("text-generation", model="gpt2") | |
| prompt = f"Create concise study notes from this summary:\n{summary}" | |
| study_notes = generator(prompt, max_length=150, num_return_sequences=1, truncation=True) | |
| return study_notes[0]["generated_text"] | |
| except Exception as e: | |
| return f"Error in generating study notes: {str(e)}" | |
| # Function to answer questions using a QA model | |
| def answer_question(question, context): | |
| try: | |
| qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad") | |
| result = qa_pipeline(question=question, context=context) | |
| return result["answer"] | |
| except Exception as e: | |
| return f"Error in answering question: {str(e)}" | |
| # Gradio UI | |
| def process_video(video_file): | |
| video_path = video_file # Directly using filepath | |
| audio_path = extract_audio(video_path) | |
| transcript = transcribe_audio(audio_path) | |
| video_summary = summarize_text(transcript) | |
| study_notes = generate_study_notes(video_summary) | |
| return transcript, video_summary, study_notes | |
| def ask_question(video_summary, question): | |
| return answer_question(question, video_summary) | |
| iface = gr.Blocks() | |
| with iface: | |
| gr.Markdown("# π₯ Video Summarizer & Study Notes Generator") | |
| with gr.Row(): | |
| video_input = gr.File(label="π Upload a video file", type="filepath") | |
| transcript_output = gr.Textbox(label="π Transcript", lines=5) | |
| summary_output = gr.Textbox(label="π Video Summary", lines=3) | |
| notes_output = gr.Textbox(label="π Study Notes", lines=3) | |
| process_button = gr.Button("Process Video") | |
| process_button.click(process_video, inputs=video_input, outputs=[transcript_output, summary_output, notes_output]) | |
| question_input = gr.Textbox(label="β Ask a question about the video:") | |
| answer_output = gr.Textbox(label="π‘ Answer") | |
| ask_button = gr.Button("Get Answer") | |
| ask_button.click(ask_question, inputs=[summary_output, question_input], outputs=answer_output) | |
| iface.launch() |