File size: 8,474 Bytes
e6cb8e8
 
 
 
 
 
 
 
 
 
 
eea8826
e6cb8e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246ef24
e6cb8e8
 
 
 
246ef24
e6cb8e8
 
 
 
 
 
 
 
 
 
eea8826
e6cb8e8
 
 
 
 
 
 
 
246ef24
e6cb8e8
 
 
 
eea8826
e6cb8e8
eea8826
246ef24
e6cb8e8
eea8826
 
e6cb8e8
 
 
 
 
 
 
 
 
 
 
eea8826
 
e6cb8e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246ef24
 
e6cb8e8
 
 
 
 
 
 
 
 
 
eea8826
e6cb8e8
 
 
 
 
 
eea8826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cb8e8
 
eea8826
e6cb8e8
 
 
 
 
 
 
 
 
 
 
eea8826
e6cb8e8
eea8826
e6cb8e8
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import gradio as gr
from openai import OpenAI
import os

ACCESS_TOKEN = os.getenv("HF_TOKEN")

client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1/", 
    api_key=ACCESS_TOKEN,
)

def generate_study_material(
    topic, 
    difficulty, 
    question_type, 
    focus_areas, 
    anxiety_level,
    num_questions
):
    # Customize prompt based on anxiety level and learning focus
    anxiety_prompts = {
        "High": "Create a gradual, confidence-building set of questions. Start with easier concepts and progressively increase difficulty. Include encouraging notes.",
        "Medium": "Balance challenge with achievability. Include hints for tougher questions and positive reinforcement.",
        "Low": "Focus on comprehensive concept testing while maintaining an encouraging tone."
    }
    
    focus_prompt = {
        "concept_understanding": "Emphasize questions that test deep understanding rather than memorization.",
        "problem_solving": "Include scenario-based questions that require analytical thinking.",
        "quick_recall": "Focus on key definitions and fundamental concepts.",
        "practical_application": "Create questions based on real-world applications."
    }

    base_prompt = f"""
    Act as an expert educational psychologist and subject matter expert creating an exam preparation guide.
    Topic: {topic}
    Difficulty: {difficulty}
    Question Type: {question_type}
    Number of Questions: {num_questions}
    
    Special Considerations:
    - Anxiety Level: {anxiety_level}
    {anxiety_prompts[anxiety_level]}
    
    - Learning Focus: {focus_areas}
    {focus_prompt[focus_areas]}
    Generate questions following these guidelines:
    1. Start with a brief confidence-building message
    2. Include clear, unambiguous questions
    3. Provide detailed explanations for correct answer
    4. Add study tips relevant to the topic
    5. Include a "Remember" section with key points
    
    Format:
    - For Multiple Choice: Include 4 options with explanations for correct one only
    - For Short Answer: Provide structure hints and model answers
    - For Descriptive: Break down marking criteria and include outline points
    
    Additional Requirements:
    - Include think-aloud strategies for problem-solving
    - Add time management suggestions
    - Highlight common misconceptions to avoid
    - End with a positive reinforcement message
    """

    try:
        messages = [
            {"role": "system", "content": "You are an expert educational content generator."},
            {"role": "user", "content": base_prompt}
        ]
        
        response = client.chat.completions.create(
            model="Qwen/QwQ-32B-Preview",
            messages=messages,
            max_tokens=1700,
            temperature=0.7,
            top_p=0.9
        )
        
        return response.choices[0].message.content
    
    except Exception as e:
        return f"An error occurred: {str(e)}\nServer is bussy! Please come back later."

def create_interface():
    with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as iface:
        gr.Markdown("""
        # <div align="center"><strong>📚 Exam Preparation Assistant</strong></div>
        
        Welcome to your personalized exam preparation assistant! This tool is designed to help you:
        - Build confidence through practiced learning
        - Understand concepts deeply
        - Reduce exam anxiety through structured practice
        
        Remember: Every practice session brings you closer to mastery! 🌟
        """)
        
        with gr.Row():
            with gr.Column():
                topic = gr.Textbox(
                    label="Topic or Subject",
                    placeholder="Enter the topic you want to study (e.g., 'Python Lists and Tuples', 'Chemical Bonding')",
                    lines=2
                )
                
                difficulty = gr.Radio(
                    choices=["Beginner", "Intermediate", "Advanced"],
                    label="Difficulty Level",
                    value="Intermediate",
                    info="Choose based on your current understanding"
                )
                
                question_type = gr.Radio(
                    choices=["Multiple Choice", "Short Answer", "Descriptive"],
                    label="Question Type",
                    value="Multiple Choice",
                    info="Select the format that best helps your learning"
                )
                
                focus_areas = gr.Radio(
                    choices=[
                        "concept_understanding",
                        "problem_solving",
                        "quick_recall",
                        "practical_application"
                    ],
                    label="Learning Focus",
                    value="concept_understanding",
                    info="What aspect do you want to improve?"
                )
                
                anxiety_level = gr.Radio(
                    choices=["High", "Medium", "Low"],
                    label="Current Anxiety Level",
                    value="Medium",
                    info="This helps us adjust the difficulty progression"
                )
                
                num_questions = gr.Slider(
                    minimum=1,
                    maximum=3,
                    value=5,
                    step=1,
                    label="Number of Questions"
                )
                
                submit_btn = gr.Button(
                    "Generate Study Material",
                    variant="primary"
                )

            with gr.Column():
                output = gr.Textbox(
                    label="Your Personalized Study Material",
                    lines=20,
                    show_copy_button=True
                )
        
        # Example scenarios
        gr.Examples(
            examples=[
                [
                    "Python Functions and Basic Programming",
                    "Beginner",
                    "Multiple Choice",
                    "concept_understanding",
                    "High",
                    5
                ],
                [
                    "Data Structures - Arrays and Linked Lists",
                    "Intermediate",
                    "Short Answer",
                    "problem_solving",
                    "Medium",
                    5
                ],
                [
                    "Advanced Algorithms - Dynamic Programming",
                    "Advanced",
                    "Descriptive",
                    "practical_application",
                    "Low",
                    3
                ]
            ],
            inputs=[
                topic,
                difficulty,
                question_type,
                focus_areas,
                anxiety_level,
                num_questions
            ],
            outputs=output,
            fn=generate_study_material,
            cache_examples=True
        )
        
        # Usage tips
        gr.Markdown("""
        ### 💡 Tips for Best Results
        1. **Be Specific** with your topic - instead of "Math", try "Quadratic Equations"
        2. **Match the Difficulty** to your current understanding
        3. **Vary Question Types** to improve overall understanding
        4. **Be Honest** about your anxiety level - it helps us provide better support
        
        ### 🎯 Learning Focus Options
        - **Concept Understanding**: Deep grasp of fundamental principles
        - **Problem Solving**: Analytical and application skills
        - **Quick Recall**: Key definitions and core concepts
        - **Practical Application**: Real-world usage and examples
        
        ### 🌟 Remember
        - Take regular breaks
        - Practice consistently
        - Focus on understanding, not just memorizing
        - Each practice session improves your knowledge!
        """)
        
        submit_btn.click(
            fn=generate_study_material,
            inputs=[
                topic,
                difficulty,
                question_type,
                focus_areas,
                anxiety_level,
                num_questions
            ],
            outputs=output
        )
    
    return iface

if __name__ == "__main__":
    iface = create_interface()
    iface.launch()