Spaces:
Sleeping
Sleeping
Download app.py from dwitee/MCQfromPdf: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/dwitee/MCQfromPdf/resolve/main/app.py
- Command line
-
hf download hf://spaces/dwitee/MCQfromPdf/app.py
-
curl -L -o app.py https://huggingface.co/spaces/dwitee/MCQfromPdf/resolve/main/app.py
3.04 kB
| import gradio as gr | |
| import fitz # PyMuPDF | |
| from openai import OpenAI | |
| import os | |
| import json | |
| # === Load OpenAI API Key === | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # === PDF Text Extractor === | |
| def extract_text_from_pdf(filepath): | |
| doc = fitz.open(filepath) | |
| return "".join([page.get_text() for page in doc]) | |
| # === GPT-Based MCQ Generator === | |
| def generate_mcqs_with_gpt(text): | |
| prompt = f""" | |
| Generate 3 multiple choice questions from the following text in this format: | |
| [ | |
| {{ | |
| "question": "...", | |
| "options": ["...", "...", "...", "..."], | |
| "answer": "..." | |
| }}, | |
| ... | |
| ] | |
| Text: | |
| {text} | |
| """ | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0.7, | |
| ) | |
| return json.loads(response.choices[0].message.content) | |
| # === Gradio UI === | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🧠 PDF to MCQ Quiz Generator (Powered by GPT-3.5)") | |
| pdf_file = gr.File(label="Upload PDF Slides", file_types=[".pdf"]) | |
| generate_btn = gr.Button("Generate Quiz") | |
| quiz_area = gr.Column(visible=False) | |
| question_radios = [] | |
| answer_store = gr.State([]) | |
| for i in range(5): # Predefine 5 slots for MCQs | |
| radio = gr.Radio(choices=[], label=f"Question {i+1}", visible=False, type="value") | |
| question_radios.append(radio) | |
| submit_btn = gr.Button("Submit Answers", visible=False) | |
| result_box = gr.Textbox(label="Result", visible=False) | |
| # === Generate Quiz Callback === | |
| def handle_generate(file): | |
| text = extract_text_from_pdf(file.name) | |
| mcqs = generate_mcqs_with_gpt(text) | |
| correct_answers = [] | |
| updates = [] | |
| for i in range(5): | |
| if i < len(mcqs): | |
| q = mcqs[i] | |
| updates.append(gr.update(choices=q["options"], label=q["question"], visible=True)) | |
| correct_answers.append(q["answer"]) | |
| else: | |
| updates.append(gr.update(visible=False)) # Hide unused | |
| return updates + [gr.update(visible=True), gr.update(visible=True), correct_answers] | |
| generate_btn.click( | |
| fn=handle_generate, | |
| inputs=pdf_file, | |
| outputs=question_radios + [submit_btn, quiz_area, answer_store] | |
| ) | |
| # === Submit Quiz Callback === | |
| def handle_submit(*user_answers_and_state): | |
| *user_answers, correct_answers = user_answers_and_state | |
| score = 0 | |
| feedback = [] | |
| for i, (user_ans, correct_ans) in enumerate(zip(user_answers, correct_answers)): | |
| if user_ans == correct_ans: | |
| score += 1 | |
| feedback.append(f"{i+1}. ✅ Correct") | |
| else: | |
| feedback.append(f"{i+1}. ❌ Your answer: {user_ans} | Correct: {correct_ans}") | |
| return f"Score: {score}/{len(correct_answers)}\\n\\n" + "\\n".join(feedback) | |
| submit_btn.click( | |
| fn=handle_submit, | |
| inputs=question_radios + [answer_store], | |
| outputs=result_box | |
| ) | |
| demo.launch() |