Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
3 |
+
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
5 |
+
@st.cache_data
|
6 |
+
def load_model(model_name):
|
7 |
+
model = AutoModelWithLMHead.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
8 |
+
return model
|
9 |
+
|
10 |
+
model = load_model("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
11 |
+
|
12 |
+
def infer(input_ids, max_length, temperature, top_k, top_p):
|
13 |
+
|
14 |
+
output_sequences = model.generate(
|
15 |
+
input_ids=input_ids,
|
16 |
+
max_length=max_length,
|
17 |
+
temperature=temperature,
|
18 |
+
top_k=top_k,
|
19 |
+
top_p=top_p,
|
20 |
+
do_sample=True,
|
21 |
+
num_return_sequences=1
|
22 |
+
)
|
23 |
+
|
24 |
+
return output_sequences
|
25 |
+
default_value = "Ask me anything!"
|
26 |
+
|
27 |
+
#prompts
|
28 |
+
st.title("Chatbot")
|
29 |
+
|
30 |
+
sent = st.text_area("Message", default_value, height = 100)
|
31 |
+
max_length = st.sidebar.slider("Max Length", min_value = 10, max_value=30)
|
32 |
+
temperature = st.sidebar.slider("Temperature", value = 1.0, min_value = 0.0, max_value=1.0, step=0.05)
|
33 |
+
top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 0)
|
34 |
+
top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)
|
35 |
+
|
36 |
+
encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
|
37 |
+
if encoded_prompt.size()[-1] == 0:
|
38 |
+
input_ids = None
|
39 |
+
else:
|
40 |
+
input_ids = encoded_prompt
|
41 |
+
|
42 |
+
|
43 |
+
output_sequences = infer(input_ids, max_length, temperature, top_k, top_p)
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
48 |
+
print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
|
49 |
+
generated_sequences = generated_sequence.tolist()
|
50 |
+
|
51 |
+
# Decode text
|
52 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
53 |
+
|
54 |
+
# Remove all text after the stop token
|
55 |
+
#text = text[: text.find(args.stop_token) if args.stop_token else None]
|
56 |
+
|
57 |
+
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
58 |
+
total_sequence = (
|
59 |
+
sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
|
60 |
+
)
|
61 |
+
|
62 |
+
generated_sequences.append(total_sequence)
|
63 |
+
print(total_sequence)
|
64 |
+
|
65 |
+
|
66 |
+
st.write(generated_sequences[-1])
|