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| 3 |
+
import json
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "dispatchAI/Llama-3.2-1B-Instruct-Q4-mobile"
|
| 8 |
+
|
| 9 |
+
tokenizer = None
|
| 10 |
+
model = None
|
| 11 |
+
|
| 12 |
+
def load_model():
|
| 13 |
+
global tokenizer, model
|
| 14 |
+
if tokenizer is None:
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
MODEL_ID,
|
| 18 |
+
torch_dtype=torch.float16,
|
| 19 |
+
device_map="auto",
|
| 20 |
+
)
|
| 21 |
+
return tokenizer, model
|
| 22 |
+
|
| 23 |
+
@spaces.GPU
|
| 24 |
+
def agent_respond(task: str, history: list) -> str:
|
| 25 |
+
"""A mobile-optimized agent that can answer questions, write code, and solve tasks.
|
| 26 |
+
|
| 27 |
+
Powered by dispatchAI/Llama-3.2-1B-Instruct-Q4-mobile β a 1B parameter model
|
| 28 |
+
quantized to Q4, designed to run on phones. This proves real agents can run
|
| 29 |
+
on pocket-sized models.
|
| 30 |
+
"""
|
| 31 |
+
tokenizer, model = load_model()
|
| 32 |
+
|
| 33 |
+
messages = [{"role": "system", "content": "You are a helpful mobile AI assistant. You are running on a 1B parameter model optimized for phones. Be concise and helpful."}]
|
| 34 |
+
for h in history:
|
| 35 |
+
messages.append({"role": "user", "content": h[0]})
|
| 36 |
+
if h[1]:
|
| 37 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 38 |
+
messages.append({"role": "user", "content": task})
|
| 39 |
+
|
| 40 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 41 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
| 42 |
+
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
outputs = model.generate(
|
| 45 |
+
**inputs,
|
| 46 |
+
max_new_tokens=256,
|
| 47 |
+
temperature=0.7,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 53 |
+
return response
|
| 54 |
+
|
| 55 |
+
@spaces.GPU
|
| 56 |
+
def agent_code(instruction: str) -> str:
|
| 57 |
+
"""Generate code using a mobile-optimized model."""
|
| 58 |
+
tokenizer, model = load_model()
|
| 59 |
+
|
| 60 |
+
prompt = f"Write Python code for: {instruction}\n\n```python\n"
|
| 61 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 62 |
+
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
outputs = model.generate(
|
| 65 |
+
**inputs,
|
| 66 |
+
max_new_tokens=200,
|
| 67 |
+
temperature=0.3,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 73 |
+
# Extract code block
|
| 74 |
+
if "```python" in code:
|
| 75 |
+
code = code.split("```python")[1].split("```")[0]
|
| 76 |
+
return code.strip()
|
| 77 |
+
|
| 78 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="dispatchAI Mobile Agent") as demo:
|
| 79 |
+
gr.Markdown("""
|
| 80 |
+
# π€ dispatchAI Mobile Agent
|
| 81 |
+
|
| 82 |
+
**A real AI agent running on a 1B parameter model β small enough for your pocket.**
|
| 83 |
+
|
| 84 |
+
Model: [Llama-3.2-1B-Instruct-Q4-mobile](https://huggingface.co/dispatchAI/Llama-3.2-1B-Instruct-Q4-mobile)
|
| 85 |
+
|
| 86 |
+
This agent runs on a model quantized to Q4 (700MB file size), designed to run on
|
| 87 |
+
Snapdragon 865 phones. It can answer questions, write code, and solve tasks β
|
| 88 |
+
all on a model 1/100th the size of GPT-4.
|
| 89 |
+
|
| 90 |
+
## Try It
|
| 91 |
+
|
| 92 |
+
- **Chat**: Ask the agent anything
|
| 93 |
+
- **Code**: Ask it to write Python code
|
| 94 |
+
|
| 95 |
+
## The Point
|
| 96 |
+
|
| 97 |
+
This isn't about matching GPT-4. It's about proving that a 1B model on a phone
|
| 98 |
+
can be genuinely useful. For the tasks people actually do on phones β quick answers,
|
| 99 |
+
code snippets, summaries, classifications β a 1B model is enough.
|
| 100 |
+
""")
|
| 101 |
+
|
| 102 |
+
with gr.Tab("π¬ Chat"):
|
| 103 |
+
chat = gr.ChatInterface(
|
| 104 |
+
fn=agent_respond,
|
| 105 |
+
title="Chat with a 1B Mobile Agent",
|
| 106 |
+
description="Powered by Llama-3.2-1B-Instruct-Q4-mobile (700MB)",
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with gr.Tab("π¨βπ» Code"):
|
| 110 |
+
code_input = gr.Textbox(label="What should I code?", placeholder="A function that reverses a string")
|
| 111 |
+
code_btn = gr.Button("Generate Code", variant="primary")
|
| 112 |
+
code_output = gr.Code(label="Generated Code", language="python")
|
| 113 |
+
code_btn.click(fn=agent_code, inputs=code_input, outputs=code_output)
|
| 114 |
+
|
| 115 |
+
with gr.Tab("βΉοΈ About"):
|
| 116 |
+
gr.Markdown("""
|
| 117 |
+
## How This Works
|
| 118 |
+
|
| 119 |
+
This Space runs a **1 billion parameter Llama-3.2 model** quantized to 4-bit.
|
| 120 |
+
|
| 121 |
+
| Metric | Value |
|
| 122 |
+
|--------|-------|
|
| 123 |
+
| Model | Llama-3.2-1B-Instruct |
|
| 124 |
+
| Params | 1B |
|
| 125 |
+
| Quantization | Q4 (4-bit) |
|
| 126 |
+
| File size | 700MB |
|
| 127 |
+
| RAM needed | ~1.1GB |
|
| 128 |
+
| Speed on Snapdragon 865 | ~18 tokens/sec |
|
| 129 |
+
| Speed on this Space (ZeroGPU) | Faster |
|
| 130 |
+
|
| 131 |
+
## Run This On Your Phone
|
| 132 |
+
|
| 133 |
+
```bash
|
| 134 |
+
# Download the GGUF
|
| 135 |
+
hf download dispatchAI/Llama-3.2-1B-Instruct-Q4-mobile model.gguf
|
| 136 |
+
|
| 137 |
+
# Run with llama.cpp
|
| 138 |
+
llama-cli -m model.gguf -p "Hello!" -n 100 -t 4
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
## The Thesis
|
| 142 |
+
|
| 143 |
+
> A 1B model on a phone is not a compromise. It's a victory.
|
| 144 |
+
|
| 145 |
+
6.8 billion smartphones. Most can't run a cloud LLM. But they CAN run a 1B model
|
| 146 |
+
at 18 tokens/sec. That's fast enough for real-time chat, code completion,
|
| 147 |
+
summarization, and classification.
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
π [dispatchAI](https://huggingface.co/dispatchAI) β Small. Mobile. Free. UAE-built.
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
demo.launch(mcp_server=True)
|