Spaces:
Running
Running
Updated
Browse files
app.py
CHANGED
@@ -1,9 +1,4 @@
|
|
1 |
-
|
2 |
-
@author: Nigel Gebodh
|
3 |
-
@email: nigel.gebodh@gmail.com
|
4 |
-
|
5 |
-
"""
|
6 |
-
|
7 |
import streamlit as st
|
8 |
from openai import OpenAI
|
9 |
import os
|
@@ -26,17 +21,17 @@ client = OpenAI(
|
|
26 |
|
27 |
#Create supported models
|
28 |
model_links ={
|
29 |
-
"
|
30 |
-
"
|
31 |
-
"Gemma-
|
|
|
32 |
"Zephyr-7B-β":"HuggingFaceH4/zephyr-7b-beta",
|
33 |
-
# "Llama-2":"meta-llama/Llama-2-7b-chat-hf"
|
34 |
|
35 |
}
|
36 |
|
37 |
#Pull info about the model to display
|
38 |
model_info ={
|
39 |
-
"Mistral":
|
40 |
{'description':"""The Mistral model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
41 |
\nIt was created by the [**Mistral AI**](https://mistral.ai/news/announcing-mistral-7b/) team as has over **7 billion parameters.** \n""",
|
42 |
'logo':'https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp'},
|
@@ -64,9 +59,30 @@ model_info ={
|
|
64 |
is the second model in the series, and is a fine-tuned version of mistralai/Mistral-7B-v0.1 \
|
65 |
that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
|
66 |
'logo':'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'},
|
67 |
-
|
|
|
|
|
|
|
68 |
}
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def reset_conversation():
|
71 |
'''
|
72 |
Resets Conversation
|
@@ -98,6 +114,8 @@ st.sidebar.markdown(model_info[selected_model]['description'])
|
|
98 |
st.sidebar.image(model_info[selected_model]['logo'])
|
99 |
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
100 |
st.sidebar.markdown("\nLearn how to build this chatbot [here](https://ngebodh.github.io/projects/2024-03-05/).")
|
|
|
|
|
101 |
|
102 |
|
103 |
|
@@ -147,16 +165,36 @@ if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
|
147 |
|
148 |
# Display assistant response in chat message container
|
149 |
with st.chat_message("assistant"):
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
1 |
+
mport numpy as np
|
|
|
|
|
|
|
|
|
|
|
2 |
import streamlit as st
|
3 |
from openai import OpenAI
|
4 |
import os
|
|
|
21 |
|
22 |
#Create supported models
|
23 |
model_links ={
|
24 |
+
"Meta-Llama-3-8B":"meta-llama/Meta-Llama-3-8B-Instruct",
|
25 |
+
"Mistral-7B":"mistralai/Mistral-7B-Instruct-v0.2",
|
26 |
+
"Gemma-7B":"google/gemma-1.1-7b-it",
|
27 |
+
"Gemma-2B":"google/gemma-1.1-2b-it",
|
28 |
"Zephyr-7B-β":"HuggingFaceH4/zephyr-7b-beta",
|
|
|
29 |
|
30 |
}
|
31 |
|
32 |
#Pull info about the model to display
|
33 |
model_info ={
|
34 |
+
"Mistral-7B":
|
35 |
{'description':"""The Mistral model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
36 |
\nIt was created by the [**Mistral AI**](https://mistral.ai/news/announcing-mistral-7b/) team as has over **7 billion parameters.** \n""",
|
37 |
'logo':'https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp'},
|
|
|
59 |
is the second model in the series, and is a fine-tuned version of mistralai/Mistral-7B-v0.1 \
|
60 |
that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
|
61 |
'logo':'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'},
|
62 |
+
"Meta-Llama-3-8B":
|
63 |
+
{'description':"""The Llama (3) model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
64 |
+
\nIt was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.** \n""",
|
65 |
+
'logo':'Llama_logo.png'},
|
66 |
}
|
67 |
|
68 |
+
|
69 |
+
#Random dog images for error message
|
70 |
+
random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
|
71 |
+
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
72 |
+
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
73 |
+
"1326984c-39b0-492c-a773-f120d747a7e2.jpg",
|
74 |
+
"42a98d03-5ed7-4b3b-af89-7c4876cb14c3.jpg",
|
75 |
+
"8b3317ed-2083-42ac-a575-7ae45f9fdc0d.jpg",
|
76 |
+
"ee17f54a-83ac-44a3-8a35-e89ff7153fb4.jpg",
|
77 |
+
"027eef85-ccc1-4a66-8967-5d74f34c8bb4.jpg",
|
78 |
+
"08f5398d-7f89-47da-a5cd-1ed74967dc1f.jpg",
|
79 |
+
"0fd781ff-ec46-4bdc-a4e8-24f18bf07def.jpg",
|
80 |
+
"0fb4aeee-f949-4c7b-a6d8-05bf0736bdd1.jpg",
|
81 |
+
"6edac66e-c0de-4e69-a9d6-b2e6f6f9001b.jpg",
|
82 |
+
"bfb9e165-c643-4993-9b3a-7e73571672a6.jpg"]
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
def reset_conversation():
|
87 |
'''
|
88 |
Resets Conversation
|
|
|
114 |
st.sidebar.image(model_info[selected_model]['logo'])
|
115 |
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
116 |
st.sidebar.markdown("\nLearn how to build this chatbot [here](https://ngebodh.github.io/projects/2024-03-05/).")
|
117 |
+
st.sidebar.markdown("\nRun into issues? Try the [back-up](https://huggingface.co/spaces/ngebodh/SimpleChatbot-Backup).")
|
118 |
+
|
119 |
|
120 |
|
121 |
|
|
|
165 |
|
166 |
# Display assistant response in chat message container
|
167 |
with st.chat_message("assistant"):
|
168 |
+
|
169 |
+
try:
|
170 |
+
stream = client.chat.completions.create(
|
171 |
+
model=model_links[selected_model],
|
172 |
+
messages=[
|
173 |
+
{"role": m["role"], "content": m["content"]}
|
174 |
+
for m in st.session_state.messages
|
175 |
+
],
|
176 |
+
temperature=temp_values,#0.5,
|
177 |
+
stream=True,
|
178 |
+
max_tokens=3000,
|
179 |
+
)
|
180 |
+
|
181 |
+
response = st.write_stream(stream)
|
182 |
+
|
183 |
+
except Exception as e:
|
184 |
+
# st.empty()
|
185 |
+
response = "😵💫 Looks like someone unplugged something!😵💫\
|
186 |
+
\n Either the model space is being updated or something is down.\
|
187 |
+
\n\
|
188 |
+
\n Try again later. \
|
189 |
+
\n\
|
190 |
+
\n Here's a random pic of a 🐶:"
|
191 |
+
st.write(response)
|
192 |
+
random_dog_pick = 'https://random.dog/'+ random_dog[np.random.randint(len(random_dog))]
|
193 |
+
st.image(random_dog_pick)
|
194 |
+
st.write("This was the error message:")
|
195 |
+
st.write(e)
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
+
|
200 |
st.session_state.messages.append({"role": "assistant", "content": response})
|