Spaces:
Runtime error
Runtime error
File size: 1,277 Bytes
bea4be2 ae4f2b7 894438d ae4f2b7 2b7d460 0a15bed 31d5547 9928aed 06fd1b7 136175a 06fd1b7 ae4f2b7 9928aed 1d39818 31d5547 136175a 31d5547 136175a bea4be2 136175a | 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 | import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "google/gemma-3-1b-it"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float32,
low_cpu_mem_usage=True,
device_map=“cpu”
)
model.eval()
def predict(message, history):
messages = []
for turn in history:
messages.append({“role”: “user”, “content”: turn[0]})
messages.append({“role”: “assistant”, “content”: turn[1]})
messages.append({“role”: “user”, “content”: message[-1000:]})
```
tokenized = tokenizer.apply_chat_template(
messages,
return_tensors="pt",
add_generation_prompt=True
)
input_ids = tokenized.to("cpu")
with torch.no_grad():
output = model.generate(
input_ids=input_ids,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
top_p=0.9,
use_cache=True
)
new_tokens = output[0][input_ids.shape[-1]:]
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
```
demo = gr.ChatInterface(
fn=predict,
title=“Gemma 3 1B (CPU)”,
description=“google/gemma-3-1b-it — runs on HF free tier CPU (~4GB RAM)”
)
if **name** == “**main**”:
demo.launch() |