File size: 4,532 Bytes
47e17b2
 
 
93ae679
47e17b2
93ae679
47e17b2
 
 
 
 
 
 
 
 
 
 
93ae679
47e17b2
 
 
93ae679
 
47e17b2
 
 
93ae679
47e17b2
 
 
 
 
 
 
 
 
 
 
93ae679
 
 
47e17b2
 
 
 
 
 
 
 
 
 
 
 
93ae679
 
47e17b2
 
 
 
 
 
 
 
 
 
 
93ae679
 
 
47e17b2
93ae679
 
47e17b2
 
 
 
 
 
 
 
93ae679
47e17b2
93ae679
47e17b2
 
93ae679
47e17b2
 
 
 
 
93ae679
47e17b2
 
 
 
93ae679
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import gradio as gr
import os
import tempfile
from mylangv2 import DocumentProcessor, QuestionGenerator

# Check for required environment variables at startup
REQUIRED_ENV_VARS = [
    "AZURE_OPENAI_API_KEY",
    "AZURE_OPENAI_ENDPOINT",
    "AZURE_OPENAI_EMBEDDING_DEPLOYMENT",
    "AZURE_OPENAI_CHAT_DEPLOYMENT",
    "AZURE_OPENAI_API_VERSION"
]
missing_vars = [var for var in REQUIRED_ENV_VARS if not os.environ.get(var)]
if missing_vars:
    raise EnvironmentError(f"Missing required environment variables: {', '.join(missing_vars)}")

# Initialize core logic
processor = DocumentProcessor()
generator = QuestionGenerator()

def generate_questions_from_pdf(pdf_file, subject, section, question_type, class_grade, difficulty, bloom_level, num_questions, additional_instructions):
    import shutil
    pdf_path = None
    vectorstore = None
    if pdf_file is not None:
        # Handle both file-like and NamedString cases
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
            if hasattr(pdf_file, 'read'):
                tmp.write(pdf_file.read())
            elif hasattr(pdf_file, 'name'):
                with open(pdf_file.name, 'rb') as fsrc:
                    shutil.copyfileobj(fsrc, tmp)
            else:
                return "<span style='color:red'>Invalid file input. Please upload a valid PDF file.</span>"
            pdf_path = tmp.name
        try:
            vectorstore, _ = processor.process_uploaded_document(pdf_path)
        except Exception as e:
            return ("<span style='color:red'>An error occurred while processing the PDF. "
                    "Please check your file and try again. If the problem persists, contact support or check the logs for details.</span>")
    try:
        topic_data = {
            "subjectName": subject,
            "sectionName": section,
            "questionType": question_type,
            "classGrade": class_grade,
            "difficulty": difficulty,
            "bloomLevel": bloom_level,
            "numQuestions": int(num_questions),
            "additionalInstructions": additional_instructions or ""
        }
        result = generator.generate_questions(topic_data, vectorstore)
        questions = result["questions"]
        # Format for display
        display = ""
        for i, q in enumerate(questions, 1):
            display += f"<b>Question {i}:</b> {q.get('question', '')}<br>"
            if 'options' in q:
                display += "Options:<ul>"
                for opt in q['options']:
                    display += f"<li>{opt}</li>"
                display += "</ul>"
            display += f"<b>Answer:</b> {q.get('answer', q.get('correctAnswer', ''))}<br>"
            display += f"<b>Explanation:</b> {q.get('explanation', '')}<br><hr>"
        return display
    except Exception as e:
        return ("<span style='color:red'>An error occurred while generating questions. "
                "Please check your input and try again. If the problem persists, contact support or check the logs for details.</span>")
    finally:
        if pdf_path is not None:
            os.remove(pdf_path)

with gr.Blocks() as demo:
    gr.Markdown("# Question Generator from PDF (LangChain)")
    with gr.Row():
        with gr.Column():
            pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
            subject = gr.Textbox(label="Subject Name", placeholder="e.g. Biology")
            section = gr.Textbox(label="Topic/Section Name", placeholder="e.g. Life Process")
            question_type = gr.Dropdown(["MCQ", "Short Answer", "Long Answer"], label="Question Type", value="MCQ")
            class_grade = gr.Textbox(label="Class/Grade", placeholder="e.g. 10")
            difficulty = gr.Dropdown(["Easy", "Medium", "Hard"], label="Difficulty", value="Medium")
            bloom_level = gr.Textbox(label="Bloom's Level", placeholder="e.g. Understand")
            num_questions = gr.Number(label="Number of Questions", value=2, precision=0)
            additional_instructions = gr.Textbox(label="Additional Instructions", placeholder="Any extra instructions (optional)")
            btn = gr.Button("Generate Questions")
        with gr.Column():
            output = gr.HTML(label="Generated Questions")
    btn.click(
        generate_questions_from_pdf,
        inputs=[pdf_file, subject, section, question_type, class_grade, difficulty, bloom_level, num_questions, additional_instructions],
        outputs=output
    )

if __name__ == "__main__":
    demo.launch()