File size: 4,445 Bytes
9f10cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import gradio as gr
from google import genai
from google.genai import types
from dotenv import load_dotenv

load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")

client = genai.Client(api_key=GEMINI_API_KEY)

question_types = {
    "MCQs": """

                Rules:

                - Generate multiple-choice questions.

                - Each question must test conceptual understanding, not just direct copying.

                - Each question must have exactly 4 options labeled A, B, C, and D.

                - Only one option should be correct.

                - Avoid ambiguous wording.

                - After all questions, provide a separate section titled 'Answer Key' listing correct answers like:

                1. B

                2. A

                3. D

                Keep formatting clean and consistent.""",

    "Short Answer": """

                    Rules:

                    - Generate short-answer questions.

                    - Questions should require 2–4 sentence answers.

                    - Focus on key concepts, definitions, and explanations.

                    - Avoid yes/no questions.

                    - Do not provide the answers.

                    - Ensure clarity and academic tone.

                    - Keep numbering consistent.""",

    "Interview": """

                    Rules:

                    - Generate interview-style questions.

                    - Questions should assess deep understanding and practical knowledge.

                    - Include scenario-based or application-based questions.

                    - Questions should be suitable for a technical interview.

                    - Avoid overly theoretical or textbook-style phrasing.

                    - Do not provide answers.

                    - Keep formatting clean and professional."""
}

difficulty_rules = {
    "Easy": "Questions should test basic definitions and direct concepts.",
    "Medium": "Questions should test understanding and application of concepts.",
    "Hard": "Questions should test deep analysis, critical thinking, and real-world application."
}

def question_generator(content, q_type,num_questions,difficulty):
    base_rules = question_types[q_type]
    difficulty_instructions = difficulty_rules[difficulty]
    system_prompt = f"""

                        You are an expert academic question paper setter.



                        Generate exactly {num_questions} {difficulty}-level {q_type} questions 

                        based strictly on the provided content.



                        IMPORTANT OUTPUT RULES:

                        - Do NOT write any introduction sentence.

                        - Do NOT write any explanation before the questions.

                        - Start directly from Question 1.

                        - Do NOT include phrases like "Here are the questions".

                        - Output only the questions and required sections.

                        - Follow formatting strictly.



                        {difficulty_instructions}



                        {base_rules}

                        """

    response = client.models.generate_content(
        model="gemini-2.5-flash",
        config=types.GenerateContentConfig(
            system_instruction=system_prompt,
            temperature=0.4,
           max_output_tokens = max(1200, num_questions * 250)
        ),
        contents=content
    )
    return response.text


demo = gr.Interface(
    fn=question_generator,
    inputs=[
        gr.Textbox(
            lines=6,
            placeholder="Paste study material or content here...",
            label="Input Content"
        ),
        gr.Radio(
            choices=list(question_types.keys()),
            value="MCQs",
            label="Question Type"
        ),
        gr.Slider(1,10,value=5, label="Number of Questions"),
        gr.Radio(
                choices=["Easy", "Medium", "Hard"],
                value="Medium",
                label="Difficulty Level",
                info="Select the difficulty level of the questions"
        )
    ],
    outputs=gr.Textbox(lines=12, label="Generated Questions"),
    title="Question Generator",
    description="Generate MCQs, short-answer, or interview-style questions from given content using Gemini."
)

demo.launch(debug=True)