sairaarif89 commited on
Commit
33b628c
Β·
verified Β·
1 Parent(s): ff30508

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+ import whisper
5
+ from transformers import pipeline
6
+ from moviepy.editor import VideoFileClip
7
+
8
+ # Function to extract audio from a video file
9
+ def extract_audio(video_path, audio_path="audio.wav"):
10
+ if os.path.exists(audio_path):
11
+ os.remove(audio_path)
12
+ video = VideoFileClip(video_path)
13
+ video.audio.write_audiofile(audio_path, codec='pcm_s16le', bitrate='32k') # Lower bitrate for faster processing
14
+ return audio_path
15
+
16
+ # Function to transcribe audio using Whisper
17
+ def transcribe_audio(audio_path):
18
+ try:
19
+ model = whisper.load_model("tiny") # Faster model
20
+ result = model.transcribe(audio_path)
21
+ return result["text"]
22
+ except Exception as e:
23
+ return f"Error in transcription: {str(e)}"
24
+
25
+ # Function to summarize text using a pre-trained transformer model
26
+ def summarize_text(text):
27
+ try:
28
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
29
+ max_chunk_size = 300 # Reduced chunk size for faster processing
30
+ chunks = [text[i:i + max_chunk_size] for i in range(0, len(text), max_chunk_size)]
31
+ summaries = [summarizer(chunk, max_length=80, min_length=20, do_sample=False)[0]["summary_text"] for chunk in chunks]
32
+ return " ".join(summaries)
33
+ except Exception as e:
34
+ return f"Error in summarization: {str(e)}"
35
+
36
+ # Function to generate study notes using GPT-2
37
+ def generate_study_notes(summary):
38
+ try:
39
+ generator = pipeline("text-generation", model="gpt2")
40
+ prompt = f"Create concise study notes from this summary:\n{summary}"
41
+ study_notes = generator(prompt, max_length=150, num_return_sequences=1, truncation=True)
42
+ return study_notes[0]["generated_text"]
43
+ except Exception as e:
44
+ return f"Error in generating study notes: {str(e)}"
45
+
46
+ # Function to answer questions using a QA model
47
+ def answer_question(question, context):
48
+ try:
49
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
50
+ result = qa_pipeline(question=question, context=context)
51
+ return result["answer"]
52
+ except Exception as e:
53
+ return f"Error in answering question: {str(e)}"
54
+
55
+ # Gradio UI
56
+ def process_video(video_file):
57
+ video_path = video_file # Directly using filepath
58
+ audio_path = extract_audio(video_path)
59
+ transcript = transcribe_audio(audio_path)
60
+ video_summary = summarize_text(transcript)
61
+ study_notes = generate_study_notes(video_summary)
62
+ return transcript, video_summary, study_notes
63
+
64
+ def ask_question(video_summary, question):
65
+ return answer_question(question, video_summary)
66
+
67
+ iface = gr.Blocks()
68
+ with iface:
69
+ gr.Markdown("# πŸŽ₯ Video Summarizer & Study Notes Generator")
70
+ with gr.Row():
71
+ video_input = gr.File(label="πŸ“‚ Upload a video file", type="filepath")
72
+ transcript_output = gr.Textbox(label="πŸ“œ Transcript", lines=5)
73
+ summary_output = gr.Textbox(label="πŸ“„ Video Summary", lines=3)
74
+ notes_output = gr.Textbox(label="πŸ“ Study Notes", lines=3)
75
+
76
+ process_button = gr.Button("Process Video")
77
+ process_button.click(process_video, inputs=video_input, outputs=[transcript_output, summary_output, notes_output])
78
+
79
+ question_input = gr.Textbox(label="❓ Ask a question about the video:")
80
+ answer_output = gr.Textbox(label="πŸ’‘ Answer")
81
+
82
+ ask_button = gr.Button("Get Answer")
83
+ ask_button.click(ask_question, inputs=[summary_output, question_input], outputs=answer_output)
84
+
85
+ iface.launch()