lsadouk1111 commited on
Commit
4cb3c71
·
verified ·
1 Parent(s): b262f30

Upload 2 files

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