Spaces:
Runtime error
Runtime error
Gokulnath2003
commited on
Commit
•
8ff67d3
1
Parent(s):
93979c3
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import numpy as np
|
3 |
import streamlit as st
|
4 |
from openai import OpenAI
|
@@ -7,107 +6,50 @@ import sys
|
|
7 |
from dotenv import load_dotenv, dotenv_values
|
8 |
load_dotenv()
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
# initialize the client
|
15 |
client = OpenAI(
|
16 |
base_url="https://api-inference.huggingface.co/v1",
|
17 |
-
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN')#
|
18 |
-
)
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
#Create supported models
|
24 |
-
model_links ={
|
25 |
-
"Meta-Llama-3-8B":"meta-llama/Meta-Llama-3-8B-Instruct",
|
26 |
-
"Mistral-7B":"mistralai/Mistral-7B-Instruct-v0.2",
|
27 |
-
"Gemma-7B":"google/gemma-1.1-7b-it",
|
28 |
-
"Gemma-2B":"google/gemma-1.1-2b-it",
|
29 |
-
"Zephyr-7B-β":"HuggingFaceH4/zephyr-7b-beta",
|
30 |
|
|
|
|
|
|
|
31 |
}
|
32 |
|
33 |
-
#Pull info about the model to display
|
34 |
-
model_info ={
|
35 |
-
"
|
36 |
-
|
37 |
-
|
38 |
-
'logo':'
|
39 |
-
|
40 |
-
{'description':"""The Gemma model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
41 |
-
\nIt was created by the [**Google's AI Team**](https://blog.google/technology/developers/gemma-open-models/) team as has over **7 billion parameters.** \n""",
|
42 |
-
'logo':'https://pbs.twimg.com/media/GG3sJg7X0AEaNIq.jpg'},
|
43 |
-
"Gemma-2B":
|
44 |
-
{'description':"""The Gemma model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
45 |
-
\nIt was created by the [**Google's AI Team**](https://blog.google/technology/developers/gemma-open-models/) team as has over **2 billion parameters.** \n""",
|
46 |
-
'logo':'https://pbs.twimg.com/media/GG3sJg7X0AEaNIq.jpg'},
|
47 |
-
"Zephyr-7B":
|
48 |
-
{'description':"""The Zephyr model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
49 |
-
\nFrom Huggingface: \n\
|
50 |
-
Zephyr is a series of language models that are trained to act as helpful assistants. \
|
51 |
-
[Zephyr 7B Gemma](https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1)\
|
52 |
-
is the third model in the series, and is a fine-tuned version of google/gemma-7b \
|
53 |
-
that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
|
54 |
-
'logo':'https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1/resolve/main/thumbnail.png'},
|
55 |
-
"Zephyr-7B-β":
|
56 |
-
{'description':"""The Zephyr model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
57 |
-
\nFrom Huggingface: \n\
|
58 |
-
Zephyr is a series of language models that are trained to act as helpful assistants. \
|
59 |
-
[Zephyr-7B-β](https://huggingface.co/HuggingFaceH4/zephyr-7b-beta)\
|
60 |
-
is the second model in the series, and is a fine-tuned version of mistralai/Mistral-7B-v0.1 \
|
61 |
-
that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
|
62 |
-
'logo':'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'},
|
63 |
-
"Meta-Llama-3-8B":
|
64 |
-
{'description':"""The Llama (3) model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
65 |
-
\nIt was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.** \n""",
|
66 |
-
'logo':'Llama_logo.png'},
|
67 |
}
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
|
72 |
-
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
73 |
-
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
74 |
-
"1326984c-39b0-492c-a773-f120d747a7e2.jpg",
|
75 |
-
"42a98d03-5ed7-4b3b-af89-7c4876cb14c3.jpg",
|
76 |
-
"8b3317ed-2083-42ac-a575-7ae45f9fdc0d.jpg",
|
77 |
-
"ee17f54a-83ac-44a3-8a35-e89ff7153fb4.jpg",
|
78 |
-
"027eef85-ccc1-4a66-8967-5d74f34c8bb4.jpg",
|
79 |
-
"08f5398d-7f89-47da-a5cd-1ed74967dc1f.jpg",
|
80 |
-
"0fd781ff-ec46-4bdc-a4e8-24f18bf07def.jpg",
|
81 |
-
"0fb4aeee-f949-4c7b-a6d8-05bf0736bdd1.jpg",
|
82 |
-
"6edac66e-c0de-4e69-a9d6-b2e6f6f9001b.jpg",
|
83 |
-
"bfb9e165-c643-4993-9b3a-7e73571672a6.jpg"]
|
84 |
-
|
85 |
-
|
86 |
|
87 |
def reset_conversation():
|
88 |
-
'''
|
89 |
-
Resets Conversation
|
90 |
-
'''
|
91 |
st.session_state.conversation = []
|
92 |
st.session_state.messages = []
|
93 |
return None
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
|
98 |
# Define the available models
|
99 |
-
models =[key for key in model_links.keys()]
|
100 |
|
101 |
# Create the sidebar with the dropdown for model selection
|
102 |
selected_model = st.sidebar.selectbox("Select Model", models)
|
103 |
|
104 |
-
#
|
105 |
-
|
106 |
-
|
107 |
|
108 |
-
#
|
109 |
-
st.sidebar.
|
110 |
|
|
|
|
|
111 |
|
112 |
# Create model description
|
113 |
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
@@ -115,57 +57,41 @@ st.sidebar.markdown(model_info[selected_model]['description'])
|
|
115 |
st.sidebar.image(model_info[selected_model]['logo'])
|
116 |
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
if "prev_option" not in st.session_state:
|
123 |
st.session_state.prev_option = selected_model
|
124 |
|
125 |
if st.session_state.prev_option != selected_model:
|
126 |
st.session_state.messages = []
|
127 |
-
# st.write(f"Changed to {selected_model}")
|
128 |
st.session_state.prev_option = selected_model
|
129 |
reset_conversation()
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
#Pull in the model we want to use
|
134 |
repo_id = model_links[selected_model]
|
135 |
|
136 |
-
|
137 |
st.subheader(f'AI - {selected_model}')
|
138 |
-
# st.title(f'ChatBot Using {selected_model}')
|
139 |
|
140 |
# Set a default model
|
141 |
if selected_model not in st.session_state:
|
142 |
-
st.session_state[selected_model] = model_links[selected_model]
|
143 |
|
144 |
# Initialize chat history
|
145 |
if "messages" not in st.session_state:
|
146 |
st.session_state.messages = []
|
147 |
|
148 |
-
|
149 |
# Display chat messages from history on app rerun
|
150 |
for message in st.session_state.messages:
|
151 |
with st.chat_message(message["role"]):
|
152 |
st.markdown(message["content"])
|
153 |
|
154 |
-
|
155 |
-
|
156 |
# Accept user input
|
157 |
if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
158 |
-
|
159 |
# Display user message in chat message container
|
160 |
with st.chat_message("user"):
|
161 |
st.markdown(prompt)
|
162 |
-
# Add user message to chat history
|
163 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
164 |
|
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],
|
@@ -173,28 +99,18 @@ if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
|
173 |
{"role": m["role"], "content": m["content"]}
|
174 |
for m in st.session_state.messages
|
175 |
],
|
176 |
-
temperature=temp_values
|
177 |
stream=True,
|
178 |
max_tokens=3000,
|
179 |
)
|
180 |
-
|
181 |
response = st.write_stream(stream)
|
182 |
|
183 |
except Exception as e:
|
184 |
-
|
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})
|
|
|
|
|
1 |
import numpy as np
|
2 |
import streamlit as st
|
3 |
from openai import OpenAI
|
|
|
6 |
from dotenv import load_dotenv, dotenv_values
|
7 |
load_dotenv()
|
8 |
|
|
|
|
|
|
|
|
|
9 |
# initialize the client
|
10 |
client = OpenAI(
|
11 |
base_url="https://api-inference.huggingface.co/v1",
|
12 |
+
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
|
13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Create supported model
|
16 |
+
model_links = {
|
17 |
+
"Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct"
|
18 |
}
|
19 |
|
20 |
+
# Pull info about the model to display
|
21 |
+
model_info = {
|
22 |
+
"Meta-Llama-3-8B": {
|
23 |
+
'description': """The Llama (3) model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n
|
24 |
+
\nIt was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.** \n""",
|
25 |
+
'logo': 'Llama_logo.png'
|
26 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
}
|
28 |
|
29 |
+
# Random dog images for error message
|
30 |
+
random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg", "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def reset_conversation():
|
33 |
+
'''Resets Conversation'''
|
|
|
|
|
34 |
st.session_state.conversation = []
|
35 |
st.session_state.messages = []
|
36 |
return None
|
|
|
|
|
|
|
37 |
|
38 |
# Define the available models
|
39 |
+
models = [key for key in model_links.keys()]
|
40 |
|
41 |
# Create the sidebar with the dropdown for model selection
|
42 |
selected_model = st.sidebar.selectbox("Select Model", models)
|
43 |
|
44 |
+
# Custom description for SciMom
|
45 |
+
st.sidebar.write("Built for my mom, with love. This model is pretrained with textbooks of Science NCERT.")
|
46 |
+
st.sidebar.write("Model used: Meta Llama, trained using: Docker AutoTrain.")
|
47 |
|
48 |
+
# Create a temperature slider
|
49 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
50 |
|
51 |
+
# Add reset button to clear conversation
|
52 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
53 |
|
54 |
# Create model description
|
55 |
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
|
|
57 |
st.sidebar.image(model_info[selected_model]['logo'])
|
58 |
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
59 |
|
|
|
|
|
|
|
|
|
60 |
if "prev_option" not in st.session_state:
|
61 |
st.session_state.prev_option = selected_model
|
62 |
|
63 |
if st.session_state.prev_option != selected_model:
|
64 |
st.session_state.messages = []
|
|
|
65 |
st.session_state.prev_option = selected_model
|
66 |
reset_conversation()
|
67 |
|
68 |
+
# Pull in the model we want to use
|
|
|
|
|
69 |
repo_id = model_links[selected_model]
|
70 |
|
|
|
71 |
st.subheader(f'AI - {selected_model}')
|
|
|
72 |
|
73 |
# Set a default model
|
74 |
if selected_model not in st.session_state:
|
75 |
+
st.session_state[selected_model] = model_links[selected_model]
|
76 |
|
77 |
# Initialize chat history
|
78 |
if "messages" not in st.session_state:
|
79 |
st.session_state.messages = []
|
80 |
|
|
|
81 |
# Display chat messages from history on app rerun
|
82 |
for message in st.session_state.messages:
|
83 |
with st.chat_message(message["role"]):
|
84 |
st.markdown(message["content"])
|
85 |
|
|
|
|
|
86 |
# Accept user input
|
87 |
if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
|
|
88 |
# Display user message in chat message container
|
89 |
with st.chat_message("user"):
|
90 |
st.markdown(prompt)
|
|
|
91 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
92 |
|
|
|
93 |
# Display assistant response in chat message container
|
94 |
with st.chat_message("assistant"):
|
|
|
95 |
try:
|
96 |
stream = client.chat.completions.create(
|
97 |
model=model_links[selected_model],
|
|
|
99 |
{"role": m["role"], "content": m["content"]}
|
100 |
for m in st.session_state.messages
|
101 |
],
|
102 |
+
temperature=temp_values,
|
103 |
stream=True,
|
104 |
max_tokens=3000,
|
105 |
)
|
|
|
106 |
response = st.write_stream(stream)
|
107 |
|
108 |
except Exception as e:
|
109 |
+
response = "😵💫 Looks like something went wrong! Here's a random pic of a 🐶:"
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
st.write(response)
|
111 |
+
random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
|
112 |
st.image(random_dog_pick)
|
113 |
st.write("This was the error message:")
|
114 |
st.write(e)
|
115 |
|
116 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|