Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
device = "cpu" # Since we are on free tier CPU
|
| 8 |
|
| 9 |
-
tokenizer
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def chat(message, history):
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
for val in history:
|
| 17 |
-
if val[0]: messages.append({"role": "user", "content": val[0]})
|
| 18 |
-
if val[1]: messages.append({"role": "assistant", "content": val[1]})
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
return response
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
# Model ID for the stable Instruct version
|
| 6 |
+
MODEL_ID = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
|
|
|
| 7 |
|
| 8 |
+
# Load tokenizer and model once at startup
|
| 9 |
+
print("System: Booting Stable-Lite Brain...")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
device_map="cpu",
|
| 14 |
+
torch_dtype=torch.float32
|
| 15 |
+
)
|
| 16 |
|
| 17 |
def chat(message, history):
|
| 18 |
+
# Standard Instruct Format for SmolLM2
|
| 19 |
+
# 'Be helpful and precise' is the only instruction to save RAM/Attention
|
| 20 |
+
prompt = f"<|user|>\nBe helpful and precise: {message}<|endoftext|>\n<|assistant|>\n"
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 23 |
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model.generate(
|
| 26 |
+
**inputs,
|
| 27 |
+
max_new_tokens=150,
|
| 28 |
+
temperature=0.1,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
repetition_penalty=1.2,
|
| 31 |
+
eos_token_id=tokenizer.eos_token_id
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
# Extracting only the new tokens (the response)
|
| 35 |
+
input_length = inputs.input_ids.shape[1]
|
| 36 |
+
response_tokens = outputs[0][input_length:]
|
| 37 |
+
response = tokenizer.decode(response_tokens, skip_special_tokens=True)
|
| 38 |
|
| 39 |
+
return response.strip()
|
| 40 |
|
| 41 |
+
# Gradio Interface configured for Stability
|
| 42 |
+
demo = gr.ChatInterface(
|
| 43 |
+
fn=chat,
|
| 44 |
+
title="Smol-AI Kano (Stable-Lite)",
|
| 45 |
+
description="Optimized for local students and businesses on 4GB RAM devices.",
|
| 46 |
+
cache_examples=False # Prevents the Python 3.13 caching error
|
| 47 |
+
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
demo.launch()
|