File size: 3,530 Bytes
33b628c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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()