suyash94's picture
Upload folder using huggingface_hub
d46efc1 verified
"""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)