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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -130
app.py CHANGED
@@ -1,65 +1,7 @@
1
- import gradio as gr
2
- 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
 
64
  # model = genai.GenerativeModel('gemini-pro')
65
 
@@ -100,84 +42,113 @@ if __name__ == "__main__":
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()
 
1
+ # import gradio as gr
2
+ # import google.generativeai as genai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")
5
 
6
  # model = genai.GenerativeModel('gemini-pro')
7
 
 
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
+ import gradio as gr
65
+ import google.generativeai as genai
66
+ import whisper
67
+ import os
68
+ import tempfile
69
+
70
+
71
+ genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")
72
+
73
+ model = genai.GenerativeModel('gemini-pro')
74
+
75
+ whisper_model = whisper.load_model("base")
76
+
77
+ def transcribe_video(video_path):
78
+ """Transcribe the audio from a video file."""
79
+ try:
80
+ result = whisper_model.transcribe(video_path)
81
+ return result["text"]
82
+ except Exception as e:
83
+ return f"Error transcribing video: {str(e)}"
84
+
85
+ def generate_summary_and_quiz(transcript, num_questions):
86
+ """Generate a summary and quiz questions based on the video transcript."""
87
+ prompt = f"""
88
+ Based on the following video lecture transcript, please provide:
89
+ 1. A concise summary of the main points (about 100 words)
90
+ 2. {num_questions} multiple-choice quiz questions to test understanding of key concepts
91
+
92
+ Transcript:
93
+ {transcript}
94
+
95
+ Format your response as follows:
96
+ Summary:
97
+ [Your summary here]
98
+
99
+ Quiz Questions:
100
+ 1. [Question]
101
+ a) [Option A]
102
+ b) [Option B]
103
+ c) [Option C]
104
+ d) [Option D]
105
+ Correct Answer: [Correct option letter]
106
+
107
+ 2. [Next question and options...]
108
+
109
+ Ensure the questions cover different aspects of the lecture and vary in difficulty.
110
+ """
111
+
112
+ try:
113
+ response = model.generate_content(prompt)
114
+ return response.text
115
+ except Exception as e:
116
+ return f"Error generating summary and quiz: {str(e)}"
117
+
118
+ def process_video(video, num_questions):
119
+ with gr.Row():
120
+ gr.Markdown("Processing video and generating summary and quiz...")
121
 
 
 
 
 
 
 
 
 
 
122
 
123
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
124
+ video.save(temp_video.name)
125
+ video_path = temp_video.name
126
+
127
+ transcript = transcribe_video(video_path)
128
 
129
+ result = generate_summary_and_quiz(transcript, num_questions)
130
+
131
+ os.unlink(video_path)
132
+
133
+ return transcript, result
134
+
135
+
136
+ with gr.Blocks() as demo:
137
+ gr.Markdown("# Video Lecture Summarizer and Quiz Generator")
 
138
 
139
+ video_input = gr.Video(label="Upload Video Lecture")
140
+ num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
 
 
141
 
142
+ generate_btn = gr.Button("Process Video and Generate Summary and Quiz")
 
 
 
 
 
143
 
144
+ transcript_output = gr.Textbox(label="Video Transcript", lines=10)
145
+ summary_quiz_output = gr.Textbox(label="Summary and Quiz", lines=20)
 
 
 
 
 
 
 
146
 
147
+ generate_btn.click(
148
+ process_video,
149
+ inputs=[video_input, num_questions],
150
+ outputs=[transcript_output, summary_quiz_output]
151
+ )
152
 
153
+ if __name__ == "__main__":
154
+ demo.launch()