Spaces:
Sleeping
Sleeping
Update pages/1_Simple_Chat_UI.py
Browse files- pages/1_Simple_Chat_UI.py +59 -60
pages/1_Simple_Chat_UI.py
CHANGED
@@ -1,60 +1,59 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
|
3 |
-
|
4 |
-
def load_model_tokenizer(model_name, hf_api_key):
|
5 |
-
if model_name == "Mistral-7B":
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
response
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
st.
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
st.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
response
|
58 |
-
|
59 |
-
message
|
60 |
-
st.session_state.messages.append(message)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
|
3 |
+
|
4 |
+
def load_model_tokenizer(model_name, hf_api_key):
|
5 |
+
if model_name == "Mistral-7B":
|
6 |
+
model_name="mistralai/Mistral-7B-Instruct-v0.2"
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, token=hf_api_key)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, tokenizer=hf_api_key)
|
9 |
+
elif model_name == "blenderbot-400M-distill":
|
10 |
+
model_name = "facebook/blenderbot-400M-distill"
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
return (model,tokenizer)
|
14 |
+
|
15 |
+
def generate_response(prompt_input, model, tokenizer):
|
16 |
+
inputs = tokenizer.encode_plus(prompt_input, return_tensors="pt")
|
17 |
+
# Generate the response from the model with additional parameters
|
18 |
+
outputs = model.generate(**inputs, max_length=max_length, do_sample=True ,temperature=temperature)
|
19 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
20 |
+
return response
|
21 |
+
|
22 |
+
st.set_page_config(page_title="Learn Geoscience")
|
23 |
+
|
24 |
+
with st.sidebar:
|
25 |
+
st.title('Learn Geoscience Chat')
|
26 |
+
if 'hf_key' in st.secrets:
|
27 |
+
st.success('Huggingface API key provided', icon='β
')
|
28 |
+
hf_api_key = st.secrets['hf_key']
|
29 |
+
else:
|
30 |
+
hf_api_key = st.text_input('Enter Huggingface API Key:', type='password')
|
31 |
+
if not hf_api_key:
|
32 |
+
st.warning('Please enter Huggingface API key!', icon='β οΈ')
|
33 |
+
else:
|
34 |
+
st.success('Proceed to entering your prompt message!', icon='π')
|
35 |
+
max_length = st.slider("Max Length", 10, 100, 50)
|
36 |
+
temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
37 |
+
|
38 |
+
if "messages" not in st.session_state.keys():
|
39 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
40 |
+
|
41 |
+
model_name = st.radio("Select model to chat", options=["Mistral-7B", "LLaMa-2B", "blenderbot-400M-distill"], horizontal=True, key='model_selection')
|
42 |
+
model, tokenizer = load_model_tokenizer(model_name, hf_api_key)
|
43 |
+
|
44 |
+
for message in st.session_state.messages:
|
45 |
+
with st.chat_message(message["role"]):
|
46 |
+
st.write(message["content"])
|
47 |
+
|
48 |
+
if prompt := st.chat_input(disabled = not hf_api_key):
|
49 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
50 |
+
with st.chat_message("user"):
|
51 |
+
st.write(prompt)
|
52 |
+
|
53 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
54 |
+
with st.chat_message("assistant"):
|
55 |
+
with st.spinner("Thinking..."):
|
56 |
+
response = generate_response(prompt, model, tokenizer)
|
57 |
+
st.write(response)
|
58 |
+
message = {"role": "assistant", "content": response}
|
59 |
+
st.session_state.messages.append(message)
|
|