Spaces:
Runtime error
Runtime error
File size: 6,362 Bytes
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 |
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 each 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 each
- 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=2048,
temperature=0.7,
top_p=0.9
)
*return* response.choices[0].message.content
*except* *Exception* *as* e:
*return* f"An error occurred: {*str*(e)}\nPlease try again with different parameters."
*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=3,
maximum=10,
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
)
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() |