Spaces:
Sleeping
Sleeping
File size: 9,341 Bytes
e064d7b | 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | import gradio as gr
import os
import requests
import re
# Get Hugging Face token from environment variable
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
def call_hugging_face_api_content(prompt):
"""Call Hugging Face API for content generation"""
if not HF_TOKEN:
return None
try:
url = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
data = {
"inputs": prompt,
"parameters": {
"max_length": 1000,
"temperature": 0.7,
"do_sample": True
}
}
response = requests.post(url, headers=headers, json=data, timeout=30)
if response.status_code == 200:
result = response.json()
if isinstance(result, list) and len(result) > 0:
return result[0].get("generated_text", "")
return None
except Exception as e:
print(f"API call error: {e}")
return None
def generate_worksheet_content(topic, subject, grade_level, difficulty, content_type, content_length):
"""Generate educational worksheet content"""
# Try Hugging Face API first if token is available
if HF_TOKEN:
try:
prompt = f"""Create a comprehensive educational {content_type.lower()} about "{topic}" for {grade_level} students studying {subject}.
Requirements:
- Grade Level: {grade_level}
- Subject: {subject}
- Topic: {topic}
- Difficulty: {difficulty}
- Content Type: {content_type}
- Length: {content_length}
IMPORTANT: DO NOT USE LaTeX expressions. Use simple HTML/markdown formatting instead:
- For fractions: use "a/b" or "a over b" instead of \frac{{a}}{{b}}
- For math: use plain text like "Speed = Distance/Time" instead of \text{{Speed}} = \frac{{\text{{Distance}}}}{{\text{{Time}}}}
- For fill-in-blanks: use "_____" instead of \_\_\_
- For answer boxes: use "[ANSWER: ___]" instead of \boxed{{\_\_}}
- For subscripts: use "H2O" instead of H_2O
- For superscripts: use "xΒ²" instead of x^2
Format the response as a structured worksheet with clear sections, examples, and exercises."""
result = call_hugging_face_api_content(prompt)
if result:
return result
except Exception as e:
print(f"API error: {e}")
# Fallback content with LaTeX-free formatting
fallback_content = f"""# **{topic} in {subject}: Interactive Worksheet**
**Grade Level:** {grade_level} | **Subject:** {subject} | **Difficulty:** {difficulty}
---
## **Introduction**
{topic} helps students understand important concepts in {subject}! In this worksheet, you'll explore {topic} through hands-on activities and real-world examples.
---
## **Learning Objectives**
By the end of this worksheet, you will be able to:
β Understand the basic concepts of {topic}
β Apply {topic} to solve problems
β Use {topic} in real-world situations
β Check your work using self-assessment
---
## **Practice Problems**
### **1. Basic Concepts**
Solve the following problems step by step.
**Example:** If we have the equation: A = B Γ C
Find A when B = 5 and C = 3
[ANSWER: A = 15]
**Now you try:**
a) If D = E + F, find D when E = 10 and F = 7
[ANSWER: _____]
b) If G = H / I, find G when H = 20 and I = 4
[ANSWER: _____]
---
## **Word Problems**
### **2. Real-World Applications**
Read each problem carefully and solve.
**Problem 1:** A student travels 120 miles in 2 hours. What is their speed?
Equation: Speed = Distance / Time
Solution: Speed = _____ / _____ = _____ miles per hour
[ANSWER: 60 mph]
**Problem 2:** A container holds 50 liters of water. If 30 liters are used, how much remains?
Equation: Remaining = Total - Used
Solution: Remaining = _____ - _____ = _____ liters
[ANSWER: 20 liters]
---
## **Self-Assessment**
Rate your understanding:
β I need more practice
β I understand most problems
β I can teach this to someone else!
**Reflection:** What was the most challenging part?
[ANSWER: ________________________________]
---
## **Answer Key**
**Practice Problems:**
a) D = 17
b) G = 5
**Word Problems:**
1. Speed = 60 mph
2. Remaining = 20 liters
---
### **Worksheet Summary**
Great job! You've practiced {topic} concepts and applied them to real-world problems. Keep practicing to master these skills!
---
**Total Words:** ~800 | **Format:** Interactive Worksheet
**Ready for Print/Digital Use!** β
"""
return fallback_content
def apply_simple_styling(content):
"""Apply simple styling to make content look better"""
# Style answer boxes
content = re.sub(r'\[ANSWER: ([^\]]+)\]', r'<span style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: \1]</span>', content)
# Style fill-in-blank spaces
content = re.sub(r'___+', r'<span style="border-bottom: 2px solid #3498db; min-width: 40px; display: inline-block; margin: 0 5px;"> </span>', content)
# Style checkboxes
content = re.sub(r'β', r'<input type="checkbox" style="margin-right: 8px; transform: scale(1.2);">', content)
content = re.sub(r'β', r'<input type="checkbox" checked style="margin-right: 8px; transform: scale(1.2);">', content)
return content
def generate_worksheet(topic, subject, grade_level, difficulty, content_type, content_length):
"""Main function to generate and style worksheet content"""
content = generate_worksheet_content(topic, subject, grade_level, difficulty, content_type, content_length)
styled_content = apply_simple_styling(content)
return styled_content
# Create the Gradio interface
with gr.Blocks(
title="BrightMind AI - Educational Worksheet Generator",
theme=gr.themes.Soft(),
css="""
.gradio-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.main-container {
background: white;
border-radius: 12px;
padding: 20px;
margin: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.worksheet-output {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
margin-top: 20px;
border-left: 4px solid #3498db;
}
"""
) as demo:
gr.Markdown("""
# π§ BrightMind AI
## Professional Educational Content Generator for Teachers
Generate interactive worksheets tailored to your specific needs. Simply fill in the details below and click "Generate Worksheet" to create engaging educational materials.
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### π Worksheet Settings")
topic = gr.Textbox(
label="π Topic",
placeholder="e.g., algebra, photosynthesis, world war II",
value="algebra"
)
subject = gr.Dropdown(
label="π Subject",
choices=["Science", "Mathematics", "History", "English", "Geography", "Art", "Music", "Physical Education"],
value="Science"
)
grade_level = gr.Dropdown(
label="π Grade Level",
choices=["K-2", "3-5", "6-8", "9-12"],
value="6-8"
)
difficulty = gr.Dropdown(
label="π― Difficulty",
choices=["Beginner", "Intermediate", "Advanced"],
value="Intermediate"
)
content_type = gr.Dropdown(
label="π Content Type",
choices=["Worksheets", "Handouts", "Study Guides", "Activities", "Presentations", "Lesson Materials"],
value="Worksheets"
)
content_length = gr.Dropdown(
label="π Length",
choices=["Short (1-2 pages)", "Medium (3-5 pages)", "Long (6+ pages)"],
value="Medium (3-5 pages)"
)
generate_btn = gr.Button(
"π Generate Worksheet",
variant="primary",
size="lg"
)
clear_btn = gr.Button(
"ποΈ Clear",
variant="secondary"
)
with gr.Column(scale=2):
gr.Markdown("### π Generated Worksheet")
worksheet_output = gr.HTML(
value="<div style='text-align: center; color: #666; font-style: italic; padding: 40px;'>Your educational worksheet will appear here...</div>",
label=""
)
# Event handlers
generate_btn.click(
fn=generate_worksheet,
inputs=[topic, subject, grade_level, difficulty, content_type, content_length],
outputs=worksheet_output
)
clear_btn.click(
fn=lambda: "<div style='text-align: center; color: #666; font-style: italic; padding: 40px;'>Your educational worksheet will appear here...</div>",
outputs=worksheet_output
)
if __name__ == "__main__":
demo.launch()
|