Spaces:
Sleeping
Sleeping
File size: 3,589 Bytes
b973e27 2965cda 2f71b8a 2965cda 2f71b8a 2965cda b973e27 2965cda b973e27 2965cda b973e27 2965cda b973e27 2965cda b973e27 2965cda 2f71b8a 2965cda d882819 2965cda b973e27 2965cda b973e27 2965cda 2f71b8a 2965cda b973e27 2965cda d882819 2965cda 2f71b8a 2965cda |
1 2 3 4 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
import streamlit as st
import g4f
from streamlit_lottie import st_lottie
import json
# Функция для загрузки анимаций Lottie
def load_lottiefile(filepath: str):
with open(filepath, "r") as f:
return json.load(f)
# Функция для отправки сообщений в GPT
def get_gpt_response(messages):
client = g4f.Client()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
)
return response.choices[0].message.content
# Инициализация списка сообщений
if 'messages' not in st.session_state:
st.session_state['messages'] = [{"role": "user", "content": "Hello"}]
# Загрузка анимаций Lottie
lottie_hello = load_lottiefile("lottie_hello.json")
lottie_typing = load_lottiefile("lottie_typing.json")
# Настройки страницы
st.set_page_config(page_title="GPT Chat", page_icon="🤖", layout="wide")
# Стили CSS для красивого интерфейса
st.markdown("""
<style>
.main {
background-color: #f0f0f5;
font-family: 'Montserrat', sans-serif;
}
.stTextInput > div > input {
background-color: #e0e0eb;
border: 1px solid #d1d1e0;
border-radius: 10px;
padding: 10px;
font-size: 18px;
}
.stButton > button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 10px;
padding: 10px 20px;
font-size: 18px;
transition: background-color 0.3s ease;
}
.stButton > button:hover {
background-color: #45a049;
}
.st-chatbox {
background-color: #f7f7f7;
border-radius: 10px;
padding: 10px;
margin: 10px 0;
}
.st-chatbox-user {
text-align: right;
color: blue;
}
.st-chatbox-bot {
text-align: left;
color: green;
}
</style>
""", unsafe_allow_html=True)
# Заголовок
st.title("Chat with GPT 🤖")
# Показ анимации в верхней части страницы
st_lottie(lottie_hello, height=200, key="hello")
# Форма для ввода сообщения пользователя
with st.form(key="chat_form", clear_on_submit=True):
user_message = st.text_input("You:", placeholder="Type your message here...")
submit_button = st.form_submit_button(label="Send")
# Отправка сообщения пользователя
if submit_button and user_message:
# Добавление сообщения пользователя в историю
st.session_state['messages'].append({"role": "user", "content": user_message})
# Показ анимации "бот печатает" перед ответом
with st.spinner('GPT is typing...'):
st_lottie(lottie_typing, height=100, key="typing")
# Получение ответа от GPT
bot_message = get_gpt_response(st.session_state['messages'])
# Добавление ответа бота в историю
st.session_state['messages'].append({"role": "assistant", "content": bot_message})
# Отображение всех сообщений
for message in st.session_state['messages']:
if message['role'] == 'user':
st.markdown(f"<div class='st-chatbox st-chatbox-user'><b>You:</b> {message['content']}</div>", unsafe_allow_html=True)
else:
st.markdown(f"<div class='st-chatbox st-chatbox-bot'><b>GPT:</b> {message['content']}</div>", unsafe_allow_html=True) |