Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def generate_response(message, history):
|
| 8 |
-
#
|
| 9 |
-
system_prompt = "
|
| 10 |
|
| 11 |
-
# Build
|
| 12 |
-
|
| 13 |
for user_msg, assistant_msg in history:
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# Create the Chat Interface
|
| 33 |
demo = gr.ChatInterface(
|
| 34 |
fn=generate_response,
|
| 35 |
-
title="
|
| 36 |
-
description="SmolLM2 135M
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
+
import torch
|
| 4 |
+
from threading import Thread
|
| 5 |
|
| 6 |
+
# Load model and tokenizer properly for streaming
|
| 7 |
+
model_id = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
|
| 11 |
def generate_response(message, history):
|
| 12 |
+
# Strict system prompt to keep it grounded
|
| 13 |
+
system_prompt = "You are a helpful, very brief assistant. Do not imagine stories or contexts. Answer only what is asked."
|
| 14 |
|
| 15 |
+
# Build chat format
|
| 16 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 17 |
for user_msg, assistant_msg in history:
|
| 18 |
+
messages.append({"role": "user", "content": user_msg})
|
| 19 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 20 |
+
messages.append({"role": "user", "content": message})
|
| 21 |
|
| 22 |
+
# Convert to model's specific format
|
| 23 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 24 |
+
inputs = tokenizer([input_text], return_tensors="pt")
|
| 25 |
|
| 26 |
+
# Set up the streamer
|
| 27 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 28 |
+
|
| 29 |
+
# Run generation in a separate thread
|
| 30 |
+
generation_kwargs = dict(
|
| 31 |
+
inputs,
|
| 32 |
+
streamer=streamer,
|
| 33 |
+
max_new_tokens=150, # Keep responses short to prevent yapping
|
| 34 |
+
temperature=0.3, # Low temp = more "sane"
|
| 35 |
+
repetition_penalty=1.2,
|
| 36 |
+
do_sample=True
|
| 37 |
)
|
| 38 |
|
| 39 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 40 |
+
thread.start()
|
| 41 |
+
|
| 42 |
+
# Yield the text as it comes in
|
| 43 |
+
partial_text = ""
|
| 44 |
+
for new_text in streamer:
|
| 45 |
+
partial_text += new_text
|
| 46 |
+
yield partial_text
|
| 47 |
|
|
|
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
fn=generate_response,
|
| 50 |
+
title="Actually Fast AI",
|
| 51 |
+
description="SmolLM2 135M with Streaming. No more imaginary stories!"
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|