Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,71 @@ 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):
|
|
@@ -42,18 +107,84 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
gr.Markdown("# Video Lecture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
generate_btn
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
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):
|
| 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 |
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()
|