File size: 1,315 Bytes
0d7b4d9 16b39b1 |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load model & tokenizer
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
print("Model loaded.")
# Global state
chat_history_ids = None
chat_step = 0
# Chat function
def respond(message, history=[]):
global chat_history_ids, chat_step
# Encode user input
new_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
# Append to chat history
bot_input_ids = (
torch.cat([chat_history_ids, new_input_ids], dim=-1)
if chat_step > 0 else new_input_ids
)
# Generate response
chat_history_ids = model.generate(
bot_input_ids,
max_new_tokens=500,
pad_token_id=tokenizer.eos_token_id,
do_sample=True,
top_k=50,
top_p=0.95,
temperature=0.8,
)
# Decode only the newly generated part
reply = tokenizer.decode(
chat_history_ids[:, bot_input_ids.shape[-1]:][0],
skip_special_tokens=True
)
chat_step += 1
return reply
# Launch Gradio interface
gr.ChatInterface(fn=respond, title="🧠 SmolLM Chatbot").launch(share=True) |