Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,150 @@
|
|
1 |
import numpy as np
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Random dog images for error messages
|
8 |
-
random_dog = [
|
9 |
-
"0f476473-2d8b-415e-b944-483768418a95.jpg",
|
10 |
-
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
11 |
-
# Add more images as needed
|
12 |
-
]
|
13 |
|
14 |
-
# Function to reset conversation
|
15 |
def reset_conversation():
|
16 |
-
'''
|
|
|
|
|
17 |
st.session_state.conversation = []
|
18 |
st.session_state.messages = []
|
19 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Initialize chat history
|
27 |
if "messages" not in st.session_state:
|
28 |
st.session_state.messages = []
|
29 |
|
|
|
30 |
# Display chat messages from history on app rerun
|
31 |
for message in st.session_state.messages:
|
32 |
with st.chat_message(message["role"]):
|
33 |
st.markdown(message["content"])
|
34 |
|
35 |
-
# Set cache directory path to /data
|
36 |
-
cache_dir = "/data" # المسار المحدد للتخزين في مساحة Hugging Face
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
with st.chat_message("user"):
|
41 |
st.markdown(prompt)
|
|
|
42 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
# Load the tokenizer and model with caching in the specified directory
|
47 |
-
tokenizer = AutoTokenizer.from_pretrained("joermd/speedy-llama2", cache_dir=cache_dir)
|
48 |
-
model = AutoModelForCausalLM.from_pretrained(
|
49 |
-
"joermd/speedy-llama2",
|
50 |
-
cache_dir=cache_dir,
|
51 |
-
torch_dtype=torch.bfloat16,
|
52 |
-
device_map="auto"
|
53 |
-
)
|
54 |
-
|
55 |
-
# Prepare the system message and conversation
|
56 |
-
system_message = {
|
57 |
-
"role": "system",
|
58 |
-
"content": "You are a friendly chatbot who answers questions in Arabic."
|
59 |
-
}
|
60 |
-
messages = [system_message, {"role": "user", "content": prompt}]
|
61 |
-
|
62 |
-
# Create conversation prompt using chat template
|
63 |
-
conversation = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
64 |
-
|
65 |
-
# Generate response
|
66 |
-
inputs = tokenizer(conversation, return_tensors="pt")
|
67 |
-
outputs = model.generate(
|
68 |
-
inputs.input_ids,
|
69 |
-
max_new_tokens=max_token_value,
|
70 |
-
temperature=temp_values,
|
71 |
-
do_sample=True,
|
72 |
-
top_k=50,
|
73 |
-
top_p=0.95
|
74 |
-
)
|
75 |
-
assistant_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
76 |
-
|
77 |
-
# Clear memory (for CUDA) and delete the model to free up RAM
|
78 |
-
if torch.cuda.is_available():
|
79 |
-
torch.cuda.empty_cache()
|
80 |
-
del model
|
81 |
-
|
82 |
-
except Exception as e:
|
83 |
-
assistant_response = "😵💫 عذراً، حدث خطأ في الاتصال! حاول مرة أخرى لاحقاً. إليك صورة كلب 🐶:"
|
84 |
-
st.image(f'https://random.dog/{random_dog[np.random.randint(len(random_dog))]}')
|
85 |
-
st.write("رسالة الخطأ:")
|
86 |
-
st.write(e)
|
87 |
-
|
88 |
-
# Display assistant response
|
89 |
with st.chat_message("assistant"):
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# 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 models
|
16 |
+
model_links = {
|
17 |
+
"Mistral-Nemo-Base-2407": "joermd/speedy-llama2",
|
18 |
+
}
|
19 |
+
|
20 |
+
#Random dog images for error message
|
21 |
+
random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
|
22 |
+
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
23 |
+
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
24 |
+
"1326984c-39b0-492c-a773-f120d747a7e2.jpg",
|
25 |
+
"42a98d03-5ed7-4b3b-af89-7c4876cb14c3.jpg",
|
26 |
+
"8b3317ed-2083-42ac-a575-7ae45f9fdc0d.jpg",
|
27 |
+
"ee17f54a-83ac-44a3-8a35-e89ff7153fb4.jpg",
|
28 |
+
"027eef85-ccc1-4a66-8967-5d74f34c8bb4.jpg",
|
29 |
+
"08f5398d-7f89-47da-a5cd-1ed74967dc1f.jpg",
|
30 |
+
"0fd781ff-ec46-4bdc-a4e8-24f18bf07def.jpg",
|
31 |
+
"0fb4aeee-f949-4c7b-a6d8-05bf0736bdd1.jpg",
|
32 |
+
"6edac66e-c0de-4e69-a9d6-b2e6f6f9001b.jpg",
|
33 |
+
"bfb9e165-c643-4993-9b3a-7e73571672a6.jpg"]
|
34 |
+
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
37 |
def reset_conversation():
|
38 |
+
'''
|
39 |
+
Resets Conversation
|
40 |
+
'''
|
41 |
st.session_state.conversation = []
|
42 |
st.session_state.messages = []
|
43 |
return None
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
# Define the available models
|
49 |
+
models =[key for key in model_links.keys()]
|
50 |
+
|
51 |
+
# Create the sidebar with the dropdown for model selection
|
52 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
53 |
+
|
54 |
+
# Create a temperature slider
|
55 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
56 |
|
57 |
+
# Create a max_token slider
|
58 |
+
max_token_value = st.sidebar.slider('Select a max_token value', 1000, 9000, (5000))
|
59 |
+
|
60 |
+
#Add reset button to clear conversation
|
61 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation) #Reset button
|
62 |
+
|
63 |
+
|
64 |
+
# Create model description
|
65 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
66 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
67 |
+
# st.sidebar.markdown("\n[TypeGPT](https://typegpt.net).")
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
if "prev_option" not in st.session_state:
|
73 |
+
st.session_state.prev_option = selected_model
|
74 |
+
|
75 |
+
if st.session_state.prev_option != selected_model:
|
76 |
+
st.session_state.messages = []
|
77 |
+
# st.write(f"Changed to {selected_model}")
|
78 |
+
st.session_state.prev_option = selected_model
|
79 |
+
reset_conversation()
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
#Pull in the model we want to use
|
84 |
+
repo_id = model_links[selected_model]
|
85 |
+
|
86 |
+
|
87 |
+
st.subheader(f'{selected_model}')
|
88 |
+
# # st.title(f'ChatBot Using {selected_model}')
|
89 |
+
|
90 |
+
# Set a default model
|
91 |
+
if selected_model not in st.session_state:
|
92 |
+
st.session_state[selected_model] = model_links[selected_model]
|
93 |
|
94 |
# Initialize chat history
|
95 |
if "messages" not in st.session_state:
|
96 |
st.session_state.messages = []
|
97 |
|
98 |
+
|
99 |
# Display chat messages from history on app rerun
|
100 |
for message in st.session_state.messages:
|
101 |
with st.chat_message(message["role"]):
|
102 |
st.markdown(message["content"])
|
103 |
|
|
|
|
|
104 |
|
105 |
+
|
106 |
+
# Accept user input
|
107 |
+
if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
108 |
+
# Display user message in chat message container
|
109 |
with st.chat_message("user"):
|
110 |
st.markdown(prompt)
|
111 |
+
# Add user message to chat history
|
112 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
113 |
|
114 |
+
|
115 |
+
# Display assistant response in chat message container
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
with st.chat_message("assistant"):
|
117 |
+
try:
|
118 |
+
stream = client.chat.completions.create(
|
119 |
+
model=model_links[selected_model],
|
120 |
+
messages=[
|
121 |
+
{"role": m["role"], "content": m["content"]}
|
122 |
+
for m in st.session_state.messages
|
123 |
+
],
|
124 |
+
temperature=temp_values,#0.5,
|
125 |
+
stream=True,
|
126 |
+
max_tokens=max_token_value,
|
127 |
+
)
|
128 |
+
|
129 |
+
response = st.write_stream(stream)
|
130 |
+
|
131 |
+
except Exception as e:
|
132 |
+
# st.empty()
|
133 |
+
response = "😵💫 Looks like someone unplugged something!\
|
134 |
+
\n Either the model space is being updated or something is down.\
|
135 |
+
\n\
|
136 |
+
\n Try again later. \
|
137 |
+
\n\
|
138 |
+
\n Here's a random pic of a 🐶:"
|
139 |
+
st.write(response)
|
140 |
+
random_dog_pick = 'https://random.dog/'+ random_dog[np.random.randint(len(random_dog))]
|
141 |
+
st.image(random_dog_pick)
|
142 |
+
st.write("This was the error message:")
|
143 |
+
st.write(e)
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|