File size: 6,671 Bytes
b93e112
 
 
 
 
eecfdbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b93e112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eecfdbe
 
 
 
 
 
 
 
 
b93e112
eecfdbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b93e112
eecfdbe
 
 
 
 
 
 
 
 
 
 
 
 
b93e112
eecfdbe
 
 
 
 
 
b93e112
eecfdbe
 
 
 
 
 
 
 
 
b93e112
eecfdbe
 
 
 
 
b93e112
 
 
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
190
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 re

# Initialize Gemini API (replace with your actual API key)
# genai.configure(api_key="YOUR_API_KEY_HERE")

# Initialize the model
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)
    
    # Extracting summary and questions
    summary_match = re.search(r'Summary:(.*?)Quiz Questions:', result, re.DOTALL)
    summary = summary_match.group(1).strip() if summary_match else "Summary not found."
    
    questions_match = re.findall(r'(\d+\.\s.*?)(?=\d+\.|$)', result.split('Quiz Questions:')[1], re.DOTALL)
    questions = [q.strip() for q in questions_match]
    
    return summary, questions

def create_quiz_interface(questions):
    quiz_elements = []
    for i, question in enumerate(questions):
        q_parts = question.split('\n')
        q_text = q_parts[0].split('.', 1)[1].strip()
        options = [opt.strip() for opt in q_parts[1:5]]
        
        quiz_elements.extend([
            gr.Markdown(f"**Question {i+1}:** {q_text}"),
            gr.Radio(options, label=f"Options for Question {i+1}")
        ])
    
    return quiz_elements

def check_answers(questions, *user_answers):
    correct_answers = []
    user_results = []
    
    for question, user_answer in zip(questions, user_answers):
        correct_answer = re.search(r'Correct Answer: (\w)', question).group(1)
        correct_answers.append(correct_answer)
        
        options = [opt.strip() for opt in question.split('\n')[1:5]]
        user_choice = chr(ord('a') + options.index(user_answer)) if user_answer in options else 'No answer'
        
        is_correct = user_choice == correct_answer
        user_results.append(f"Your answer: {user_choice}, Correct answer: {correct_answer}, {'Correct!' if is_correct else 'Incorrect'}")
    
    return "\n".join(user_results)

# Create the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Interactive Video Lecture Assistant")
    
    with gr.Tab("Generate Summary and Quiz"):
        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")
        summary_output = gr.Textbox(label="Summary", lines=5)
        quiz_output = gr.Textbox(label="Quiz Questions", lines=15, visible=False)
    
    with gr.Tab("Take Quiz"):
        quiz_interface = gr.Column()
        submit_quiz_btn = gr.Button("Submit Quiz")
        quiz_results = gr.Textbox(label="Quiz Results", lines=5)
    
    def update_quiz_interface(questions):
        quiz_interface.clear()
        elements = create_quiz_interface(questions)
        for element in elements:
            quiz_interface.append(element)
        return {quiz_interface: gr.update(visible=True)}
    
    generate_btn.click(
        process_lecture, 
        inputs=[transcript_input, num_questions], 
        outputs=[summary_output, quiz_output]
    ).then(
        update_quiz_interface,
        inputs=[quiz_output],
        outputs=[quiz_interface]
    )
    
    submit_quiz_btn.click(
        check_answers,
        inputs=[quiz_output] + [child for child in quiz_interface.children if isinstance(child, gr.components.Radio)],
        outputs=[quiz_results]
    )

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