Spaces:
Sleeping
Sleeping
Mahdi Naser Moghadasi
commited on
Commit
Β·
e064d7b
1
Parent(s):
1306b1b
Refactor to clean Gradio Blocks interface
Browse files- Create simplified app.py with proper Gradio Blocks structure
- Use gr.HTML() to render worksheet content instead of raw text
- Add clean UI with input controls and generate button
- Implement proper demo.launch() for Hugging Face Spaces
- Remove complex features and focus on core worksheet generation
- Add LaTeX-free content generation with proper styling
- Create backup of complex version as app_complex.py
- Simplify requirements.txt for better compatibility
- This follows the requested structure with gr.Blocks, gr.Markdown, and demo.launch()
- app.py +0 -0
- app_complex.py +0 -0
- app_simple.py +282 -0
- requirements.txt +2 -2
app.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app_complex.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app_simple.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
# Get Hugging Face token from environment variable
|
| 7 |
+
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
| 8 |
+
|
| 9 |
+
def call_hugging_face_api_content(prompt):
|
| 10 |
+
"""Call Hugging Face API for content generation"""
|
| 11 |
+
if not HF_TOKEN:
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
url = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
|
| 16 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 17 |
+
|
| 18 |
+
data = {
|
| 19 |
+
"inputs": prompt,
|
| 20 |
+
"parameters": {
|
| 21 |
+
"max_length": 1000,
|
| 22 |
+
"temperature": 0.7,
|
| 23 |
+
"do_sample": True
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
response = requests.post(url, headers=headers, json=data, timeout=30)
|
| 28 |
+
|
| 29 |
+
if response.status_code == 200:
|
| 30 |
+
result = response.json()
|
| 31 |
+
if isinstance(result, list) and len(result) > 0:
|
| 32 |
+
return result[0].get("generated_text", "")
|
| 33 |
+
return None
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"API call error: {e}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
def generate_worksheet_content(topic, subject, grade_level, difficulty, content_type, content_length):
|
| 39 |
+
"""Generate educational worksheet content"""
|
| 40 |
+
|
| 41 |
+
# Try Hugging Face API first if token is available
|
| 42 |
+
if HF_TOKEN:
|
| 43 |
+
try:
|
| 44 |
+
prompt = f"""Create a comprehensive educational {content_type.lower()} about "{topic}" for {grade_level} students studying {subject}.
|
| 45 |
+
|
| 46 |
+
Requirements:
|
| 47 |
+
- Grade Level: {grade_level}
|
| 48 |
+
- Subject: {subject}
|
| 49 |
+
- Topic: {topic}
|
| 50 |
+
- Difficulty: {difficulty}
|
| 51 |
+
- Content Type: {content_type}
|
| 52 |
+
- Length: {content_length}
|
| 53 |
+
|
| 54 |
+
IMPORTANT: DO NOT USE LaTeX expressions. Use simple HTML/markdown formatting instead:
|
| 55 |
+
- For fractions: use "a/b" or "a over b" instead of \frac{{a}}{{b}}
|
| 56 |
+
- For math: use plain text like "Speed = Distance/Time" instead of \text{{Speed}} = \frac{{\text{{Distance}}}}{{\text{{Time}}}}
|
| 57 |
+
- For fill-in-blanks: use "_____" instead of \_\_\_
|
| 58 |
+
- For answer boxes: use "[ANSWER: ___]" instead of \boxed{{\_\_}}
|
| 59 |
+
- For subscripts: use "H2O" instead of H_2O
|
| 60 |
+
- For superscripts: use "xΒ²" instead of x^2
|
| 61 |
+
|
| 62 |
+
Format the response as a structured worksheet with clear sections, examples, and exercises."""
|
| 63 |
+
|
| 64 |
+
result = call_hugging_face_api_content(prompt)
|
| 65 |
+
if result:
|
| 66 |
+
return result
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"API error: {e}")
|
| 69 |
+
|
| 70 |
+
# Fallback content with LaTeX-free formatting
|
| 71 |
+
fallback_content = f"""# **{topic} in {subject}: Interactive Worksheet**
|
| 72 |
+
**Grade Level:** {grade_level} | **Subject:** {subject} | **Difficulty:** {difficulty}
|
| 73 |
+
|
| 74 |
+
---
|
| 75 |
+
|
| 76 |
+
## **Introduction**
|
| 77 |
+
{topic} helps students understand important concepts in {subject}! In this worksheet, you'll explore {topic} through hands-on activities and real-world examples.
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## **Learning Objectives**
|
| 82 |
+
By the end of this worksheet, you will be able to:
|
| 83 |
+
β Understand the basic concepts of {topic}
|
| 84 |
+
β Apply {topic} to solve problems
|
| 85 |
+
β Use {topic} in real-world situations
|
| 86 |
+
β Check your work using self-assessment
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## **Practice Problems**
|
| 91 |
+
|
| 92 |
+
### **1. Basic Concepts**
|
| 93 |
+
Solve the following problems step by step.
|
| 94 |
+
|
| 95 |
+
**Example:** If we have the equation: A = B Γ C
|
| 96 |
+
Find A when B = 5 and C = 3
|
| 97 |
+
[ANSWER: A = 15]
|
| 98 |
+
|
| 99 |
+
**Now you try:**
|
| 100 |
+
a) If D = E + F, find D when E = 10 and F = 7
|
| 101 |
+
[ANSWER: _____]
|
| 102 |
+
|
| 103 |
+
b) If G = H / I, find G when H = 20 and I = 4
|
| 104 |
+
[ANSWER: _____]
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
## **Word Problems**
|
| 109 |
+
|
| 110 |
+
### **2. Real-World Applications**
|
| 111 |
+
Read each problem carefully and solve.
|
| 112 |
+
|
| 113 |
+
**Problem 1:** A student travels 120 miles in 2 hours. What is their speed?
|
| 114 |
+
Equation: Speed = Distance / Time
|
| 115 |
+
Solution: Speed = _____ / _____ = _____ miles per hour
|
| 116 |
+
[ANSWER: 60 mph]
|
| 117 |
+
|
| 118 |
+
**Problem 2:** A container holds 50 liters of water. If 30 liters are used, how much remains?
|
| 119 |
+
Equation: Remaining = Total - Used
|
| 120 |
+
Solution: Remaining = _____ - _____ = _____ liters
|
| 121 |
+
[ANSWER: 20 liters]
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## **Self-Assessment**
|
| 126 |
+
Rate your understanding:
|
| 127 |
+
β I need more practice
|
| 128 |
+
β I understand most problems
|
| 129 |
+
β I can teach this to someone else!
|
| 130 |
+
|
| 131 |
+
**Reflection:** What was the most challenging part?
|
| 132 |
+
[ANSWER: ________________________________]
|
| 133 |
+
|
| 134 |
+
---
|
| 135 |
+
|
| 136 |
+
## **Answer Key**
|
| 137 |
+
**Practice Problems:**
|
| 138 |
+
a) D = 17
|
| 139 |
+
b) G = 5
|
| 140 |
+
|
| 141 |
+
**Word Problems:**
|
| 142 |
+
1. Speed = 60 mph
|
| 143 |
+
2. Remaining = 20 liters
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
|
| 147 |
+
### **Worksheet Summary**
|
| 148 |
+
Great job! You've practiced {topic} concepts and applied them to real-world problems. Keep practicing to master these skills!
|
| 149 |
+
|
| 150 |
+
---
|
| 151 |
+
**Total Words:** ~800 | **Format:** Interactive Worksheet
|
| 152 |
+
**Ready for Print/Digital Use!** β
"""
|
| 153 |
+
|
| 154 |
+
return fallback_content
|
| 155 |
+
|
| 156 |
+
def apply_simple_styling(content):
|
| 157 |
+
"""Apply simple styling to make content look better"""
|
| 158 |
+
# Style answer boxes
|
| 159 |
+
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)
|
| 160 |
+
|
| 161 |
+
# Style fill-in-blank spaces
|
| 162 |
+
content = re.sub(r'___+', r'<span style="border-bottom: 2px solid #3498db; min-width: 40px; display: inline-block; margin: 0 5px;"> </span>', content)
|
| 163 |
+
|
| 164 |
+
# Style checkboxes
|
| 165 |
+
content = re.sub(r'β', r'<input type="checkbox" style="margin-right: 8px; transform: scale(1.2);">', content)
|
| 166 |
+
content = re.sub(r'β', r'<input type="checkbox" checked style="margin-right: 8px; transform: scale(1.2);">', content)
|
| 167 |
+
|
| 168 |
+
return content
|
| 169 |
+
|
| 170 |
+
def generate_worksheet(topic, subject, grade_level, difficulty, content_type, content_length):
|
| 171 |
+
"""Main function to generate and style worksheet content"""
|
| 172 |
+
content = generate_worksheet_content(topic, subject, grade_level, difficulty, content_type, content_length)
|
| 173 |
+
styled_content = apply_simple_styling(content)
|
| 174 |
+
return styled_content
|
| 175 |
+
|
| 176 |
+
# Create the Gradio interface
|
| 177 |
+
with gr.Blocks(
|
| 178 |
+
title="BrightMind AI - Educational Worksheet Generator",
|
| 179 |
+
theme=gr.themes.Soft(),
|
| 180 |
+
css="""
|
| 181 |
+
.gradio-container {
|
| 182 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 183 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 184 |
+
min-height: 100vh;
|
| 185 |
+
}
|
| 186 |
+
.main-container {
|
| 187 |
+
background: white;
|
| 188 |
+
border-radius: 12px;
|
| 189 |
+
padding: 20px;
|
| 190 |
+
margin: 20px;
|
| 191 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 192 |
+
}
|
| 193 |
+
.worksheet-output {
|
| 194 |
+
background: #f8f9fa;
|
| 195 |
+
border-radius: 8px;
|
| 196 |
+
padding: 20px;
|
| 197 |
+
margin-top: 20px;
|
| 198 |
+
border-left: 4px solid #3498db;
|
| 199 |
+
}
|
| 200 |
+
"""
|
| 201 |
+
) as demo:
|
| 202 |
+
|
| 203 |
+
gr.Markdown("""
|
| 204 |
+
# π§ BrightMind AI
|
| 205 |
+
## Professional Educational Content Generator for Teachers
|
| 206 |
+
|
| 207 |
+
Generate interactive worksheets tailored to your specific needs. Simply fill in the details below and click "Generate Worksheet" to create engaging educational materials.
|
| 208 |
+
""")
|
| 209 |
+
|
| 210 |
+
with gr.Row():
|
| 211 |
+
with gr.Column(scale=1):
|
| 212 |
+
gr.Markdown("### π Worksheet Settings")
|
| 213 |
+
|
| 214 |
+
topic = gr.Textbox(
|
| 215 |
+
label="π Topic",
|
| 216 |
+
placeholder="e.g., algebra, photosynthesis, world war II",
|
| 217 |
+
value="algebra"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
subject = gr.Dropdown(
|
| 221 |
+
label="π Subject",
|
| 222 |
+
choices=["Science", "Mathematics", "History", "English", "Geography", "Art", "Music", "Physical Education"],
|
| 223 |
+
value="Science"
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
grade_level = gr.Dropdown(
|
| 227 |
+
label="π Grade Level",
|
| 228 |
+
choices=["K-2", "3-5", "6-8", "9-12"],
|
| 229 |
+
value="6-8"
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
difficulty = gr.Dropdown(
|
| 233 |
+
label="π― Difficulty",
|
| 234 |
+
choices=["Beginner", "Intermediate", "Advanced"],
|
| 235 |
+
value="Intermediate"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
content_type = gr.Dropdown(
|
| 239 |
+
label="π Content Type",
|
| 240 |
+
choices=["Worksheets", "Handouts", "Study Guides", "Activities", "Presentations", "Lesson Materials"],
|
| 241 |
+
value="Worksheets"
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
content_length = gr.Dropdown(
|
| 245 |
+
label="π Length",
|
| 246 |
+
choices=["Short (1-2 pages)", "Medium (3-5 pages)", "Long (6+ pages)"],
|
| 247 |
+
value="Medium (3-5 pages)"
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
generate_btn = gr.Button(
|
| 251 |
+
"π Generate Worksheet",
|
| 252 |
+
variant="primary",
|
| 253 |
+
size="lg"
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
clear_btn = gr.Button(
|
| 257 |
+
"ποΈ Clear",
|
| 258 |
+
variant="secondary"
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
with gr.Column(scale=2):
|
| 262 |
+
gr.Markdown("### π Generated Worksheet")
|
| 263 |
+
|
| 264 |
+
worksheet_output = gr.HTML(
|
| 265 |
+
value="<div style='text-align: center; color: #666; font-style: italic; padding: 40px;'>Your educational worksheet will appear here...</div>",
|
| 266 |
+
label=""
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
# Event handlers
|
| 270 |
+
generate_btn.click(
|
| 271 |
+
fn=generate_worksheet,
|
| 272 |
+
inputs=[topic, subject, grade_level, difficulty, content_type, content_length],
|
| 273 |
+
outputs=worksheet_output
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
clear_btn.click(
|
| 277 |
+
fn=lambda: "<div style='text-align: center; color: #666; font-style: italic; padding: 40px;'>Your educational worksheet will appear here...</div>",
|
| 278 |
+
outputs=worksheet_output
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
if __name__ == "__main__":
|
| 282 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
gradio>=4.
|
| 2 |
-
requests>=2.
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
requests>=2.25.0
|