Spaces:
Sleeping
Sleeping
File size: 8,746 Bytes
d46efc1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
"""SLM Function Calling - Gradio App for HuggingFace Spaces."""
import os
import gradio as gr
from inferencer import Inferencer
# Configuration
REPO_ID = os.environ.get("HF_MODEL_REPO", "suyash94/gpt2-fc-adapter")
LOCAL_DIR = os.environ.get("LOCAL_CHECKPOINT_DIR", None)
BASE_MODEL = os.environ.get("BASE_MODEL", "gpt2")
# Initialize inferencer (loads model on startup)
print("=" * 60)
print("SLM Function Calling - Car Control Demo")
print("=" * 60)
if LOCAL_DIR:
print(f"Loading from local: {LOCAL_DIR}")
inferencer = Inferencer(local_dir=LOCAL_DIR, base_model=BASE_MODEL)
else:
print(f"Loading from HuggingFace Hub: {REPO_ID}")
inferencer = Inferencer(repo_id=REPO_ID, base_model=BASE_MODEL)
print("Model ready!")
def predict_function_call(command: str) -> tuple[str, dict]:
"""Predict function call from user command.
:param command: User's natural language command
:return: Tuple of (raw response, parsed function call dict)
"""
if not command or not command.strip():
return "", {"info": "Please enter a command"}
result = inferencer.predict(command.strip())
raw_response = result["response"]
parsed = result["parsed"]
return raw_response, parsed
# Example commands covering all 18 functions
EXAMPLE_COMMANDS = [
# Climate Control
["Set the temperature to 22 degrees for the driver"],
["Turn up the heat"],
["Set the fan to high"],
# Comfort
["Move my seat forward"],
["Close all the windows"],
# Wipers & Defroster
["Set wipers to medium"],
["Speed up the wipers"],
["Turn on the defroster for 15 minutes"],
# Engine & Doors
["Start the engine"],
["Lock all the doors"],
# Entertainment
["Play some jazz music at volume 7"],
# Navigation
["Navigate to Central Park, New York"],
# Lighting
["Turn on the headlights"],
["Set ambient lighting to blue at intensity 8"],
# Driving
["Set cruise control to 80"],
["Activate sport mode"],
# Maintenance
["Check the battery health with history"],
]
# Main description (left column)
LEFT_DESCRIPTION = """
# SLM Function Calling
A **small language model (GPT-2, 124M params)** fine-tuned to convert natural language commands into structured function calls for car control.
## What This Model Does
Given a command like *"Set the temperature to 22 degrees"*, outputs:
```json
{"fn_name": "set_temperature", "properties": {"temperature": 22}}
```
## Available Functions (18 Total)
| Category | Functions |
|----------|-----------|
| **Climate** | `set_temperature`, `adjust_temperature`, `set_fan_speed`, `adjust_fan_speed` |
| **Comfort** | `adjust_seat`, `control_window` |
| **Wipers** | `set_wiper_speed`, `adjust_wiper_speed`, `activate_defroster` |
| **Engine** | `start_engine`, `lock_doors` |
| **Media** | `play_music` |
| **Nav** | `set_navigation_destination` |
| **Lights** | `toggle_headlights`, `control_ambient_lighting` |
| **Driving** | `set_cruise_control`, `toggle_sport_mode` |
| **Maintenance** | `check_battery_health` |
## Example Commands
- *"Set the temperature to 25 degrees"*
- *"Turn up the heat"* / *"Make it cooler"*
- *"Move my seat forward"*
- *"Open all windows"*
- *"Turn on the wipers"*
- *"Start the car remotely"*
- *"Lock the doors"*
- *"Play jazz at volume 7"*
- *"Navigate to Central Park"*
- *"Turn on headlights"*
- *"Set ambient lighting to blue"*
- *"Set cruise control to 80"*
- *"Activate sport mode"*
- *"Check battery health"*
"""
FUNCTION_REFERENCE = """
## Function Reference
### Climate Control
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `set_temperature` | Set temperature in a zone | `temperature` (1-80) | `area` (driver/front-passenger/rear-right/rear-left), `unit` (Celsius/Fahrenheit) |
| `adjust_temperature` | Increase/decrease temperature | `action` (increase/decrease) | `area` |
| `set_fan_speed` | Set fan to specific level | `speed` (LOW/MEDIUM/HIGH) | `area` |
| `adjust_fan_speed` | Increase/decrease fan speed | `speed` (increase/decrease) | `area` |
### Comfort
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `adjust_seat` | Adjust seat position | `position` (forward/backward/up/down/tilt-forward/tilt-backward) | `seat_type` (driver/front-passenger/rear_right/rear_left) |
| `control_window` | Open/close windows | `window_position` (open/close) | `window_location` (driver/front-passenger/rear_right/rear_left) |
### Wipers & Defroster
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `set_wiper_speed` | Set wiper speed | `speed` (LOW/MEDIUM/HIGH) | - |
| `adjust_wiper_speed` | Increase/decrease wipers | `speed` (INCREASE/DECREASE) | - |
| `activate_defroster` | Activate window defroster | - | `defroster_zone` (front/rear/all), `duration_minutes` (1-30) |
### Engine & Security
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `start_engine` | Start the car's engine | - | `method` (remote/keyless/keyed) |
| `lock_doors` | Lock/unlock car doors | `lock_state` (lock/unlock) | - |
### Entertainment
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `play_music` | Control music player | - | `track` (song name), `volume` (1-10) |
### Navigation
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `set_navigation_destination` | Set GPS destination | `destination` (address/location) | - |
### Lighting
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `toggle_headlights` | Turn headlights on/off | `light_state` (on/off) | - |
| `control_ambient_lighting` | Set interior lighting | `color` (warm/red/blue/dark/white) | `intensity` (1-10) |
### Driving
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `set_cruise_control` | Set cruise control speed | `speed` (10-150 km/h) | - |
| `toggle_sport_mode` | Activate/deactivate sport mode | `action` (activate/deactivate) | - |
### Maintenance
| Function | Description | Required Parameters | Optional Parameters |
|----------|-------------|---------------------|---------------------|
| `check_battery_health` | Check battery status | - | `include_history` (true/false) |
"""
# Build Gradio interface
with gr.Blocks(
title="SLM Function Calling - Car Control",
) as demo:
with gr.Row():
# Left column: Description
with gr.Column(scale=1):
gr.Markdown(LEFT_DESCRIPTION)
# Right column: Demo
with gr.Column(scale=1):
gr.Markdown("## Try It Out")
command_input = gr.Textbox(
label="Your Command",
placeholder="e.g., Set the temperature to 22 degrees",
lines=2,
)
predict_btn = gr.Button("Predict Function Call", variant="primary")
raw_output = gr.Textbox(
label="Raw Model Output",
lines=3,
interactive=False,
)
parsed_output = gr.JSON(
label="Parsed Function Call",
)
gr.Examples(
examples=EXAMPLE_COMMANDS,
inputs=[command_input],
label="Example Commands",
)
# Event handlers
predict_btn.click(
fn=predict_function_call,
inputs=[command_input],
outputs=[raw_output, parsed_output],
)
command_input.submit(
fn=predict_function_call,
inputs=[command_input],
outputs=[raw_output, parsed_output],
)
# Function reference accordion
gr.Markdown("---")
with gr.Accordion("Function Reference (All 18 Functions)", open=False):
gr.Markdown(FUNCTION_REFERENCE)
gr.Markdown(
"""
---
**Source Code:** [GitHub Repository](https://github.com/suyash94/slm-function-calling)
**Limitations:**
- Only trained on car control domain commands
- May produce incorrect outputs for ambiguous or out-of-domain queries
- Best results with clear, specific commands
"""
)
if __name__ == "__main__":
# share=True creates a public URL (works for 72 hours)
demo.launch(share=True)
|