File size: 1,402 Bytes
785f434
9c366d9
dc5e6ef
785f434
dc5e6ef
785f434
9c366d9
 
 
 
 
 
785f434
9c366d9
 
 
 
 
 
 
 
 
 
 
 
dc5e6ef
 
 
 
 
9c366d9
 
785f434
9c366d9
dc5e6ef
 
9c366d9
785f434
9c366d9
785f434
9c366d9
785f434
 
 
dc5e6ef
9c366d9
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
import gradio as gr
import requests
import os

API_KEY = os.getenv("100d2c15370546965c6fb5ea7ae2ee3c9d6e4cb5a9f36726ac771fddd4eef9a3")  # Secure method!

def chat_with_deepseek(user_input, history):
    messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
    for user, bot in history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": bot})
    messages.append({"role": "user", "content": user_input})

    response = requests.post(
        "https://api.deepseek.com/v1/chat/completions",
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        },
        json={
            "model": "deepseek-chat",
            "messages": messages,
            "temperature": 0.7
        }
    )

    if response.status_code != 200:
        return history + [(user_input, f"Error {response.status_code}: {response.text}")], history

    reply = response.json()["choices"][0]["message"]["content"]
    history.append((user_input, reply))
    return history, history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(label="Europa", type="messages")
    msg = gr.Textbox(placeholder="Type your question…", show_label=False)
    state = gr.State([])

    msg.submit(chat_with_deepseek, inputs=[msg, state], outputs=[chatbot, state])

demo.launch()