Spaces:
Runtime error
Runtime error
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() |