Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,122 +1,105 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
st.markdown("""
|
9 |
-
<style>
|
10 |
-
.stTextInput > div > div > input {
|
11 |
-
background-color: #f0f2f6;
|
12 |
-
}
|
13 |
-
.stButton > button {
|
14 |
-
width: 100%;
|
15 |
-
border-radius: 20px;
|
16 |
-
}
|
17 |
-
.chat-message {
|
18 |
-
padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
|
19 |
-
}
|
20 |
-
.chat-message.user {
|
21 |
-
background-color: #2b313e
|
22 |
-
}
|
23 |
-
.chat-message.bot {
|
24 |
-
background-color: #475063
|
25 |
-
}
|
26 |
-
.chat-message .avatar {
|
27 |
-
width: 20%;
|
28 |
-
}
|
29 |
-
.chat-message .avatar img {
|
30 |
-
max-width: 78px;
|
31 |
-
max-height: 78px;
|
32 |
-
border-radius: 50%;
|
33 |
-
object-fit: cover;
|
34 |
-
}
|
35 |
-
.chat-message .message {
|
36 |
-
width: 80%;
|
37 |
-
padding: 0 1.5rem;
|
38 |
-
color: #fff;
|
39 |
-
}
|
40 |
-
</style>
|
41 |
-
""", unsafe_allow_html=True)
|
42 |
-
|
43 |
-
# Инициализация клиента
|
44 |
-
@st.cache_resource
|
45 |
-
def get_client():
|
46 |
-
return Client()
|
47 |
-
|
48 |
-
client = get_client()
|
49 |
-
|
50 |
-
# Функция для получения ответа от GPT
|
51 |
def get_gpt_response(messages):
|
|
|
52 |
response = client.chat.completions.create(
|
53 |
model="gpt-3.5-turbo",
|
54 |
-
messages=messages
|
55 |
)
|
56 |
return response.choices[0].message.content
|
57 |
|
58 |
-
# Инициализация
|
59 |
if 'messages' not in st.session_state:
|
60 |
-
st.session_state
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
if msg['role'] == 'user':
|
66 |
-
st.markdown(f"""
|
67 |
-
<div class="chat-message user">
|
68 |
-
<div class="avatar">
|
69 |
-
<img src="https://i.imgur.com/Yx5c8e1.png">
|
70 |
-
</div>
|
71 |
-
<div class="message">{msg['content']}</div>
|
72 |
-
</div>
|
73 |
-
""", unsafe_allow_html=True)
|
74 |
-
else:
|
75 |
-
st.markdown(f"""
|
76 |
-
<div class="chat-message bot">
|
77 |
-
<div class="avatar">
|
78 |
-
<img src="https://i.imgur.com/nGF1K8f.jpg">
|
79 |
-
</div>
|
80 |
-
<div class="message">{msg['content']}</div>
|
81 |
-
</div>
|
82 |
-
""", unsafe_allow_html=True)
|
83 |
|
84 |
-
#
|
85 |
-
st.
|
86 |
|
87 |
-
#
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
|
93 |
-
#
|
94 |
-
|
95 |
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
101 |
-
|
102 |
-
with st.spinner('GPT обрабатывает ваш запрос...'):
|
103 |
-
# Получение ответа от GPT
|
104 |
-
gpt_response = get_gpt_response(st.session_state.messages)
|
105 |
-
|
106 |
-
# Сохранение ответа GPT
|
107 |
-
st.session_state.messages.append({"role": "assistant", "content": gpt_response})
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
-
#
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
-
#
|
122 |
-
st.
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import g4f
|
3 |
+
from streamlit_lottie import st_lottie
|
4 |
+
import json
|
5 |
|
6 |
+
# Функция для загрузки анимаций Lottie
|
7 |
+
def load_lottiefile(filepath: str):
|
8 |
+
with open(filepath, "r") as f:
|
9 |
+
return json.load(f)
|
10 |
|
11 |
+
# Функция для отправки сообщений в GPT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def get_gpt_response(messages):
|
13 |
+
client = g4f.Client()
|
14 |
response = client.chat.completions.create(
|
15 |
model="gpt-3.5-turbo",
|
16 |
+
messages=messages,
|
17 |
)
|
18 |
return response.choices[0].message.content
|
19 |
|
20 |
+
# Инициализация списка сообщений
|
21 |
if 'messages' not in st.session_state:
|
22 |
+
st.session_state['messages'] = [{"role": "user", "content": "Hello"}]
|
23 |
|
24 |
+
# Загрузка анимаций Lottie
|
25 |
+
lottie_hello = load_lottiefile("lottie_hello.json")
|
26 |
+
lottie_typing = load_lottiefile("lottie_typing.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Настройки страницы
|
29 |
+
st.set_page_config(page_title="GPT Chat", page_icon="🤖", layout="wide")
|
30 |
|
31 |
+
# Стили CSS для красивого интерфейса
|
32 |
+
st.markdown("""
|
33 |
+
<style>
|
34 |
+
.main {
|
35 |
+
background-color: #f0f0f5;
|
36 |
+
font-family: 'Montserrat', sans-serif;
|
37 |
+
}
|
38 |
+
.stTextInput > div > input {
|
39 |
+
background-color: #e0e0eb;
|
40 |
+
border: 1px solid #d1d1e0;
|
41 |
+
border-radius: 10px;
|
42 |
+
padding: 10px;
|
43 |
+
font-size: 18px;
|
44 |
+
}
|
45 |
+
.stButton > button {
|
46 |
+
background-color: #4CAF50;
|
47 |
+
color: white;
|
48 |
+
border: none;
|
49 |
+
border-radius: 10px;
|
50 |
+
padding: 10px 20px;
|
51 |
+
font-size: 18px;
|
52 |
+
transition: background-color 0.3s ease;
|
53 |
+
}
|
54 |
+
.stButton > button:hover {
|
55 |
+
background-color: #45a049;
|
56 |
+
}
|
57 |
+
.st-chatbox {
|
58 |
+
background-color: #f7f7f7;
|
59 |
+
border-radius: 10px;
|
60 |
+
padding: 10px;
|
61 |
+
margin: 10px 0;
|
62 |
+
}
|
63 |
+
.st-chatbox-user {
|
64 |
+
text-align: right;
|
65 |
+
color: blue;
|
66 |
+
}
|
67 |
+
.st-chatbox-bot {
|
68 |
+
text-align: left;
|
69 |
+
color: green;
|
70 |
+
}
|
71 |
+
</style>
|
72 |
+
""", unsafe_allow_html=True)
|
73 |
|
74 |
+
# Заголовок
|
75 |
+
st.title("Chat with GPT 🤖")
|
76 |
|
77 |
+
# Показ анимации в верхней части страницы
|
78 |
+
st_lottie(lottie_hello, height=200, key="hello")
|
79 |
|
80 |
+
# Форма для ввода сообщения пользователя
|
81 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
82 |
+
user_message = st.text_input("You:", placeholder="Type your message here...")
|
83 |
+
submit_button = st.form_submit_button(label="Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
# Отправка сообщения пользователя
|
86 |
+
if submit_button and user_message:
|
87 |
+
# Добавление сообщения пользователя в историю
|
88 |
+
st.session_state['messages'].append({"role": "user", "content": user_message})
|
89 |
+
|
90 |
+
# Показ анимации "бот печатает" перед ответом
|
91 |
+
with st.spinner('GPT is typing...'):
|
92 |
+
st_lottie(lottie_typing, height=100, key="typing")
|
93 |
|
94 |
+
# Получение ответа от GPT
|
95 |
+
bot_message = get_gpt_response(st.session_state['messages'])
|
96 |
+
|
97 |
+
# Добавление ответа бота в историю
|
98 |
+
st.session_state['messages'].append({"role": "assistant", "content": bot_message})
|
99 |
|
100 |
+
# Отображение всех сообщений
|
101 |
+
for message in st.session_state['messages']:
|
102 |
+
if message['role'] == 'user':
|
103 |
+
st.markdown(f"<div class='st-chatbox st-chatbox-user'><b>You:</b> {message['content']}</div>", unsafe_allow_html=True)
|
104 |
+
else:
|
105 |
+
st.markdown(f"<div class='st-chatbox st-chatbox-bot'><b>GPT:</b> {message['content']}</div>", unsafe_allow_html=True)
|