qamarsidd's picture
Update app.py
eea8826 verified
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
)
# 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()