-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
70 lines (53 loc) · 2.19 KB
/
chatbot.py
File metadata and controls
70 lines (53 loc) · 2.19 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
import torch
import streamlit as st
from tokenizers import Tokenizer
from config import config
from model import TransformerModel
from inference import sample_sequence, tokenize_input, decode_output
# Hack to prevent streamlit error
torch.classes.__path__ = []
@st.cache_resource
def load_model(config):
model = TransformerModel(config)
model = model.to(config.device)
model = torch.compile(model)
model.load_state_dict(torch.load(config.model_filename, weights_only=True, map_location=config.device))
return model
@st.cache_resource
def load_tokenizer(config):
return Tokenizer.from_file(config.tokenizer_filename)
# A simple streamlit chatbot
st.title("Cursed Chatbot")
# Load model and tokenizer
status_text = st.text("Loading model...")
model = load_model(config)
status_text.text("Loading tokenizer...")
tokenizer = load_tokenizer(config)
status_text.empty()
sep_id = tokenizer.token_to_id(config.sep_token)
end_id = tokenizer.token_to_id(config.end_token)
with st.sidebar:
strategy = st.selectbox("Sampling strategy", ["greedy", "top-p"], index=1)
temperature = st.slider("Temperature", 0.1, 2.0, 0.7)
top_p = st.slider("Top-p", 0.1, 1.0, 0.95)
clear_chat = st.button("Clear chat history")
if clear_chat:
st.session_state.messages = []
# Based on code from the Streamlit docs
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# React to user input
if prompt := st.chat_input("Type your question...", max_chars=100):
st.chat_message("user").write(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
# Sample answer from model
input_sequence = tokenize_input(tokenizer, prompt, sep_id)
answer = sample_sequence(input_sequence, model, strategy, config.max_len, config.device, end_id, p=top_p, temperature=temperature)
answer_text = decode_output(tokenizer, answer)
st.chat_message("assistant").write(answer_text)
st.session_state.messages.append({"role": "assistant", "content": answer_text})