File size: 6,261 Bytes
72c31b0
 
eae382b
72c31b0
eae382b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72c31b0
 
 
 
eae382b
72c31b0
 
eae382b
72c31b0
 
eecfdbe
72c31b0
 
 
 
 
 
eecfdbe
72c31b0
 
 
a1e5f87
 
72c31b0
 
a1e5f87
72c31b0
a1e5f87
72c31b0
 
a1e5f87
72c31b0
 
 
 
 
a1e5f87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72c31b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eecfdbe
72c31b0
 
 
eecfdbe
a1e5f87
72c31b0
 
 
 
 
 
 
 
eecfdbe
72c31b0
 
b93e112
72c31b0
b93e112
72c31b0
 
b93e112
72c31b0
 
 
 
 
b93e112
72c31b0
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# import gradio as gr
# import google.generativeai as genai

# genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")

# model = genai.GenerativeModel('gemini-pro')

# def generate_summary_and_quiz(transcript, num_questions):
#     """Generate a summary and quiz questions based on the video transcript."""
#     prompt = f"""
#     Based on the following video lecture transcript, please provide:
#     1. A concise summary of the main points (about 100 words)
#     2. {num_questions} multiple-choice quiz questions to test understanding of key concepts

#     Transcript:
#     {transcript}

#     Format your response as follows:
#     Summary:
#     [Your summary here]

#     Quiz Questions:
#     1. [Question]
#     a) [Option A]
#     b) [Option B]
#     c) [Option C]
#     d) [Option D]
#     Correct Answer: [Correct option letter]

#     2. [Next question and options...]

#     Ensure the questions cover different aspects of the lecture and vary in difficulty.
#     """

#     try:
#         response = model.generate_content(prompt)
#         return response.text
#     except Exception as e:
#         return f"Error generating summary and quiz: {str(e)}"

# def process_lecture(transcript, num_questions):
#     with gr.Row():
#         gr.Markdown("Generating summary and quiz...")
#     result = generate_summary_and_quiz(transcript, num_questions)
#     return result

# with gr.Blocks() as demo:
#     gr.Markdown("# Video Lecture Summarizer and Quiz Generator")
    
#     transcript_input = gr.Textbox(label="Video Lecture Transcript", lines=10, placeholder="Paste the video transcript or a detailed description of the lecture content here...")
#     num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
    
#     generate_btn = gr.Button("Generate Summary and Quiz")
#     output = gr.Textbox(label="Summary and Quiz", lines=20)
    
#     generate_btn.click(process_lecture, inputs=[transcript_input, num_questions], outputs=output)

# if __name__ == "__main__":
#     demo.launch()




import gradio as gr
import google.generativeai as genai
import speech_recognition as sr
from pydub import AudioSegment
import os
import tempfile
import moviepy.editor as mp

# Initialize Gemini API
genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")

# Initialize the Gemini model
model = genai.GenerativeModel('gemini-pro')

def transcribe_video(video_path):
    """Transcribe the audio from a video file."""
    try:
        # Extract audio from video
        video = mp.VideoFileClip(video_path)
        audio_path = tempfile.mktemp(suffix=".wav")
        video.audio.write_audiofile(audio_path, codec='pcm_s16le')
        
        # Load audio file
        audio = AudioSegment.from_wav(audio_path)
        
        # Initialize recognizer
        r = sr.Recognizer()
        
        # Split audio into chunks to handle long audio
        chunk_length_ms = 30000  # 30 seconds
        chunks = [audio[i:i+chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
        
        transcript = ""
        for i, chunk in enumerate(chunks):
            # Export chunk to a temporary file
            chunk_path = tempfile.mktemp(suffix=".wav")
            chunk.export(chunk_path, format="wav")
            
            # Recognize speech in the chunk
            with sr.AudioFile(chunk_path) as source:
                audio_listened = r.record(source)
                try:
                    text = r.recognize_google(audio_listened)
                    transcript += text + " "
                except sr.UnknownValueError:
                    print(f"Could not understand audio in chunk {i+1}")
                except sr.RequestError:
                    print(f"Could not request results from Google Speech Recognition service for chunk {i+1}")
            
            # Clean up temporary chunk file
            os.remove(chunk_path)
        
        # Clean up temporary audio file
        os.remove(audio_path)
        
        return transcript.strip()
    except Exception as e:
        return f"Error transcribing video: {str(e)}"

def generate_summary_and_quiz(transcript, num_questions):
    """Generate a summary and quiz questions based on the video transcript."""
    prompt = f"""
    Based on the following video lecture transcript, please provide:
    1. A concise summary of the main points (about 100 words)
    2. {num_questions} multiple-choice quiz questions to test understanding of key concepts

    Transcript:
    {transcript}

    Format your response as follows:
    Summary:
    [Your summary here]

    Quiz Questions:
    1. [Question]
    a) [Option A]
    b) [Option B]
    c) [Option C]
    d) [Option D]
    Correct Answer: [Correct option letter]

    2. [Next question and options...]

    Ensure the questions cover different aspects of the lecture and vary in difficulty.
    """

    try:
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error generating summary and quiz: {str(e)}"

def process_video(video, num_questions):
    with gr.Row():
        gr.Markdown("Processing video and generating summary and quiz...")
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
        video.save(temp_video.name)
        video_path = temp_video.name

    transcript = transcribe_video(video_path)
    result = generate_summary_and_quiz(transcript, num_questions)

    os.unlink(video_path)

    return transcript, result

with gr.Blocks() as demo:
    gr.Markdown("# Video Lecture Summarizer and Quiz Generator")
    
    video_input = gr.Video(label="Upload Video Lecture")
    num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
    
    generate_btn = gr.Button("Process Video and Generate Summary and Quiz")
    
    transcript_output = gr.Textbox(label="Video Transcript", lines=10)
    summary_quiz_output = gr.Textbox(label="Summary and Quiz", lines=20)
    
    generate_btn.click(
        process_video, 
        inputs=[video_input, num_questions], 
        outputs=[transcript_output, summary_quiz_output]
    )

if __name__ == "__main__":
    demo.launch()