Spaces:
Runtime error
Runtime error
| # app.py | |
| import gradio as gr | |
| from moviepy.editor import VideoFileClip | |
| import whisper | |
| from transformers import pipeline | |
| import os | |
| # Function to extract audio from video | |
| 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) | |
| return audio_path | |
| # Function to transcribe audio | |
| def transcribe_audio(audio_path): | |
| model = whisper.load_model("base") | |
| result = model.transcribe(audio_path) | |
| return result["text"] | |
| # Function to summarize text | |
| def summarize_text(text): | |
| summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
| max_chunk_size = 1000 | |
| chunks = [text[i:i + max_chunk_size] for i in range(0, len(text), max_chunk_size)] | |
| summaries = [] | |
| for chunk in chunks: | |
| summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False) | |
| summaries.append(summary[0]["summary_text"]) | |
| return " ".join(summaries) | |
| # Function to generate study notes | |
| def generate_study_notes(summary): | |
| generator = pipeline("text-generation", model="gpt2") | |
| prompt = f"Create study notes from the following summary:\n{summary}" | |
| study_notes = generator( | |
| prompt, | |
| max_length=400, | |
| max_new_tokens=200, | |
| num_return_sequences=1, | |
| truncation=True | |
| ) | |
| return study_notes[0]["generated_text"] | |
| # Function to answer questions | |
| def answer_question(question, context): | |
| qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad") | |
| result = qa_pipeline(question=question, context=context) | |
| return result["answer"] | |
| # Main function to process video | |
| def process_video(video_file): | |
| # Step 1: Save the uploaded video | |
| video_path = "uploaded_video.mp4" | |
| with open(video_path, "wb") as f: | |
| f.write(video_file) | |
| # Step 2: Extract audio | |
| audio_path = extract_audio(video_path) | |
| # Step 3: Transcribe audio | |
| transcript = transcribe_audio(audio_path) | |
| # Step 4: Summarize transcript | |
| summary = summarize_text(transcript) | |
| # Step 5: Generate study notes | |
| study_notes = generate_study_notes(summary) | |
| return transcript, summary, study_notes | |
| # Gradio interface | |
| def gradio_interface(video_file, question): | |
| # Process the video | |
| transcript, summary, study_notes = process_video(video_file) | |
| # Answer the question | |
| if question.strip(): | |
| answer = answer_question(question, summary) | |
| else: | |
| answer = "Please enter a question." | |
| return transcript, summary, study_notes, answer | |
| # Create Gradio app | |
| iface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs=[ | |
| gr.Video(label="Upload Video"), | |
| gr.Textbox(label="Ask a Question (Optional)") | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Transcript"), | |
| gr.Textbox(label="Summary"), | |
| gr.Textbox(label="Study Notes"), | |
| gr.Textbox(label="Answer") | |
| ], | |
| title="Video Summarizer 🎥📝", | |
| description="Upload a video, and get a transcript, summary, study notes, and answers to your questions!" | |
| ) | |
| # Launch the app | |
| iface.launch() |