Ed_quiz / app.py
arjunanand13's picture
Update app.py
72c31b0 verified
raw
history blame
4.72 kB
# 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 whisper
import os
import tempfile
genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")
model = genai.GenerativeModel('gemini-pro')
whisper_model = whisper.load_model("base")
def transcribe_video(video_path):
"""Transcribe the audio from a video file."""
try:
result = whisper_model.transcribe(video_path)
return result["text"]
except Exception as e:
return f"Error transcribing video: {str(e)}"
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_video(video, num_questions):
with gr.Row():
gr.Markdown("Processing video and generating summary and quiz...")
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
video.save(temp_video.name)
video_path = temp_video.name
transcript = transcribe_video(video_path)
result = generate_summary_and_quiz(transcript, num_questions)
os.unlink(video_path)
return transcript, result
with gr.Blocks() as demo:
gr.Markdown("# Video Lecture Summarizer and Quiz Generator")
video_input = gr.Video(label="Upload Video Lecture")
num_questions = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz Questions")
generate_btn = gr.Button("Process Video and Generate Summary and Quiz")
transcript_output = gr.Textbox(label="Video Transcript", lines=10)
summary_quiz_output = gr.Textbox(label="Summary and Quiz", lines=20)
generate_btn.click(
process_video,
inputs=[video_input, num_questions],
outputs=[transcript_output, summary_quiz_output]
)
if __name__ == "__main__":
demo.launch()