arjunanand13 commited on
Commit
eae382b
·
verified ·
1 Parent(s): eecfdbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -129
app.py CHANGED
@@ -3,71 +3,6 @@ import google.generativeai as genai
3
 
4
  genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")
5
 
6
- # model = genai.GenerativeModel('gemini-pro')
7
-
8
- # def generate_summary_and_quiz(transcript, num_questions):
9
- # """Generate a summary and quiz questions based on the video transcript."""
10
- # prompt = f"""
11
- # Based on the following video lecture transcript, please provide:
12
- # 1. A concise summary of the main points (about 100 words)
13
- # 2. {num_questions} multiple-choice quiz questions to test understanding of key concepts
14
-
15
- # Transcript:
16
- # {transcript}
17
-
18
- # Format your response as follows:
19
- # Summary:
20
- # [Your summary here]
21
-
22
- # Quiz Questions:
23
- # 1. [Question]
24
- # a) [Option A]
25
- # b) [Option B]
26
- # c) [Option C]
27
- # d) [Option D]
28
- # Correct Answer: [Correct option letter]
29
-
30
- # 2. [Next question and options...]
31
-
32
- # Ensure the questions cover different aspects of the lecture and vary in difficulty.
33
- # """
34
-
35
- # try:
36
- # response = model.generate_content(prompt)
37
- # return response.text
38
- # except Exception as e:
39
- # return f"Error generating summary and quiz: {str(e)}"
40
-
41
- # def process_lecture(transcript, num_questions):
42
- # with gr.Row():
43
- # gr.Markdown("Generating summary and quiz...")
44
- # result = generate_summary_and_quiz(transcript, num_questions)
45
- # return result
46
-
47
- # with gr.Blocks() as demo:
48
- # gr.Markdown("# Video Lecture Summarizer and Quiz Generator")
49
-
50
- # transcript_input = gr.Textbox(label="Video Lecture Transcript", lines=10, placeholder="Paste the video transcript or a detailed description of the lecture content here...")
51
- # num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
52
-
53
- # generate_btn = gr.Button("Generate Summary and Quiz")
54
- # output = gr.Textbox(label="Summary and Quiz", lines=20)
55
-
56
- # generate_btn.click(process_lecture, inputs=[transcript_input, num_questions], outputs=output)
57
-
58
- # if __name__ == "__main__":
59
- # demo.launch()
60
-
61
-
62
-
63
- import gradio as gr
64
- import google.generativeai as genai
65
- import re
66
-
67
- # Initialize Gemini API (replace with your actual API key)
68
- # genai.configure(api_key="YOUR_API_KEY_HERE")
69
-
70
- # Initialize the model
71
  model = genai.GenerativeModel('gemini-pro')
72
 
73
  def generate_summary_and_quiz(transcript, num_questions):
@@ -107,84 +42,142 @@ def process_lecture(transcript, num_questions):
107
  with gr.Row():
108
  gr.Markdown("Generating summary and quiz...")
109
  result = generate_summary_and_quiz(transcript, num_questions)
 
 
 
 
110
 
111
- # Extracting summary and questions
112
- summary_match = re.search(r'Summary:(.*?)Quiz Questions:', result, re.DOTALL)
113
- summary = summary_match.group(1).strip() if summary_match else "Summary not found."
114
 
115
- questions_match = re.findall(r'(\d+\.\s.*?)(?=\d+\.|$)', result.split('Quiz Questions:')[1], re.DOTALL)
116
- questions = [q.strip() for q in questions_match]
117
 
