Spaces:
Sleeping
Update app.py
Browse filesKey Functionality and Components
The application is a combined mechanical calculator and a Hugging Face LLM inference engine deployed via Gradio.
1. Gear Dimension Calculator (gear_calc)
Purpose: Takes standard gear inputs—Number of Teeth (N), Diametral Pitch (Pd in teeth/inch), and Pressure Angle (ϕ in degrees)—and applies common mechanical engineering formulas (e.g., for Pitch Diameter, Base Diameter, Addendum, Dedendum, etc.).
Output: Returns a comprehensive dictionary of over 18 key gear dimensions, calculated and provided in both millimeters (mm) and inches (in).
2. Large Language Model (LLM) Integration
Model: Uses the HuggingFaceTB/SmolLM2-135M-Instruct model, a small, fast, instruction-following model suitable for low-latency tasks.
Inference: The transformers pipeline is initialized with deterministic settings (do_sample=False, temperature=0.0) to ensure the LLM provides the most compliant, non-creative, and factual response possible.
Recommendation (llm_explain): This function extracts key size parameters (Pitch Diameter and Circular Pitch) from the calculation results and uses a highly restrictive system prompt to force the LLM to output EXACTLY ONE CONCISE SENTENCE suggesting a general machinery category (e.g., "precision instrumentation," "heavy-duty power transmission").
3. Gradio Interface
Inputs: Three numerical input fields for the required gear parameters.
Process: The run_once function acts as the main entry point, handling input validation, running the calculation, formatting the numerical results into a Pandas DataFrame for a clean tabular display, and generating the LLM-based narrative.
Outputs: Displays the complete table of calculated dimensions and the LLM-generated single-sentence use case recommendation below it. The interface also includes pre-set examples for testing.
This tool demonstrates a multi-purpose application where traditional engineering computation is augmented by the interpretive and summarization capabilities of a small LLM.
|
@@ -132,37 +132,70 @@ def _llm_generate(prompt: str, max_tokens: int) -> str:
|
|
| 132 |
|
| 133 |
def llm_explain(results: dict, inputs: list) -> str:
|
| 134 |
"""
|
| 135 |
-
Generates a
|
| 136 |
-
|
| 137 |
-
to prevent rambling or lists.
|
| 138 |
"""
|
| 139 |
-
|
| 140 |
-
circular_pitch_in = results['circular_pitch_in']
|
| 141 |
pitch_diameter_in = results['pitch_diameter_in']
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
system_prompt = (
|
| 145 |
-
"You are a mechanical engineer
|
| 146 |
-
"
|
| 147 |
-
"
|
| 148 |
-
"
|
|
|
|
|
|
|
| 149 |
)
|
| 150 |
|
| 151 |
-
#
|
| 152 |
user_prompt = (
|
| 153 |
-
f"
|
| 154 |
-
f"
|
| 155 |
-
"
|
|
|
|
| 156 |
)
|
| 157 |
|
| 158 |
-
# Apply the chat template and generate text with a low token limit for brevity
|
| 159 |
formatted = _format_chat(system_prompt, user_prompt)
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
| 163 |
if not explanation:
|
| 164 |
-
explanation =
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
return explanation
|
| 167 |
|
| 168 |
|
|
|
|
| 132 |
|
| 133 |
def llm_explain(results: dict, inputs: list) -> str:
|
| 134 |
"""
|
| 135 |
+
Generates a clear, customized explanation for a non-technical user
|
| 136 |
+
based on the calculated gear dimensions.
|
|
|
|
| 137 |
"""
|
| 138 |
+
|
|
|
|
| 139 |
pitch_diameter_in = results['pitch_diameter_in']
|
| 140 |
+
tooth_thickness_in = results['tooth_thickness_in']
|
| 141 |
+
circular_pitch_in = results['circular_pitch_in']
|
| 142 |
|
| 143 |
+
# Estimate scale
|
| 144 |
+
if pitch_diameter_in < 2:
|
| 145 |
+
scale = "small"
|
| 146 |
+
examples = "clocks, cameras, or precision instruments"
|
| 147 |
+
elif pitch_diameter_in < 10:
|
| 148 |
+
scale = "medium"
|
| 149 |
+
examples = "bicycles, sewing machines, or automotive gearboxes"
|
| 150 |
+
else:
|
| 151 |
+
scale = "large"
|
| 152 |
+
examples = "wind turbines, cranes, or heavy mining machinery"
|
| 153 |
+
|
| 154 |
+
# Interpret tooth strength
|
| 155 |
+
if tooth_thickness_in < 0.05:
|
| 156 |
+
strength = "delicate and precise, best for very light loads"
|
| 157 |
+
elif tooth_thickness_in < 0.3:
|
| 158 |
+
strength = "balanced between precision and strength"
|
| 159 |
+
else:
|
| 160 |
+
strength = "very strong, made for heavy-duty work"
|
| 161 |
+
|
| 162 |
+
# Interpret circular pitch (spacing)
|
| 163 |
+
if circular_pitch_in < 0.2:
|
| 164 |
+
spacing = "fine and closely spaced for smooth, quiet motion"
|
| 165 |
+
elif circular_pitch_in < 1:
|
| 166 |
+
spacing = "moderate spacing for reliable general use"
|
| 167 |
+
else:
|
| 168 |
+
spacing = "wide spacing, rugged enough to handle shocks and dirt"
|
| 169 |
+
|
| 170 |
+
# System prompt
|
| 171 |
system_prompt = (
|
| 172 |
+
"You are a mechanical engineer explaining gears to a non-technical user. "
|
| 173 |
+
"Write in 2–4 friendly sentences. "
|
| 174 |
+
"Explain what the size suggests, what the tooth shape/spacing means in practice, "
|
| 175 |
+
"and give unique example applications that fit the gear’s scale. "
|
| 176 |
+
"Do not simply restate the numbers given. "
|
| 177 |
+
"Be specific and avoid repeating the same examples across different gear sizes."
|
| 178 |
)
|
| 179 |
|
| 180 |
+
# User prompt (gear context)
|
| 181 |
user_prompt = (
|
| 182 |
+
f"This is a {scale} gear. "
|
| 183 |
+
f"Its teeth are {strength}, and the spacing is {spacing}. "
|
| 184 |
+
f"Suggest everyday uses where a gear like this would make sense, such as {examples}. "
|
| 185 |
+
"Explain it in a way that feels useful and practical for someone who is not an engineer."
|
| 186 |
)
|
| 187 |
|
|
|
|
| 188 |
formatted = _format_chat(system_prompt, user_prompt)
|
| 189 |
+
|
| 190 |
+
explanation = _llm_generate(formatted, max_tokens=200)
|
| 191 |
+
|
| 192 |
+
# Fallback if the LLM fails
|
| 193 |
if not explanation:
|
| 194 |
+
explanation = (
|
| 195 |
+
f"This is a {scale} gear with {strength} and {spacing}. "
|
| 196 |
+
f"You’d expect to find gears like this in {examples}."
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
return explanation
|
| 200 |
|
| 201 |
|