Spaces:
Sleeping
Sleeping
Ron Vallejo
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,147 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import streamlit as st
|
3 |
+
from openai import OpenAI
|
4 |
+
import os
|
5 |
+
import sys
|
6 |
+
from dotenv import load_dotenv, dotenv_values
|
7 |
+
load_dotenv()
|
8 |
|
9 |
+
|
10 |
+
# initialize the client
|
11 |
+
client = OpenAI(
|
12 |
+
base_url="https://api-inference.huggingface.co/v1",
|
13 |
+
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN')#"hf_xxx" # Replace with your token
|
14 |
+
)
|
15 |
+
|
16 |
+
#Create supported models
|
17 |
+
model_links ={
|
18 |
+
"Meta-Llama-3.1-8B":"meta-llama/Meta-Llama-3.1-8B-Instruct"
|
19 |
+
}
|
20 |
+
|
21 |
+
#Pull info about the model to display
|
22 |
+
model_info ={
|
23 |
+
"Meta-Llama-3.1-8B":
|
24 |
+
{'description':"""The Llama (3.1) model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
25 |
+
\nIt was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.** \n""",
|
26 |
+
'logo':'Llama3_1_logo.png'},
|
27 |
+
}
|
28 |
+
|
29 |
+
|
30 |
+
#Random dog images for error message
|
31 |
+
random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
|
32 |
+
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
33 |
+
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
34 |
+
"1326984c-39b0-492c-a773-f120d747a7e2.jpg",
|
35 |
+
"42a98d03-5ed7-4b3b-af89-7c4876cb14c3.jpg",
|
36 |
+
"8b3317ed-2083-42ac-a575-7ae45f9fdc0d.jpg",
|
37 |
+
"ee17f54a-83ac-44a3-8a35-e89ff7153fb4.jpg",
|
38 |
+
"027eef85-ccc1-4a66-8967-5d74f34c8bb4.jpg",
|
39 |
+
"08f5398d-7f89-47da-a5cd-1ed74967dc1f.jpg",
|
40 |
+
"0fd781ff-ec46-4bdc-a4e8-24f18bf07def.jpg",
|
41 |
+
"0fb4aeee-f949-4c7b-a6d8-05bf0736bdd1.jpg",
|
42 |
+
"6edac66e-c0de-4e69-a9d6-b2e6f6f9001b.jpg",
|
43 |
+
"bfb9e165-c643-4993-9b3a-7e73571672a6.jpg"]
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
def reset_conversation():
|
48 |
+
'''
|
49 |
+
Resets Conversation
|
50 |
+
'''
|
51 |
+
st.session_state.conversation = []
|
52 |
+
st.session_state.messages = []
|
53 |
+
return None
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
# Define the available models
|
59 |
+
models =[key for key in model_links.keys()]
|
60 |
+
|
61 |
+
# Create the sidebar with the dropdown for model selection
|
62 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
63 |
+
|
64 |
+
#Create a temperature slider
|
65 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
66 |
+
|
67 |
+
|
68 |
+
#Add reset button to clear conversation
|
69 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation) #Reset button
|
70 |
+
|
71 |
+
# Create model description
|
72 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
73 |
+
st.sidebar.markdown(model_info[selected_model]['description'])
|
74 |
+
st.sidebar.image(model_info[selected_model]['logo'])
|
75 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
76 |
+
|
77 |
+
if "prev_option" not in st.session_state:
|
78 |
+
st.session_state.prev_option = selected_model
|
79 |
+
|
80 |
+
if st.session_state.prev_option != selected_model:
|
81 |
+
st.session_state.messages = []
|
82 |
+
# st.write(f"Changed to {selected_model}")
|
83 |
+
st.session_state.prev_option = selected_model
|
84 |
+
reset_conversation()
|
85 |
+
|
86 |
+
#Pull in the model we want to use
|
87 |
+
repo_id = model_links[selected_model]
|
88 |
+
|
89 |
+
st.subheader(f'AI - {selected_model}')
|
90 |
+
# st.title(f'ChatBot Using {selected_model}')
|
91 |
+
|
92 |
+
# Set a default model
|
93 |
+
if selected_model not in st.session_state:
|
94 |
+
st.session_state[selected_model] = model_links[selected_model]
|
95 |
+
|
96 |
+
# Initialize chat history
|
97 |
+
if "messages" not in st.session_state:
|
98 |
+
st.session_state.messages = []
|
99 |
+
|
100 |
+
|
101 |
+
# Display chat messages from history on app rerun
|
102 |
+
for message in st.session_state.messages:
|
103 |
+
with st.chat_message(message["role"]):
|
104 |
+
st.markdown(message["content"])
|
105 |
+
|
106 |
+
# Accept user input
|
107 |
+
if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
108 |
+
|
109 |
+
# Display user message in chat message container
|
110 |
+
with st.chat_message("user"):
|
111 |
+
st.markdown(prompt)
|
112 |
+
# Add user message to chat history
|
113 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
114 |
+
|
115 |
+
|
116 |
+
# Display assistant response in chat message container
|
117 |
+
with st.chat_message("assistant"):
|
118 |
+
|
119 |
+
try:
|
120 |
+
stream = client.chat.completions.create(
|
121 |
+
model=model_links[selected_model],
|
122 |
+
messages=[
|
123 |
+
{"role": m["role"], "content": m["content"]}
|
124 |
+
for m in st.session_state.messages
|
125 |
+
],
|
126 |
+
temperature=temp_values,#0.5,
|
127 |
+
stream=True,
|
128 |
+
max_tokens=3000,
|
129 |
+
)
|
130 |
+
|
131 |
+
response = st.write_stream(stream)
|
132 |
+
|
133 |
+
except Exception as e:
|
134 |
+
# st.empty()
|
135 |
+
response = "😵💫 Looks like someone unplugged something!\
|
136 |
+
\n Either the model space is being updated or something is down.\
|
137 |
+
\n\
|
138 |
+
\n Try again later. \
|
139 |
+
\n\
|
140 |
+
\n Here's a random pic of a 🐶:"
|
141 |
+
st.write(response)
|
142 |
+
random_dog_pick = 'https://random.dog/'+ random_dog[np.random.randint(len(random_dog))]
|
143 |
+
st.image(random_dog_pick)
|
144 |
+
st.write("This was the error message:")
|
145 |
+
st.write(e)
|
146 |
+
|
147 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|