Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,112 @@
|
|
1 |
import numpy as np
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
|
30 |
def reset_conversation():
|
31 |
'''
|
32 |
-
|
33 |
'''
|
34 |
st.session_state.conversation = []
|
35 |
st.session_state.messages = []
|
36 |
return None
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
# Define the available models
|
42 |
-
models =[key for key in model_links.keys()]
|
43 |
-
|
44 |
-
# Create the sidebar with the dropdown for model selection
|
45 |
-
selected_model = st.sidebar.selectbox("Select Model", models)
|
46 |
-
|
47 |
-
# Create a temperature slider
|
48 |
-
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
49 |
-
|
50 |
-
# Create a max_token slider
|
51 |
-
max_token_value = st.sidebar.slider('Select a max_token value', 1000, 9000, (5000))
|
52 |
-
|
53 |
-
#Add reset button to clear conversation
|
54 |
-
st.sidebar.button('Reset Chat', on_click=reset_conversation) #Reset button
|
55 |
-
|
56 |
-
|
57 |
-
# Create model description
|
58 |
-
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
59 |
-
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
60 |
-
# st.sidebar.markdown("\n[TypeGPT](https://typegpt.net).")
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
if "prev_option" not in st.session_state:
|
66 |
-
st.session_state.prev_option = selected_model
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
if "messages" not in st.session_state:
|
89 |
st.session_state.messages = []
|
90 |
|
91 |
-
|
92 |
-
# Display chat messages from history on app rerun
|
93 |
for message in st.session_state.messages:
|
94 |
with st.chat_message(message["role"]):
|
95 |
st.markdown(message["content"])
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
#
|
100 |
-
if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
101 |
-
# Display user message in chat message container
|
102 |
with st.chat_message("user"):
|
103 |
st.markdown(prompt)
|
104 |
-
# Add user message to chat history
|
105 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
106 |
|
107 |
-
|
108 |
-
# Display assistant response in chat message container
|
109 |
with st.chat_message("assistant"):
|
110 |
try:
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
max_tokens=max_token_value,
|
120 |
-
)
|
121 |
-
|
122 |
-
response = st.write_stream(stream)
|
123 |
|
124 |
except Exception as e:
|
125 |
-
|
126 |
-
response = "😵💫 Looks like someone unplugged something!\
|
127 |
-
\n Either the model space is being updated or something is down.\
|
128 |
-
\n\
|
129 |
-
\n Try again later. \
|
130 |
-
\n\
|
131 |
-
\n Here's a random pic of a 🐶:"
|
132 |
st.write(response)
|
133 |
-
random_dog_pick = 'https://random.dog/'+ random_dog[np.random.randint(len(random_dog))]
|
134 |
st.image(random_dog_pick)
|
135 |
-
st.write("
|
136 |
st.write(e)
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
1 |
import numpy as np
|
2 |
import streamlit as st
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# التحقق من توفر GPU
|
7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
"""
|
12 |
+
تحميل النموذج والمُرمِّز مع التخزين المؤقت
|
13 |
+
"""
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
"joermd/speedy-llama2",
|
16 |
+
torch_dtype=torch.float16,
|
17 |
+
device_map=device
|
18 |
+
)
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained("joermd/speedy-llama2")
|
20 |
+
return model, tokenizer
|
21 |
+
|
22 |
+
# الصور العشوائية للكلاب عند حدوث خطأ
|
23 |
+
random_dog = [
|
24 |
+
"0f476473-2d8b-415e-b944-483768418a95.jpg",
|
25 |
+
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
26 |
+
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
27 |
+
"1326984c-39b0-492c-a773-f120d747a7e2.jpg",
|
28 |
+
"42a98d03-5ed7-4b3b-af89-7c4376cb14c3.jpg"
|
29 |
+
]
|
30 |
|
31 |
def reset_conversation():
|
32 |
'''
|
33 |
+
إعادة تعيين المحادثة
|
34 |
'''
|
35 |
st.session_state.conversation = []
|
36 |
st.session_state.messages = []
|
37 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
def generate_response(prompt, temperature, max_length):
|
40 |
+
"""
|
41 |
+
توليد استجابة من النموذج
|
42 |
+
"""
|
43 |
+
try:
|
44 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
45 |
+
|
46 |
+
with torch.no_grad():
|
47 |
+
outputs = model.generate(
|
48 |
+
inputs,
|
49 |
+
max_length=max_length,
|
50 |
+
temperature=temperature,
|
51 |
+
do_sample=True,
|
52 |
+
pad_token_id=tokenizer.eos_token_id
|
53 |
+
)
|
54 |
+
|
55 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
56 |
+
return response
|
57 |
+
except Exception as e:
|
58 |
+
return str(e)
|
59 |
+
|
60 |
+
# تحميل النموذج والمُرمِّز
|
61 |
+
try:
|
62 |
+
with st.spinner('جاري تحميل النموذج... قد يستغرق هذا بضع دقائق...'):
|
63 |
+
model, tokenizer = load_model()
|
64 |
+
except Exception as e:
|
65 |
+
st.error(f"حدث خطأ أثناء تحميل النموذج: {str(e)}")
|
66 |
+
st.stop()
|
67 |
+
|
68 |
+
# إعداد واجهة Streamlit
|
69 |
+
st.subheader('Mistral Chat')
|
70 |
+
|
71 |
+
# إضافة أزرار التحكم في الشريط الجانبي
|
72 |
+
temp_values = st.sidebar.slider('اختر قيمة درجة الحرارة', 0.0, 1.0, 0.5)
|
73 |
+
max_token_value = st.sidebar.slider('اختر الحد الأقصى للرموز', 100, 2000, 500)
|
74 |
+
st.sidebar.button('إعادة تعيين المحادثة', on_click=reset_conversation)
|
75 |
+
|
76 |
+
# تهيئة سجل المحادثة
|
77 |
if "messages" not in st.session_state:
|
78 |
st.session_state.messages = []
|
79 |
|
80 |
+
# عرض رسائل المحادثة السابقة
|
|
|
81 |
for message in st.session_state.messages:
|
82 |
with st.chat_message(message["role"]):
|
83 |
st.markdown(message["content"])
|
84 |
|
85 |
+
# معالجة إدخال المستخدم
|
86 |
+
if prompt := st.chat_input("اسألني سؤالاً"):
|
87 |
+
# عرض رسالة المستخدم
|
|
|
|
|
88 |
with st.chat_message("user"):
|
89 |
st.markdown(prompt)
|
|
|
90 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
91 |
|
92 |
+
# عرض رد المساعد
|
|
|
93 |
with st.chat_message("assistant"):
|
94 |
try:
|
95 |
+
# توليد الرد
|
96 |
+
with st.spinner('جاري التفكير...'):
|
97 |
+
response = generate_response(
|
98 |
+
prompt,
|
99 |
+
temperature=temp_values,
|
100 |
+
max_length=max_token_value
|
101 |
+
)
|
102 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
103 |
|
104 |
except Exception as e:
|
105 |
+
response = "😵💫 يبدو أن هناك خطأ ما!\n حاول مرة أخرى لاحقاً.\n\n إليك صورة عشوائية لكلب 🐶:"
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
st.write(response)
|
107 |
+
random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
|
108 |
st.image(random_dog_pick)
|
109 |
+
st.write("رسالة الخطأ:")
|
110 |
st.write(e)
|
111 |
|
112 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|