Siddiqui Qamar commited on
Commit
e6cb8e8
·
verified ·
1 Parent(s): 900b4d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -0
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+
5
+ ACCESS_TOKEN = os.getenv("HF_TOKEN")
6
+
7
+ client = OpenAI(
8
+ base_url="https://api-inference.huggingface.co/v1/",
9
+ api_key=ACCESS_TOKEN,
10
+ )
11
+
12
+ *def* *generate_study_material*(
13
+ topic,
14
+ difficulty,
15
+ question_type,
16
+ focus_areas,
17
+ anxiety_level,
18
+ num_questions
19
+ ):
20
+ # Customize prompt based on anxiety level and learning focus
21
+ anxiety_prompts = {
22
+ "High": "Create a gradual, confidence-building set of questions. Start with easier concepts and progressively increase difficulty. Include encouraging notes.",
23
+ "Medium": "Balance challenge with achievability. Include hints for tougher questions and positive reinforcement.",
24
+ "Low": "Focus on comprehensive concept testing while maintaining an encouraging tone."
25
+ }
26
+
27
+ focus_prompt = {
28
+ "concept_understanding": "Emphasize questions that test deep understanding rather than memorization.",
29
+ "problem_solving": "Include scenario-based questions that require analytical thinking.",
30
+ "quick_recall": "Focus on key definitions and fundamental concepts.",
31
+ "practical_application": "Create questions based on real-world applications."
32
+ }
33
+
34
+ base_prompt = f"""
35
+ Act as an expert educational psychologist and subject matter expert creating an exam preparation guide.
36
+ Topic: {topic}
37
+ Difficulty: {difficulty}
38
+ Question Type: {question_type}
39
+ Number of Questions: {num_questions}
40
+
41
+ Special Considerations:
42
+ - Anxiety Level: {anxiety_level}
43
+ {anxiety_prompts[anxiety_level]}
44
+
45
+ - Learning Focus: {focus_areas}
46
+ {focus_prompt[focus_areas]}
47
+ Generate questions following these guidelines:
48
+ 1. Start with a brief confidence-building message
49
+ 2. Include clear, unambiguous questions
50
+ 3. Provide detailed explanations for each answer
51
+ 4. Add study tips relevant to the topic
52
+ 5. Include a "Remember" section with key points
53
+
54
+ Format:
55
+ - For Multiple Choice: Include 4 options with explanations for each
56
+ - For Short Answer: Provide structure hints and model answers
57
+ - For Descriptive: Break down marking criteria and include outline points
58
+
59
+ Additional Requirements:
60
+ - Include think-aloud strategies for problem-solving
61
+ - Add time management suggestions
62
+ - Highlight common misconceptions to avoid
63
+ - End with a positive reinforcement message
64
+ """
65
+
66
+ *try*:
67
+ messages = [
68
+ {"role": "system", "content": "You are an expert educational content generator."},
69
+ {"role": "user", "content": base_prompt}
70
+ ]
71
+
72
+ response = client.chat.completions.create(
73
+ model="Qwen/QwQ-32B-Preview",
74
+ messages=messages,
75
+ max_tokens=2048,
76
+ temperature=0.7,
77
+ top_p=0.9
78
+ )
79
+
80
+ *return* response.choices[0].message.content
81
+
82
+ *except* *Exception* *as* e:
83
+ *return* f"An error occurred: {*str*(e)}\nPlease try again with different parameters."
84
+
85
+ *def* *create_interface*():
86
+ *with* gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) *as* iface:
87
+ gr.Markdown("""
88
+ # <div align="center"><strong>📚 Exam Preparation Assistant</strong></div>
89
+
90
+ Welcome to your personalized exam preparation assistant! This tool is designed to help you:
91
+ - Build confidence through practiced learning
92
+ - Understand concepts deeply
93
+ - Reduce exam anxiety through structured practice
94
+
95
+ Remember: Every practice session brings you closer to mastery! 🌟
96
+ """)
97
+
98
+ *with* gr.Row():
99
+ *with* gr.Column():
100
+ topic = gr.Textbox(
101
+ label="Topic or Subject",
102
+ placeholder="Enter the topic you want to study (e.g., 'Python Lists and Tuples', 'Chemical Bonding')",
103
+ lines=2
104
+ )
105
+
106
+ difficulty = gr.Radio(
107
+ choices=["Beginner", "Intermediate", "Advanced"],
108
+ label="Difficulty Level",
109
+ value="Intermediate",
110
+ info="Choose based on your current understanding"
111
+ )
112
+
113
+ question_type = gr.Radio(
114
+ choices=["Multiple Choice", "Short Answer", "Descriptive"],
115
+ label="Question Type",
116
+ value="Multiple Choice",
117
+ info="Select the format that best helps your learning"
118
+ )
119
+
120
+ focus_areas = gr.Radio(
121
+ choices=[
122
+ "concept_understanding",
123
+ "problem_solving",
124
+ "quick_recall",
125
+ "practical_application"
126
+ ],
127
+ label="Learning Focus",
128
+ value="concept_understanding",
129
+ info="What aspect do you want to improve?"
130
+ )
131
+
132
+ anxiety_level = gr.Radio(
133
+ choices=["High", "Medium", "Low"],
134
+ label="Current Anxiety Level",
135
+ value="Medium",
136
+ info="This helps us adjust the difficulty progression"
137
+ )
138
+
139
+ num_questions = gr.Slider(
140
+ minimum=3,
141
+ maximum=10,
142
+ value=5,
143
+ step=1,
144
+ label="Number of Questions"
145
+ )
146
+
147
+ submit_btn = gr.Button(
148
+ "Generate Study Material",
149
+ variant="primary"
150
+ )
151
+
152
+ *with* gr.Column():
153
+ output = gr.Textbox(
154
+ label="Your Personalized Study Material",
155
+ lines=20,
156
+ show_copy_button=True
157
+ )
158
+
159
+
160
+ submit_btn.click(
161
+ *fn*=generate_study_material,
162
+ inputs=[
163
+ topic,
164
+ difficulty,
165
+ question_type,
166
+ focus_areas,
167
+ anxiety_level,
168
+ num_questions
169
+ ],
170
+ outputs=output
171
+ )
172
+
173
+ *return* iface
174
+
175
+ *if* **name** == "__main__":
176
+ iface = create_interface()
177
+ iface.launch()