|
import gradio as gr |
|
from openai import OpenAI |
|
|
|
|
|
client = OpenAI( |
|
api_key="sk-a02694cf3c8640c9ae60428ee2c5a62e", |
|
base_url="https://api.deepseek.com" |
|
) |
|
|
|
|
|
|
|
|
|
def chat_with_deepseek(user_message, history): |
|
""" |
|
:param user_message: текст последнего сообщения от пользователя |
|
:param history: список кортежей ([(user_msg, bot_msg), ...]), |
|
где хранится вся история переписки |
|
:return: обновлённая история с добавленным ответом от deepseek-reasoner |
|
""" |
|
|
|
|
|
messages = [] |
|
for user_msg, bot_msg in history: |
|
if user_msg: |
|
messages.append({"role": "user", "content": user_msg}) |
|
if bot_msg: |
|
messages.append({"role": "assistant", "content": bot_msg}) |
|
|
|
|
|
messages.append({"role": "user", "content": user_message}) |
|
|
|
|
|
try: |
|
response = client.chat.completions.create( |
|
model="deepseek-reasoner", |
|
messages=messages |
|
) |
|
bot_reply = response.choices[0].message.content |
|
except Exception as e: |
|
bot_reply = f"Ошибка при обращении к API: {str(e)}" |
|
|
|
|
|
history.append((user_message, bot_reply)) |
|
return history, history |
|
|
|
|
|
|
|
|
|
with gr.Blocks( |
|
theme=gr.themes.Base( |
|
primary_hue="slate", |
|
secondary_hue="blue", |
|
neutral_hue="slate", |
|
text_size="md", |
|
font=["Arial", "sans-serif"], |
|
), |
|
css=""" |
|
body { |
|
background-color: #111111 !important; |
|
} |
|
.block.block--main { |
|
background-color: #111111 !important; |
|
} |
|
.gradio-container { |
|
color: #ffffff !important; |
|
} |
|
/* Дополнительные правки под темный фон */ |
|
#chatbot { |
|
background-color: #222222 !important; |
|
} |
|
""") as demo: |
|
|
|
gr.Markdown( |
|
"<h1 style='text-align: center; color: #ffffff;'>Чат с deepseek-reasoner</h1>" |
|
"<p style='text-align: center; color: #bbbbbb;'>Тёмная тема, многошаговый диалог</p>" |
|
) |
|
|
|
|
|
chatbot = gr.Chatbot(label="Диалог").style(height=400) |
|
|
|
|
|
msg = gr.Textbox( |
|
label="Ваш вопрос", |
|
placeholder="Напишите сообщение...", |
|
lines=3 |
|
) |
|
|
|
|
|
send_btn = gr.Button("Отправить", variant="primary") |
|
|
|
|
|
state = gr.State([]) |
|
|
|
|
|
|
|
|
|
send_btn.click( |
|
fn=chat_with_deepseek, |
|
inputs=[msg, state], |
|
outputs=[chatbot, state], |
|
scroll_to_output=True |
|
) |
|
|
|
|
|
msg.submit( |
|
fn=chat_with_deepseek, |
|
inputs=[msg, state], |
|
outputs=[chatbot, state], |
|
scroll_to_output=True |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|