sgattup commited on
Commit
fff100c
Β·
verified Β·
1 Parent(s): ab9731b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Estella Explainer β€” HuggingFace Spaces Demo
3
+ A math reading coach for grades 3–8, based on Usable Math (usablemath.org)
4
+ Model: sgattup/EstellaExplainerLLM (Gemma 4 E4B fine-tuned)
5
+
6
+ Deploy to a ZeroGPU Space for free GPU inference.
7
+ """
8
+
9
+ import gradio as gr
10
+ import torch
11
+ from transformers import AutoModelForCausalLM, AutoTokenizer
12
+
13
+ MODEL_ID = "sgattup/EstellaExplainerLLM"
14
+
15
+ SYSTEM_PROMPT = """You are Estella Explainer, a math reading and language coach for young learners in grades 3 through 8. Your motto is: "My job is to explain math questions clearly so you know what you are supposed to do to solve the problem."
16
+
17
+ Your rules:
18
+ - Give only ONE hint per problem.
19
+ - Do NOT solve the problem or show the answer.
20
+ - Do NOT show calculation steps or compute anything.
21
+ - Help the student understand what the problem is ASKING.
22
+ - Use very simple words and short sentences (Flesch Reading Ease 90-100).
23
+ - Identify what is known and what is being asked.
24
+ - Use phrases like "We know...", "We are looking for...", or "Think about...".
25
+ - Be friendly and encouraging."""
26
+
27
+ EXAMPLES = [
28
+ "A rectangle has a length of 12 cm and a width of 5 cm. What is the area?",
29
+ "Sam had 3/4 of a pizza. He ate 1/4. How much is left?",
30
+ "A store sells apples for $0.75 each. Jake buys 6 apples. How much does he spend?",
31
+ "Round 4,567 to the nearest hundred.",
32
+ "There are 48 students put into equal groups of 6. How many groups are there?",
33
+ "A bag has 5 red marbles and 3 blue marbles. What fraction of the marbles are red?",
34
+ "A train leaves at 10:15 AM and arrives at 1:45 PM. How long is the trip?",
35
+ "The perimeter of a square is 36 inches. What is the length of one side?",
36
+ ]
37
+
38
+ # Load model once at startup
39
+ print(f"Loading {MODEL_ID}...")
40
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
41
+ model = AutoModelForCausalLM.from_pretrained(
42
+ MODEL_ID,
43
+ torch_dtype=torch.float16,
44
+ device_map="auto"
45
+ )
46
+ model.eval()
47
+ print("Model ready.")
48
+
49
+
50
+ def ask_estella(problem: str) -> str:
51
+ if not problem.strip():
52
+ return "Please type a math word problem above."
53
+
54
+ messages = [
55
+ {"role": "user", "content": SYSTEM_PROMPT + f"\n\n### Math Problem:\n{problem.strip()}\n\n### Estella's Hint:"}
56
+ ]
57
+ text = tokenizer.apply_chat_template(
58
+ messages,
59
+ add_generation_prompt=True,
60
+ tokenize=False
61
+ )
62
+ inputs = tokenizer(text=[text], return_tensors="pt")
63
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
64
+ input_len = inputs["input_ids"].shape[1]
65
+
66
+ with torch.no_grad():
67
+ outputs = model.generate(
68
+ **inputs,
69
+ max_new_tokens=150,
70
+ temperature=0.7,
71
+ do_sample=True,
72
+ repetition_penalty=1.1,
73
+ )
74
+
75
+ hint = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip()
76
+ return hint
77
+
78
+
79
+ # Gradio UI
80
+ with gr.Blocks(title="Estella Explainer β€” Math Reading Coach") as demo:
81
+ gr.Markdown("""
82
+ # πŸ“š Estella Explainer
83
+ ### A Math Reading Coach for Grades 3–8
84
+
85
+ *"My job is to explain math questions clearly so you know what you are supposed to do to solve the problem."*
86
+
87
+ Estella helps students **understand** what a math problem is asking β€” without solving it.
88
+ Based on [Usable Math](https://usablemath.org), an open educational resource from UMass Amherst.
89
+
90
+ ---
91
+ """)
92
+
93
+ with gr.Row():
94
+ with gr.Column(scale=2):
95
+ problem_input = gr.Textbox(
96
+ label="Math Word Problem",
97
+ placeholder="Type or paste a math problem here...",
98
+ lines=4
99
+ )
100
+ submit_btn = gr.Button("Ask Estella", variant="primary")
101
+
102
+ with gr.Column(scale=2):
103
+ hint_output = gr.Textbox(
104
+ label="Estella's Hint",
105
+ lines=4,
106
+ interactive=False
107
+ )
108
+
109
+ gr.Examples(
110
+ examples=EXAMPLES,
111
+ inputs=problem_input,
112
+ label="Try these examples"
113
+ )
114
+
115
+ gr.Markdown("""
116
+ ---
117
+ **Model:** [sgattup/EstellaExplainerLLM](https://huggingface.co/sgattup/EstellaExplainerLLM) Β· Fine-tuned Gemma 4 E4B via QLoRA
118
+ **Project:** [Usable Math](https://usablemath.org) Β· UMass Amherst College of Education
119
+ **Note:** Estella explains problems β€” she does not solve them or show answers.
120
+ """)
121
+
122
+ submit_btn.click(fn=ask_estella, inputs=problem_input, outputs=hint_output)
123
+ problem_input.submit(fn=ask_estella, inputs=problem_input, outputs=hint_output)
124
+
125
+ demo.launch()