Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,8 +3,6 @@ import gradio as gr
|
|
| 3 |
import pandas as pd
|
| 4 |
from typing import Dict, Any
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
-
from textwrap import dedent
|
| 7 |
-
|
| 8 |
|
| 9 |
LLM_ID = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(LLM_ID)
|
|
@@ -18,7 +16,7 @@ llm = pipeline(
|
|
| 18 |
def projectile_calc(v0_mps: float, theta_deg: float, y0_m: float, g: float, weight_kg: float) -> Dict[str, Any]:
|
| 19 |
errors = []
|
| 20 |
if not (0 < v0_mps <= 500): errors.append("Initial speed must be in (0, 500] m/s.")
|
| 21 |
-
if not (-10 <= theta_deg <= 90): errors.append("Launch angle must be between -10
|
| 22 |
if not (0 <= y0_m <= 1000): errors.append("Initial height must be in [0, 1000] m.")
|
| 23 |
if not (1 <= g <= 50): errors.append("Gravity must be in [1, 50] m/s^2.")
|
| 24 |
if not (0.05 <= weight_kg <= 50): errors.append("Ball weight must be in [0.05, 50] kg.")
|
|
@@ -64,9 +62,10 @@ def projectile_calc(v0_mps: float, theta_deg: float, y0_m: float, g: float, weig
|
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
-
|
| 68 |
-
"You
|
| 69 |
-
"
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
def llm_one_sentence(structured: Dict[str, Any]) -> str:
|
|
@@ -93,31 +92,23 @@ def llm_one_sentence(structured: Dict[str, Any]) -> str:
|
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
"Do not invent or rename fields; keep units as given; use ~2–3 sig figs."
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
# Show the exact sentence shape we want.
|
| 103 |
-
instruction = '''Use only these keys: inputs, derived, outputs.
|
| 104 |
-
Return exactly one sentence in this form (fill in the braces with the JSON values):
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
user_msg = "
|
| 111 |
-
"
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
])
|
| 117 |
|
| 118 |
-
# ✅ Use the tokenizer's chat template
|
| 119 |
messages = [
|
| 120 |
-
{"role": "system", "content":
|
| 121 |
{"role": "user", "content": instruction},
|
| 122 |
{"role": "user", "content": user_msg},
|
| 123 |
]
|
|
@@ -132,12 +123,12 @@ def llm_one_sentence(structured: Dict[str, Any]) -> str:
|
|
| 132 |
)
|
| 133 |
text = out[0]["generated_text"].strip()
|
| 134 |
|
| 135 |
-
#
|
| 136 |
if ("If you threw a ball" not in text) or (len(text.split()) < 6):
|
| 137 |
p = payload
|
| 138 |
text = (
|
| 139 |
f"If you threw a ball that weighed {p['inputs']['weight_kg']} kg at v0={p['inputs']['v0_mps']} m/s, "
|
| 140 |
-
f"
|
| 141 |
f"it would stay in the air for {p['outputs']['time_of_flight_s']} s, "
|
| 142 |
f"reach {p['outputs']['max_height_m']} m, travel {p['outputs']['range_m']} m, "
|
| 143 |
f"and impact at {p['outputs']['impact_speed_mps']} m/s."
|
|
@@ -172,9 +163,9 @@ with gr.Blocks(title="Projectile Motion — Deterministic + One-Sentence LLM") a
|
|
| 172 |
gr.Markdown("Deterministic projectile motion (no air resistance). See all inputs and derived values, plus a single, grounded sentence.")
|
| 173 |
|
| 174 |
with gr.Row():
|
| 175 |
-
v0 = gr.Slider(1, 200, value=30.0, step=0.5, label="Initial speed
|
| 176 |
-
theta = gr.Slider(-10, 90, value=45.0, step=0.5, label="Launch angle
|
| 177 |
-
y0 = gr.Slider(0, 20, value=1.5, step=0.1, label="Initial height
|
| 178 |
g = gr.Slider(5, 20, value=9.81, step=0.01, label="Gravity g [m/s^2]")
|
| 179 |
w = gr.Slider(0.05, 10.0, value=0.43, step=0.01, label="Ball weight [kg]")
|
| 180 |
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from typing import Dict, Any
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
|
|
|
| 6 |
|
| 7 |
LLM_ID = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(LLM_ID)
|
|
|
|
| 16 |
def projectile_calc(v0_mps: float, theta_deg: float, y0_m: float, g: float, weight_kg: float) -> Dict[str, Any]:
|
| 17 |
errors = []
|
| 18 |
if not (0 < v0_mps <= 500): errors.append("Initial speed must be in (0, 500] m/s.")
|
| 19 |
+
if not (-10 <= theta_deg <= 90): errors.append("Launch angle must be between -10 and 90 degrees.")
|
| 20 |
if not (0 <= y0_m <= 1000): errors.append("Initial height must be in [0, 1000] m.")
|
| 21 |
if not (1 <= g <= 50): errors.append("Gravity must be in [1, 50] m/s^2.")
|
| 22 |
if not (0.05 <= weight_kg <= 50): errors.append("Ball weight must be in [0.05, 50] kg.")
|
|
|
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
+
SYSTEM_MSG = (
|
| 66 |
+
"You are a careful technical writer. "
|
| 67 |
+
"Write EXACTLY ONE sentence using ONLY numbers provided in the JSON. "
|
| 68 |
+
"Do not invent or rename fields; keep units as given; use ~2–3 sig figs."
|
| 69 |
)
|
| 70 |
|
| 71 |
def llm_one_sentence(structured: Dict[str, Any]) -> str:
|
|
|
|
| 92 |
}
|
| 93 |
}
|
| 94 |
|
| 95 |
+
# Exact sentence shape we want.
|
| 96 |
+
instruction = """Use only these keys: inputs, derived, outputs.
|
| 97 |
+
Return exactly one sentence in this form (fill in the braces with the JSON values):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
If you threw a ball that weighed {weight_kg} kg at v0={v0_mps} m/s, theta={theta_deg} deg, from y0={y0_m} m under g={g_mps2} m/s^2,
|
| 100 |
+
it would stay in the air for {time_of_flight_s} s, reach {max_height_m} m, travel {range_m} m, and impact at {impact_speed_mps} m/s.
|
| 101 |
+
"""
|
| 102 |
|
| 103 |
+
user_msg = "\n".join([
|
| 104 |
+
"JSON:",
|
| 105 |
+
json.dumps(payload, indent=2),
|
| 106 |
+
"",
|
| 107 |
+
"Produce the single sentence as specified."
|
| 108 |
+
])
|
|
|
|
| 109 |
|
|
|
|
| 110 |
messages = [
|
| 111 |
+
{"role": "system", "content": SYSTEM_MSG},
|
| 112 |
{"role": "user", "content": instruction},
|
| 113 |
{"role": "user", "content": user_msg},
|
| 114 |
]
|
|
|
|
| 123 |
)
|
| 124 |
text = out[0]["generated_text"].strip()
|
| 125 |
|
| 126 |
+
# Fallback if the model strays
|
| 127 |
if ("If you threw a ball" not in text) or (len(text.split()) < 6):
|
| 128 |
p = payload
|
| 129 |
text = (
|
| 130 |
f"If you threw a ball that weighed {p['inputs']['weight_kg']} kg at v0={p['inputs']['v0_mps']} m/s, "
|
| 131 |
+
f"theta={p['inputs']['theta_deg']} deg, from y0={p['inputs']['y0_m']} m under g={p['inputs']['g_mps2']} m/s^2, "
|
| 132 |
f"it would stay in the air for {p['outputs']['time_of_flight_s']} s, "
|
| 133 |
f"reach {p['outputs']['max_height_m']} m, travel {p['outputs']['range_m']} m, "
|
| 134 |
f"and impact at {p['outputs']['impact_speed_mps']} m/s."
|
|
|
|
| 163 |
gr.Markdown("Deterministic projectile motion (no air resistance). See all inputs and derived values, plus a single, grounded sentence.")
|
| 164 |
|
| 165 |
with gr.Row():
|
| 166 |
+
v0 = gr.Slider(1, 200, value=30.0, step=0.5, label="Initial speed v0 [m/s]")
|
| 167 |
+
theta = gr.Slider(-10, 90, value=45.0, step=0.5, label="Launch angle theta [deg]")
|
| 168 |
+
y0 = gr.Slider(0, 20, value=1.5, step=0.1, label="Initial height y0 [m]")
|
| 169 |
g = gr.Slider(5, 20, value=9.81, step=0.01, label="Gravity g [m/s^2]")
|
| 170 |
w = gr.Slider(0.05, 10.0, value=0.43, step=0.01, label="Ball weight [kg]")
|
| 171 |
|