Spaces:
Runtime error
Runtime error
File size: 1,772 Bytes
af88c48 | 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 | import gradio as gr
import spaces
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
MODEL_ID = "dispatchAI/SmolLM2-135M-Instruct-mobile"
tokenizer = None
model = None
def load_model():
global tokenizer, model
if tokenizer is None:
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16,
device_map="auto",
)
return tokenizer, model
@spaces.GPU
def chat(message, history):
tokenizer, model = load_model()
messages = [{"role": "system", "content": "You are a helpful assistant running on a mobile-optimized model."}]
for h in history:
messages.append({"role": "user", "content": h[0]})
messages.append({"role": "assistant", "content": h[1]})
messages.append({"role": "user", "content": message})
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.7,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
return response
demo = gr.ChatInterface(
fn=chat,
title="🚀 dispatchAI Mobile Chat",
description="Chat with dispatchAI/SmolLM2-135M-Instruct-mobile — a 135M parameter model optimized for mobile devices. This runs on ZeroGPU.",
theme=gr.themes.Soft(primary_hue="blue"),
)
if __name__ == "__main__":
demo.launch()
|