Commit ·
fd0b9df
0
Parent(s):
Initial release of MathAnimaux 🐾
Browse filesKids math app with FLUX.1-schnell cute animal rewards.
- .gitignore +5 -0
- README.md +41 -0
- app.py +242 -0
- image_generator.py +127 -0
- math_engine.py +61 -0
- requirements.txt +9 -0
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
.env
|
| 5 |
+
.DS_Store
|
README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: MathAnimaux
|
| 3 |
+
emoji: 🐾
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "4.40.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
tags:
|
| 12 |
+
- flux
|
| 13 |
+
- text-to-image
|
| 14 |
+
- kids
|
| 15 |
+
- education
|
| 16 |
+
- math
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# 🐾 MathAnimaux
|
| 20 |
+
|
| 21 |
+
Une application de calcul mental pour les enfants, avec des récompenses sous forme d'images d'animaux générées par l'IA !
|
| 22 |
+
|
| 23 |
+
## Comment ça marche ?
|
| 24 |
+
|
| 25 |
+
1. Entre ton prénom
|
| 26 |
+
2. Réponds aux questions de calcul mental
|
| 27 |
+
3. Toutes les **3 bonnes réponses**, tu gagnes une image d'un animal trop mignon généré par IA !
|
| 28 |
+
4. Monte de niveau : Additions → Soustractions → Multiplications → Mélange
|
| 29 |
+
|
| 30 |
+
## Niveaux
|
| 31 |
+
|
| 32 |
+
| Niveau | Opération | Objectif |
|
| 33 |
+
|--------|-----------|----------|
|
| 34 |
+
| 1 | ➕ Additions | 5 bonnes réponses |
|
| 35 |
+
| 2 | ➖ Soustractions | 5 bonnes réponses |
|
| 36 |
+
| 3 | ✖️ Multiplications | 5 bonnes réponses |
|
| 37 |
+
| 4 | 🎲 Mélange | Mode expert infini |
|
| 38 |
+
|
| 39 |
+
## Modèle utilisé
|
| 40 |
+
|
| 41 |
+
Images générées avec **FLUX.1-schnell** (12B paramètres, Apache 2.0) via 🤗 Diffusers.
|
app.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
🐾 MathAnimaux — Application de calcul mental avec récompenses mignonnes !
|
| 3 |
+
Pour Léa et Zoé 🌟
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from math_engine import generate_question, LEVEL_NAMES, LEVEL_THRESHOLDS, level_up_message
|
| 8 |
+
from image_generator import generate_reward_image
|
| 9 |
+
|
| 10 |
+
# ---------------------------------------------------------------------------
|
| 11 |
+
# How many correct answers trigger a reward image
|
| 12 |
+
# ---------------------------------------------------------------------------
|
| 13 |
+
REWARD_EVERY = 3
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# ---------------------------------------------------------------------------
|
| 17 |
+
# Core game logic
|
| 18 |
+
# ---------------------------------------------------------------------------
|
| 19 |
+
|
| 20 |
+
def start_game(player_name: str, state: dict):
|
| 21 |
+
"""Initialize or reset the game for a player."""
|
| 22 |
+
name = player_name.strip() or "Champion(ne)"
|
| 23 |
+
state = {
|
| 24 |
+
"name": name,
|
| 25 |
+
"level": 1,
|
| 26 |
+
"score": 0,
|
| 27 |
+
"streak": 0,
|
| 28 |
+
"correct_since_reward": 0,
|
| 29 |
+
"level_correct": 0,
|
| 30 |
+
"question": "",
|
| 31 |
+
"answer": 0,
|
| 32 |
+
"waiting_next": False,
|
| 33 |
+
}
|
| 34 |
+
question, answer = generate_question(state["level"])
|
| 35 |
+
state["question"] = question
|
| 36 |
+
state["answer"] = answer
|
| 37 |
+
|
| 38 |
+
status = _status_text(state)
|
| 39 |
+
return (
|
| 40 |
+
state,
|
| 41 |
+
gr.update(visible=False), # hide welcome panel
|
| 42 |
+
gr.update(visible=True), # show game panel
|
| 43 |
+
f"Bonjour **{name}** ! C'est parti 🚀",
|
| 44 |
+
status,
|
| 45 |
+
f"## {question} = ?",
|
| 46 |
+
"", # clear input
|
| 47 |
+
"", # clear feedback
|
| 48 |
+
None, # clear image
|
| 49 |
+
gr.update(visible=False), # hide reward panel
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def check_answer(user_input: str, state: dict):
|
| 54 |
+
"""Validate the answer and update game state."""
|
| 55 |
+
if not state or state.get("waiting_next"):
|
| 56 |
+
return (state,) + _no_change_outputs(state)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
user_answer = int(user_input.strip())
|
| 60 |
+
except ValueError:
|
| 61 |
+
return (
|
| 62 |
+
state,
|
| 63 |
+
_status_text(state),
|
| 64 |
+
f"## {state['question']} = ?",
|
| 65 |
+
"",
|
| 66 |
+
"⚠️ Écris un nombre entier !",
|
| 67 |
+
None,
|
| 68 |
+
gr.update(visible=False),
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
correct = user_answer == state["answer"]
|
| 72 |
+
|
| 73 |
+
if correct:
|
| 74 |
+
state["score"] += 1
|
| 75 |
+
state["streak"] += 1
|
| 76 |
+
state["correct_since_reward"] += 1
|
| 77 |
+
state["level_correct"] += 1
|
| 78 |
+
|
| 79 |
+
# Check level-up
|
| 80 |
+
level_msg = ""
|
| 81 |
+
leveled_up = False
|
| 82 |
+
threshold = LEVEL_THRESHOLDS.get(state["level"], 999)
|
| 83 |
+
if state["level_correct"] >= threshold and state["level"] < 4:
|
| 84 |
+
state["level"] += 1
|
| 85 |
+
state["level_correct"] = 0
|
| 86 |
+
level_msg = level_up_message(state["level"])
|
| 87 |
+
leveled_up = True
|
| 88 |
+
|
| 89 |
+
# Check reward
|
| 90 |
+
reward_image = None
|
| 91 |
+
reward_visible = False
|
| 92 |
+
if state["correct_since_reward"] >= REWARD_EVERY:
|
| 93 |
+
state["correct_since_reward"] = 0
|
| 94 |
+
reward_image, _ = generate_reward_image(state["streak"])
|
| 95 |
+
reward_visible = True
|
| 96 |
+
|
| 97 |
+
# Next question
|
| 98 |
+
question, answer = generate_question(state["level"])
|
| 99 |
+
state["question"] = question
|
| 100 |
+
state["answer"] = answer
|
| 101 |
+
|
| 102 |
+
streak_emoji = "🔥" * min(state["streak"], 5)
|
| 103 |
+
if leveled_up:
|
| 104 |
+
feedback = f"✅ Correct ! {level_msg}"
|
| 105 |
+
else:
|
| 106 |
+
feedback = f"✅ Bravo ! {streak_emoji}" if state["streak"] > 1 else "✅ Correct !"
|
| 107 |
+
|
| 108 |
+
return (
|
| 109 |
+
state,
|
| 110 |
+
_status_text(state),
|
| 111 |
+
f"## {question} = ?",
|
| 112 |
+
"",
|
| 113 |
+
feedback,
|
| 114 |
+
reward_image,
|
| 115 |
+
gr.update(visible=reward_visible),
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
else:
|
| 119 |
+
state["streak"] = 0
|
| 120 |
+
feedback = f"❌ Raté ! La réponse était **{state['answer']}**. Essaie encore !"
|
| 121 |
+
# New question after wrong answer
|
| 122 |
+
question, answer = generate_question(state["level"])
|
| 123 |
+
state["question"] = question
|
| 124 |
+
state["answer"] = answer
|
| 125 |
+
return (
|
| 126 |
+
state,
|
| 127 |
+
_status_text(state),
|
| 128 |
+
f"## {question} = ?",
|
| 129 |
+
"",
|
| 130 |
+
feedback,
|
| 131 |
+
None,
|
| 132 |
+
gr.update(visible=False),
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def _status_text(state: dict) -> str:
|
| 137 |
+
if not state:
|
| 138 |
+
return ""
|
| 139 |
+
level_name = LEVEL_NAMES.get(state["level"], "")
|
| 140 |
+
return (
|
| 141 |
+
f"👤 {state['name']} | "
|
| 142 |
+
f"⭐ Score : {state['score']} | "
|
| 143 |
+
f"📚 Niveau : {level_name}"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def _no_change_outputs(state):
|
| 148 |
+
return (
|
| 149 |
+
_status_text(state),
|
| 150 |
+
f"## {state.get('question', '')} = ?",
|
| 151 |
+
"",
|
| 152 |
+
"",
|
| 153 |
+
None,
|
| 154 |
+
gr.update(visible=False),
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# ---------------------------------------------------------------------------
|
| 159 |
+
# Gradio UI
|
| 160 |
+
# ---------------------------------------------------------------------------
|
| 161 |
+
|
| 162 |
+
CSS = """
|
| 163 |
+
#title { text-align: center; font-size: 2em; margin-bottom: 0.2em; }
|
| 164 |
+
#subtitle { text-align: center; color: #888; margin-bottom: 1em; }
|
| 165 |
+
#question-box { text-align: center; font-size: 2.5em; font-weight: bold; padding: 0.5em; }
|
| 166 |
+
#feedback-box { text-align: center; font-size: 1.3em; min-height: 2em; }
|
| 167 |
+
#status-box { text-align: center; padding: 0.4em; background: #f9f0ff; border-radius: 8px; }
|
| 168 |
+
#reward-img { border-radius: 16px; }
|
| 169 |
+
.answer-input input { font-size: 2em !important; text-align: center !important; }
|
| 170 |
+
"""
|
| 171 |
+
|
| 172 |
+
with gr.Blocks(title="🐾 MathAnimaux", theme=gr.themes.Soft(primary_hue="purple", secondary_hue="pink")) as demo:
|
| 173 |
+
|
| 174 |
+
state = gr.State({})
|
| 175 |
+
|
| 176 |
+
# ---- Header ----
|
| 177 |
+
gr.Markdown("# 🐾 MathAnimaux", elem_id="title")
|
| 178 |
+
gr.Markdown("*Calcule bien et gagne des images d'animaux trop mignons !*", elem_id="subtitle")
|
| 179 |
+
|
| 180 |
+
# ---- Welcome panel ----
|
| 181 |
+
with gr.Group(visible=True) as welcome_panel:
|
| 182 |
+
gr.Markdown("### 👋 Qui es-tu ?")
|
| 183 |
+
player_name = gr.Textbox(
|
| 184 |
+
placeholder="Écris ton prénom…",
|
| 185 |
+
label="Ton prénom",
|
| 186 |
+
max_lines=1,
|
| 187 |
+
)
|
| 188 |
+
start_btn = gr.Button("🚀 Commencer !", variant="primary", size="lg")
|
| 189 |
+
|
| 190 |
+
# ---- Game panel ----
|
| 191 |
+
with gr.Group(visible=False) as game_panel:
|
| 192 |
+
hello_msg = gr.Markdown("")
|
| 193 |
+
status_md = gr.Markdown("", elem_id="status-box", container=True)
|
| 194 |
+
|
| 195 |
+
question_md = gr.Markdown("", elem_id="question-box")
|
| 196 |
+
|
| 197 |
+
answer_input = gr.Textbox(
|
| 198 |
+
placeholder="Ta réponse…",
|
| 199 |
+
label="Ta réponse",
|
| 200 |
+
max_lines=1,
|
| 201 |
+
elem_classes=["answer-input"],
|
| 202 |
+
)
|
| 203 |
+
submit_btn = gr.Button("✔️ Valider", variant="primary")
|
| 204 |
+
|
| 205 |
+
feedback_md = gr.Markdown("", elem_id="feedback-box")
|
| 206 |
+
|
| 207 |
+
# ---- Reward panel ----
|
| 208 |
+
with gr.Group(visible=False) as reward_panel:
|
| 209 |
+
gr.Markdown("### 🎁 Bravo ! Voici ta récompense !")
|
| 210 |
+
reward_image = gr.Image(
|
| 211 |
+
label="",
|
| 212 |
+
show_label=False,
|
| 213 |
+
elem_id="reward-img",
|
| 214 |
+
height=400,
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# ---- Restart button (always visible once game started) ----
|
| 218 |
+
restart_btn = gr.Button("🔄 Recommencer", variant="secondary", size="sm")
|
| 219 |
+
|
| 220 |
+
# ---- Wiring ----
|
| 221 |
+
start_outputs = [
|
| 222 |
+
state, welcome_panel, game_panel,
|
| 223 |
+
hello_msg, status_md, question_md,
|
| 224 |
+
answer_input, feedback_md, reward_image, reward_panel,
|
| 225 |
+
]
|
| 226 |
+
start_btn.click(start_game, inputs=[player_name, state], outputs=start_outputs)
|
| 227 |
+
player_name.submit(start_game, inputs=[player_name, state], outputs=start_outputs)
|
| 228 |
+
|
| 229 |
+
check_outputs = [
|
| 230 |
+
state, status_md, question_md, answer_input, feedback_md, reward_image, reward_panel,
|
| 231 |
+
]
|
| 232 |
+
submit_btn.click(check_answer, inputs=[answer_input, state], outputs=check_outputs)
|
| 233 |
+
answer_input.submit(check_answer, inputs=[answer_input, state], outputs=check_outputs)
|
| 234 |
+
|
| 235 |
+
restart_btn.click(
|
| 236 |
+
lambda: (gr.update(visible=True), gr.update(visible=False), {}, "", ""),
|
| 237 |
+
outputs=[welcome_panel, game_panel, state, player_name, hello_msg],
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
if __name__ == "__main__":
|
| 242 |
+
demo.launch(css=CSS)
|
image_generator.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Patch for torch < 2.4 which lacks torch.xpu (required by diffusers >= 0.30)
|
| 6 |
+
if not hasattr(torch, "xpu"):
|
| 7 |
+
class _MockXPU:
|
| 8 |
+
is_available = staticmethod(lambda: False)
|
| 9 |
+
device_count = staticmethod(lambda: 0)
|
| 10 |
+
empty_cache = staticmethod(lambda: None)
|
| 11 |
+
manual_seed = staticmethod(lambda seed: None)
|
| 12 |
+
reset_peak_memory_stats = staticmethod(lambda: None)
|
| 13 |
+
max_memory_allocated = staticmethod(lambda: 0)
|
| 14 |
+
synchronize = staticmethod(lambda: None)
|
| 15 |
+
torch.xpu = _MockXPU()
|
| 16 |
+
|
| 17 |
+
# ---------------------------------------------------------------------------
|
| 18 |
+
# Detect if running on HuggingFace Spaces with ZeroGPU
|
| 19 |
+
# ---------------------------------------------------------------------------
|
| 20 |
+
|
| 21 |
+
IS_HF_SPACE = os.environ.get("SPACE_ID") is not None
|
| 22 |
+
|
| 23 |
+
if IS_HF_SPACE:
|
| 24 |
+
import spaces
|
| 25 |
+
|
| 26 |
+
# ---------------------------------------------------------------------------
|
| 27 |
+
# Animal reward prompts — escalate with streak
|
| 28 |
+
# ---------------------------------------------------------------------------
|
| 29 |
+
|
| 30 |
+
ANIMALS = [
|
| 31 |
+
"bunny", "kitten", "puppy", "baby panda", "duckling",
|
| 32 |
+
"baby fox", "hedgehog", "baby penguin", "baby koala",
|
| 33 |
+
"baby deer", "hamster", "baby owl", "baby elephant",
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
STYLES = [
|
| 37 |
+
"kawaii digital art",
|
| 38 |
+
"soft watercolor illustration",
|
| 39 |
+
"cute cartoon style",
|
| 40 |
+
"pastel chibi illustration",
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
def build_prompt(streak: int) -> str:
|
| 44 |
+
animal = random.choice(ANIMALS)
|
| 45 |
+
style = random.choice(STYLES)
|
| 46 |
+
|
| 47 |
+
if streak <= 2:
|
| 48 |
+
mood = "cute"
|
| 49 |
+
extras = "with big sparkling eyes, soft pastel colors"
|
| 50 |
+
elif streak <= 5:
|
| 51 |
+
mood = "super cute and happy"
|
| 52 |
+
extras = "with big sparkling eyes, glitter, pastel rainbow colors, smiling"
|
| 53 |
+
else:
|
| 54 |
+
mood = "magically adorable, ultra fluffy"
|
| 55 |
+
extras = (
|
| 56 |
+
"with big sparkling eyes, magical sparkles, rainbow aura, "
|
| 57 |
+
"tiny crown, pastel colors, looking amazed"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
prompt = (
|
| 61 |
+
f"A {mood} {animal}, {style}, {extras}, "
|
| 62 |
+
"white background, children's book illustration style, "
|
| 63 |
+
"high quality, detailed"
|
| 64 |
+
)
|
| 65 |
+
return prompt
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# ---------------------------------------------------------------------------
|
| 69 |
+
# Pipeline loader (cached so we only load once)
|
| 70 |
+
# ---------------------------------------------------------------------------
|
| 71 |
+
|
| 72 |
+
_pipe = None
|
| 73 |
+
|
| 74 |
+
def get_pipeline():
|
| 75 |
+
global _pipe
|
| 76 |
+
if _pipe is not None:
|
| 77 |
+
return _pipe
|
| 78 |
+
|
| 79 |
+
from diffusers import FluxPipeline
|
| 80 |
+
|
| 81 |
+
print("Loading FLUX.1-schnell pipeline...")
|
| 82 |
+
_pipe = FluxPipeline.from_pretrained(
|
| 83 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 84 |
+
torch_dtype=torch.bfloat16,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Device selection: CUDA > MPS (Apple Silicon) > CPU
|
| 88 |
+
if torch.cuda.is_available():
|
| 89 |
+
_pipe = _pipe.to("cuda")
|
| 90 |
+
elif torch.backends.mps.is_available():
|
| 91 |
+
_pipe = _pipe.to("mps")
|
| 92 |
+
else:
|
| 93 |
+
# CPU fallback — slow but works
|
| 94 |
+
_pipe.enable_model_cpu_offload()
|
| 95 |
+
|
| 96 |
+
print("Pipeline ready.")
|
| 97 |
+
return _pipe
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# ---------------------------------------------------------------------------
|
| 101 |
+
# Main generation function
|
| 102 |
+
# Two versions: one with @spaces.GPU for HF, one plain for local
|
| 103 |
+
# ---------------------------------------------------------------------------
|
| 104 |
+
|
| 105 |
+
def _generate(streak: int):
|
| 106 |
+
pipe = get_pipeline()
|
| 107 |
+
prompt = build_prompt(streak)
|
| 108 |
+
print(f"Generating image | streak={streak} | prompt: {prompt}")
|
| 109 |
+
result = pipe(
|
| 110 |
+
prompt=prompt,
|
| 111 |
+
num_inference_steps=4, # schnell is optimised for 1-4 steps
|
| 112 |
+
guidance_scale=0.0, # schnell doesn't use CFG
|
| 113 |
+
height=512,
|
| 114 |
+
width=512,
|
| 115 |
+
)
|
| 116 |
+
return result.images[0], prompt
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
if IS_HF_SPACE:
|
| 120 |
+
@spaces.GPU(duration=60)
|
| 121 |
+
def generate_reward_image(streak: int):
|
| 122 |
+
"""Generate a cute animal image as a reward (HF Spaces / ZeroGPU)."""
|
| 123 |
+
return _generate(streak)
|
| 124 |
+
else:
|
| 125 |
+
def generate_reward_image(streak: int):
|
| 126 |
+
"""Generate a cute animal image as a reward (local)."""
|
| 127 |
+
return _generate(streak)
|
math_engine.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
# Levels definition: (operation_label, generator_function)
|
| 4 |
+
# Targeting ages 9-10 (French CE2/CM1 curriculum)
|
| 5 |
+
|
| 6 |
+
def generate_question(level: int) -> tuple[str, int]:
|
| 7 |
+
"""
|
| 8 |
+
Returns (question_string, correct_answer) for the given level.
|
| 9 |
+
Levels:
|
| 10 |
+
1 - Addition (numbers 1-30)
|
| 11 |
+
2 - Subtraction (result always positive, numbers 1-30)
|
| 12 |
+
3 - Multiplication tables (2 to 9)
|
| 13 |
+
4 - Mixed bag (all of the above)
|
| 14 |
+
"""
|
| 15 |
+
if level == 1:
|
| 16 |
+
a = random.randint(1, 30)
|
| 17 |
+
b = random.randint(1, 30)
|
| 18 |
+
return f"{a} + {b}", a + b
|
| 19 |
+
|
| 20 |
+
elif level == 2:
|
| 21 |
+
a = random.randint(1, 30)
|
| 22 |
+
b = random.randint(1, a) # ensure positive result
|
| 23 |
+
return f"{a} - {b}", a - b
|
| 24 |
+
|
| 25 |
+
elif level == 3:
|
| 26 |
+
a = random.randint(2, 9)
|
| 27 |
+
b = random.randint(2, 9)
|
| 28 |
+
return f"{a} × {b}", a * b
|
| 29 |
+
|
| 30 |
+
else: # level 4 — mixed
|
| 31 |
+
op = random.choice(["add", "sub", "mul"])
|
| 32 |
+
if op == "add":
|
| 33 |
+
a = random.randint(10, 50)
|
| 34 |
+
b = random.randint(10, 50)
|
| 35 |
+
return f"{a} + {b}", a + b
|
| 36 |
+
elif op == "sub":
|
| 37 |
+
a = random.randint(10, 50)
|
| 38 |
+
b = random.randint(1, a)
|
| 39 |
+
return f"{a} - {b}", a - b
|
| 40 |
+
else:
|
| 41 |
+
a = random.randint(2, 12)
|
| 42 |
+
b = random.randint(2, 12)
|
| 43 |
+
return f"{a} × {b}", a * b
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
LEVEL_NAMES = {
|
| 47 |
+
1: "➕ Additions",
|
| 48 |
+
2: "➖ Soustractions",
|
| 49 |
+
3: "✖️ Multiplications",
|
| 50 |
+
4: "🎲 Mélange",
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
LEVEL_THRESHOLDS = {1: 5, 2: 5, 3: 5, 4: 999} # correct answers needed to level up
|
| 54 |
+
|
| 55 |
+
def level_up_message(level: int) -> str:
|
| 56 |
+
messages = {
|
| 57 |
+
2: "🎉 Bravo ! Tu passes aux soustractions !",
|
| 58 |
+
3: "🌟 Incroyable ! Place aux multiplications !",
|
| 59 |
+
4: "🏆 Tu es un(e) champion(ne) ! Mode expert activé !",
|
| 60 |
+
}
|
| 61 |
+
return messages.get(level, "")
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.40.0
|
| 2 |
+
diffusers>=0.30.0
|
| 3 |
+
transformers>=4.44.0
|
| 4 |
+
accelerate>=0.33.0
|
| 5 |
+
torch>=2.2.0
|
| 6 |
+
numpy<2
|
| 7 |
+
sentencepiece
|
| 8 |
+
protobuf
|
| 9 |
+
spaces
|