118
- return summary, questions
119
-
120
- def create_quiz_interface(questions):
121
- quiz_elements = []
122
- for i, question in enumerate(questions):
123
- q_parts = question.split('\n')
124
- q_text = q_parts[0].split('.', 1)[1].strip()
125
- options = [opt.strip() for opt in q_parts[1:5]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
- quiz_elements.extend([
128
- gr.Markdown(f"**Question {i+1}:** {q_text}"),
129
- gr.Radio(options, label=f"Options for Question {i+1}")
130
- ])
131
 
132
- return quiz_elements
133
 
134
- def check_answers(questions, *user_answers):
135
- correct_answers = []
136
- user_results = []
137
 
138
- for question, user_answer in zip(questions, user_answers):
139
- correct_answer = re.search(r'Correct Answer: (\w)', question).group(1)
140
- correct_answers.append(correct_answer)
141
 
142
- options = [opt.strip() for opt in question.split('\n')[1:5]]
143
- user_choice = chr(ord('a') + options.index(user_answer)) if user_answer in options else 'No answer'
144
 
145
- is_correct = user_choice == correct_answer
146
- user_results.append(f"Your answer: {user_choice}, Correct answer: {correct_answer}, {'Correct!' if is_correct else 'Incorrect'}")
147
 
148
- return "\n".join(user_results)
149
 
150
- # Create the Gradio interface
151
- with gr.Blocks() as demo:
152
- gr.Markdown("# Interactive Video Lecture Assistant")
153
 
154
- with gr.Tab("Generate Summary and Quiz"):
155
- transcript_input = gr.Textbox(label="Video Lecture Transcript", lines=10, placeholder="Paste the video transcript or a detailed description of the lecture content here...")
156
- num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
157
- generate_btn = gr.Button("Generate Summary and Quiz")
158
- summary_output = gr.Textbox(label="Summary", lines=5)
159
- quiz_output = gr.Textbox(label="Quiz Questions", lines=15, visible=False)
160
 
161
- with gr.Tab("Take Quiz"):
162
- quiz_interface = gr.Column()
163
- submit_quiz_btn = gr.Button("Submit Quiz")
164
- quiz_results = gr.Textbox(label="Quiz Results", lines=5)
165
 
166
- def update_quiz_interface(questions):
167
- quiz_interface.clear()
168
- elements = create_quiz_interface(questions)
169
- for element in elements:
170
- quiz_interface.append(element)
171
- return {quiz_interface: gr.update(visible=True)}
172
 
173
- generate_btn.click(
174
- process_lecture,
175
- inputs=[transcript_input, num_questions],
176
- outputs=[summary_output, quiz_output]
177
- ).then(
178
- update_quiz_interface,
179
- inputs=[quiz_output],
180
- outputs=[quiz_interface]
181
- )
182
 
183
- submit_quiz_btn.click(
184
- check_answers,
185
- inputs=[quiz_output] + [child for child in quiz_interface.children if isinstance(child, gr.components.Radio)],
186
- outputs=[quiz_results]
187
- )
188
 
189
- if __name__ == "__main__":
190
- demo.launch()
 
3
 
4
  genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  model = genai.GenerativeModel('gemini-pro')
7
 
8
  def generate_summary_and_quiz(transcript, num_questions):
 
42
  with gr.Row():
43
  gr.Markdown("Generating summary and quiz...")
44
  result = generate_summary_and_quiz(transcript, num_questions)
45
+ return result
46
+
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown("# Video Lecture Summarizer and Quiz Generator")
49
 
50
+ transcript_input = gr.Textbox(label="Video Lecture Transcript", lines=10, placeholder="Paste the video transcript or a detailed description of the lecture content here...")
51
+ num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
 
52
 
53
+ generate_btn = gr.Button("Generate Summary and Quiz")
54
+ output = gr.Textbox(label="Summary and Quiz", lines=20)
55
 
56
+ generate_btn.click(process_lecture, inputs=[transcript_input, num_questions], outputs=output)
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()
60
+
61
+
62
+
63
+
64
+ # model = genai.GenerativeModel('gemini-pro')
65
+
66
+ # def generate_summary_and_quiz(transcript, num_questions):
67
+ # """Generate a summary and quiz questions based on the video transcript."""
68
+ # prompt = f"""
69
+ # Based on the following video lecture transcript, please provide:
70
+ # 1. A concise summary of the main points (about 100 words)
71
+ # 2. {num_questions} multiple-choice quiz questions to test understanding of key concepts
72
+
73
+ # Transcript:
74
+ # {transcript}
75
+
76
+ # Format your response as follows:
77
+ # Summary:
78
+ # [Your summary here]
79
+
80
+ # Quiz Questions:
81
+ # 1. [Question]
82
+ # a) [Option A]
83
+ # b) [Option B]
84
+ # c) [Option C]
85
+ # d) [Option D]
86
+ # Correct Answer: [Correct option letter]
87
+
88
+ # 2. [Next question and options...]
89
+
90
+ # Ensure the questions cover different aspects of the lecture and vary in difficulty.
91
+ # """
92
+
93
+ # try:
94
+ # response = model.generate_content(prompt)
95
+ # return response.text
96
+ # except Exception as e:
97
+ # return f"Error generating summary and quiz: {str(e)}"
98
+
99
+ # def process_lecture(transcript, num_questions):
100
+ # with gr.Row():
101
+ # gr.Markdown("Generating summary and quiz...")
102
+ # result = generate_summary_and_quiz(transcript, num_questions)
103
+
104
+ # # Extracting summary and questions
105
+ # summary_match = re.search(r'Summary:(.*?)Quiz Questions:', result, re.DOTALL)
106
+ # summary = summary_match.group(1).strip() if summary_match else "Summary not found."
107
+
108
+ # questions_match = re.findall(r'(\d+\.\s.*?)(?=\d+\.|$)', result.split('Quiz Questions:')[1], re.DOTALL)
109
+ # questions = [q.strip() for q in questions_match]
110
+
111
+ # return summary, questions
112
+
113
+ # def create_quiz_interface(questions):
114
+ # quiz_elements = []
115
+ # for i, question in enumerate(questions):
116
+ # q_parts = question.split('\n')
117
+ # q_text = q_parts[0].split('.', 1)[1].strip()
118
+ # options = [opt.strip() for opt in q_parts[1:5]]
119
 
120
+ # quiz_elements.extend([
121
+ # gr.Markdown(f"**Question {i+1}:** {q_text}"),
122
+ # gr.Radio(options, label=f"Options for Question {i+1}")
123
+ # ])
124
 
125
+ # return quiz_elements
126
 
127
+ # def check_answers(questions, *user_answers):
128
+ # correct_answers = []
129
+ # user_results = []
130
 
131
+ # for question, user_answer in zip(questions, user_answers):
132
+ # correct_answer = re.search(r'Correct Answer: (\w)', question).group(1)
133
+ # correct_answers.append(correct_answer)
134
 
135
+ # options = [opt.strip() for opt in question.split('\n')[1:5]]
136
+ # user_choice = chr(ord('a') + options.index(user_answer)) if user_answer in options else 'No answer'
137
 
138
+ # is_correct = user_choice == correct_answer
139
+ # user_results.append(f"Your answer: {user_choice}, Correct answer: {correct_answer}, {'Correct!' if is_correct else 'Incorrect'}")
140
 
141
+ # return "\n".join(user_results)
142
 
143
+ # # Create the Gradio interface
144
+ # with gr.Blocks() as demo:
145
+ # gr.Markdown("# Interactive Video Lecture Assistant")
146
 
147
+ # with gr.Tab("Generate Summary and Quiz"):
148
+ # transcript_input = gr.Textbox(label="Video Lecture Transcript", lines=10, placeholder="Paste the video transcript or a detailed description of the lecture content here...")
149
+ # num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
150
+ # generate_btn = gr.Button("Generate Summary and Quiz")
151
+ # summary_output = gr.Textbox(label="Summary", lines=5)
152
+ # quiz_output = gr.Textbox(label="Quiz Questions", lines=15, visible=False)
153
 
154
+ # with gr.Tab("Take Quiz"):
155
+ # quiz_interface = gr.Column()
156
+ # submit_quiz_btn = gr.Button("Submit Quiz")
157
+ # quiz_results = gr.Textbox(label="Quiz Results", lines=5)
158
 
159
+ # def update_quiz_interface(questions):
160
+ # quiz_interface.clear()
161
+ # elements = create_quiz_interface(questions)
162
+ # for element in elements:
163
+ # quiz_interface.append(element)
164
+ # return {quiz_interface: gr.update(visible=True)}
165
 
166
+ # generate_btn.click(
167
+ # process_lecture,
168
+ # inputs=[transcript_input, num_questions],
169
+ # outputs=[summary_output, quiz_output]
170
+ # ).then(
171
+ # update_quiz_interface,
172
+ # inputs=[quiz_output],
173
+ # outputs=[quiz_interface]
174
+ # )
175
 
176
+ # submit_quiz_btn.click(
177
+ # check_answers,
178
+ # inputs=[quiz_output] + [child for child in quiz_interface.children if isinstance(child, gr.components.Radio)],
179
+ # outputs=[quiz_results]
180
+ # )
181
 
182
+ # if __name__ == "__main__":
183
+ # demo.launch()