lsadouk1111 commited on
Commit
5c6f553
Β·
verified Β·
1 Parent(s): a06c0cb

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +199 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VisualStep β€” ADHD Classroom Instruction Decomposer
3
+ Gradio demo app for Hugging Face Spaces
4
+ """
5
+
6
+ import json
7
+ import gradio as gr
8
+ import torch
9
+ from unsloth import FastModel
10
+ import spaces
11
+
12
+
13
+ # ── Model loading ─────────────────────────────────────────────────────────────
14
+ MODEL_ID = "lsadouk1111/VisualStep"
15
+
16
+ print("Loading VisualStep model...")
17
+ model, tokenizer = FastModel.from_pretrained(
18
+ model_name=MODEL_ID,
19
+ max_seq_length=512,
20
+ load_in_4bit=True,
21
+ dtype=None,
22
+ )
23
+ FastModel.for_inference(model)
24
+ model.eval()
25
+ print("Model ready.")
26
+
27
+ # ── System prompt ─────────────────────────────────────────────────────────────
28
+ SYSTEM_PROMPT = """You are an expert in ADHD classroom accommodations for primary school children aged 5-10.
29
+ Transform the teacher's spoken classroom instruction into an ADHD-adapted visual step card.
30
+
31
+ Rules:
32
+ - Maximum 5 steps
33
+ - Each step: one concrete physical action only
34
+ - Each action: maximum 6 words
35
+ - Start each step with an imperative verb
36
+ - Include a pictogram keyword in the icon field
37
+ - Output JSON only, no other text
38
+
39
+ Output format:
40
+ {"steps":[{"id":1,"action":"Open your book","detail":"page 23","icon":"book","check":true}],"n_steps":1,"support_level":"medium"}
41
+
42
+ support_level: "light" (1-2 steps), "medium" (3-4 steps), "high" (5 steps)"""
43
+
44
+ # ── Icon to emoji ──────────────────────────────────────────────────────────────
45
+ EMOJI = {
46
+ "book": "πŸ“–", "page": "πŸ“„", "pencil": "✏️", "write": "✏️",
47
+ "read": "πŸ‘οΈ", "eye": "πŸ‘οΈ", "hand": "βœ‹", "finger": "πŸ‘†",
48
+ "talk": "πŸ—£οΈ", "listen": "πŸ‘‚", "think": "🧠", "look": "πŸ‘οΈ",
49
+ "draw": "🎨", "paper": "πŸ“", "notebook": "πŸ““", "board": "πŸ“‹",
50
+ "number": "πŸ”’", "count": "πŸ”’", "answer": "βœ…", "sort": "πŸ—‚οΈ",
51
+ "match": "πŸ”—", "circle": "β­•", "plant": "🌱", "seed": "🌰",
52
+ "water": "πŸ’§", "animal": "🐾", "picture": "πŸ–ΌοΈ", "partner": "πŸ‘₯",
53
+ "find": "πŸ”", "search": "πŸ”", "observe": "πŸ”­", "measure": "πŸ“",
54
+ "record": "πŸ“Š", "label": "🏷️", "colour": "πŸ–ŒοΈ", "color": "πŸ–ŒοΈ",
55
+ "cut": "βœ‚οΈ", "touch": "πŸ‘†", "quiet": "🀫", "ready": "βœ…",
56
+ "open": "πŸ“‚", "put": "πŸ“Œ", "place": "πŸ“Œ", "sit": "πŸͺ‘",
57
+ "science": "πŸ”¬", "maths": "βž•", "english": "πŸ“š",
58
+ }
59
+
60
+ def get_emoji(icon):
61
+ if not icon:
62
+ return "πŸ“Œ"
63
+ k = str(icon).lower()
64
+ for key, em in EMOJI.items():
65
+ if key in k:
66
+ return em
67
+ return "πŸ“Œ"
68
+
69
+ # ── Generation ────────────────────────────────────────────────────────────────
70
+ @spaces.GPU
71
+ def generate_step_card(instruction, year, subject):
72
+ if not instruction.strip():
73
+ return "Please enter a classroom instruction."
74
+
75
+ messages = [
76
+ {"role": "system", "content": SYSTEM_PROMPT},
77
+ {"role": "user", "content": f"Year: {year} | Subject: {subject} | Instruction: {instruction}"},
78
+ ]
79
+ prompt = tokenizer.apply_chat_template(
80
+ messages, tokenize=False, add_generation_prompt=True
81
+ )
82
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
83
+
84
+ with torch.no_grad():
85
+ outputs = model.generate(
86
+ **inputs,
87
+ max_new_tokens=256,
88
+ do_sample=False,
89
+ pad_token_id=tokenizer.eos_token_id,
90
+ )
91
+ generated = outputs[0][inputs["input_ids"].shape[1]:]
92
+ raw = tokenizer.decode(generated, skip_special_tokens=True).strip()
93
+ raw = raw.replace("```json", "").replace("```", "").strip()
94
+
95
+ try:
96
+ data = json.loads(raw)
97
+ steps = data.get("steps", [])
98
+ support = data.get("support_level", "medium")
99
+ n = data.get("n_steps", len(steps))
100
+
101
+ # Build visual card
102
+ lines = ["## πŸ“Œ WHAT TO DO NOW\n"]
103
+ for s in steps:
104
+ emoji = get_emoji(s.get("icon", ""))
105
+ action = s.get("action", "")
106
+ detail = s.get("detail", "")
107
+ line = f"☐ **{s.get('id', '')}.** {emoji} {action}"
108
+ if detail:
109
+ line += f" β†’ *{detail}*"
110
+ lines.append(line)
111
+
112
+ lines.append(f"\n---")
113
+ lines.append(f"*Support level: {support} Β· {n} step(s)*")
114
+
115
+ return "\n\n".join(lines)
116
+
117
+ except json.JSONDecodeError:
118
+ return f"⚠️ Model output could not be parsed as JSON.\n\nRaw output:\n```\n{raw}\n```"
119
+
120
+ # ── Gradio interface ────────────────────────────────────────────────────────���──
121
+ EXAMPLES = [
122
+ ["Ok everyone, open your books to page 23, read the paragraph quietly, then answer questions 1 to 3 in your notebook and put your hand up when you're done.", "2", "english"],
123
+ ["Right, so, um, can we all take our science books and turn to page 10? And then, after that, I want you to look at the picture and think about what you see.", "1", "science"],
124
+ ["Everyone stop what you're doing, pack away your things, and line up quietly at the door please.", "3", "routine"],
125
+ ["I want you to sort these animals into mammals and not mammals using the sorting hoops, then write one sentence explaining how you decided.", "1", "science"],
126
+ ["OK so, find your maths book, open to page 15, do the first three problems and check your answers with your partner.", "2", "maths"],
127
+ ]
128
+
129
+ with gr.Blocks(
130
+ title="VisualStep β€” ADHD Instruction Decomposer",
131
+ theme=gr.themes.Soft(primary_hue="blue"),
132
+ css="""
133
+ .card-output { font-size: 1.1em; line-height: 1.8; }
134
+ h1 { color: #1F4E79; }
135
+ .subtitle { color: #666; font-size: 0.95em; margin-top: -10px; }
136
+ """
137
+ ) as demo:
138
+
139
+ gr.Markdown("""
140
+ # VisualStep
141
+ ### ADHD-Adapted Visual Instruction Decomposition for Primary School Classrooms
142
+
143
+ *Fine-tuned Phi-3 Mini (3.8B) Β· Lamyaa Sadouk Β· EMSI Casablanca*
144
+
145
+ ---
146
+
147
+ Enter a classroom instruction as a teacher would say it out loud.
148
+ VisualStep will decompose it into a structured, ADHD-adapted visual step card.
149
+ """)
150
+
151
+ with gr.Row():
152
+ with gr.Column(scale=1):
153
+ instruction = gr.Textbox(
154
+ label="Teacher's spoken instruction",
155
+ placeholder="e.g. Ok everyone, open your books to page 23, read the paragraph quietly, then answer questions 1 to 3...",
156
+ lines=4,
157
+ )
158
+ with gr.Row():
159
+ year = gr.Dropdown(
160
+ choices=["1", "2", "3", "4", "5", "6"],
161
+ value="2",
162
+ label="Year group",
163
+ )
164
+ subject = gr.Dropdown(
165
+ choices=["english", "maths", "science", "routine", "transition"],
166
+ value="english",
167
+ label="Subject",
168
+ )
169
+ btn = gr.Button("Generate Step Card", variant="primary", size="lg")
170
+
171
+ with gr.Column(scale=1):
172
+ output = gr.Markdown(
173
+ label="ADHD-adapted step card",
174
+ elem_classes=["card-output"],
175
+ value="*Your step card will appear here.*"
176
+ )
177
+
178
+ btn.click(
179
+ fn=generate_step_card,
180
+ inputs=[instruction, year, subject],
181
+ outputs=output,
182
+ )
183
+
184
+ gr.Examples(
185
+ examples=EXAMPLES,
186
+ inputs=[instruction, year, subject],
187
+ label="Try these examples",
188
+ )
189
+
190
+ gr.Markdown("""
191
+ ---
192
+ **About VisualStep:**
193
+ This demo accompanies the paper *"VisualStep: A Fine-Tuned Small Language Model for ADHD-Adapted
194
+ Visual Instruction Decomposition in Primary School Classrooms"*.
195
+ The model was fine-tuned on VisualStep-2K, a dataset of 2,000 spoken classroom instruction–step card pairs.
196
+ All inference runs locally β€” no data is sent to external servers.
197
+ """)
198
+
199
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ spaces
2
+ unsloth
3
+ torch
4
+ gradio
5
+ transformers
6
+ peft
7
+ bitsandbytes
8
+ accelerate