|
import streamlit as st |
|
from openai import OpenAI |
|
|
|
|
|
st.set_page_config(page_title="Чат с deepseek-reasoner", layout="wide") |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
/* Убираем верхний хедер и хамбёргер-меню Streamlit (по желанию) */ |
|
#MainMenu, header, footer {visibility: hidden;} |
|
|
|
/* Тёмный фон всего приложения */ |
|
body { |
|
background-color: #181818; |
|
color: #FFFFFF; |
|
} |
|
|
|
/* Контейнер для всего чата */ |
|
.chat-container { |
|
background-color: #222222; |
|
padding: 20px; |
|
border-radius: 10px; |
|
max-height: 70vh; /* Высота окна для переписки */ |
|
overflow-y: auto; /* Скролл при переполнении */ |
|
margin-bottom: 20px; |
|
} |
|
|
|
/* «Пузырёк» для сообщений пользователя (справа) */ |
|
.user-bubble { |
|
background-color: #005f73; |
|
color: #ffffff; |
|
padding: 10px 15px; |
|
border-radius: 10px; |
|
margin: 10px; |
|
max-width: 60%; |
|
text-align: left; |
|
margin-left: auto; /* прижимает пузырёк к правому краю */ |
|
} |
|
|
|
/* «Пузырёк» для сообщений ассистента (слева) */ |
|
.assistant-bubble { |
|
background-color: #333333; |
|
color: #ffffff; |
|
padding: 10px 15px; |
|
border-radius: 10px; |
|
margin: 10px; |
|
max-width: 60%; |
|
text-align: left; |
|
margin-right: auto; /* прижимает пузырёк к левому краю */ |
|
} |
|
</style> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
|
|
client = OpenAI( |
|
api_key="sk-a02694cf3c8640c9ae60428ee2c5a62e", |
|
base_url="https://api.deepseek.com" |
|
) |
|
|
|
|
|
st.title("Чат с deepseek-reasoner") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state["messages"] = [] |
|
|
|
|
|
def render_chat(): |
|
st.markdown("<div class='chat-container'>", unsafe_allow_html=True) |
|
|
|
|
|
for msg in st.session_state["messages"]: |
|
if msg["role"] == "user": |
|
bubble_class = "user-bubble" |
|
else: |
|
bubble_class = "assistant-bubble" |
|
st.markdown( |
|
f"<div class='{bubble_class}'>{msg['content']}</div>", |
|
unsafe_allow_html=True |
|
) |
|
|
|
st.markdown("</div>", unsafe_allow_html=True) |
|
|
|
|
|
render_chat() |
|
|
|
|
|
with st.container(): |
|
user_input = st.text_area("Напишите сообщение и нажмите Enter или кнопку «Отправить»:", |
|
value="", height=80) |
|
|
|
if st.button("Отправить"): |
|
content = user_input.strip() |
|
if content: |
|
|
|
st.session_state["messages"].append({"role": "user", "content": content}) |
|
|
|
|
|
try: |
|
response = client.chat.completions.create( |
|
model="deepseek-reasoner", |
|
messages=st.session_state["messages"] |
|
) |
|
|
|
assistant_content = response.choices[0].message.content |
|
|
|
|
|
st.session_state["messages"].append( |
|
{"role": "assistant", "content": assistant_content} |
|
) |
|
except Exception as e: |
|
st.error(f"Ошибка при обращении к API: {e}") |
|
|
|
|
|
st.experimental_rerun() |
|
|
|
|