File size: 2,272 Bytes
2551578
43bea2e
 
2551578
43bea2e
 
 
 
4388077
 
43bea2e
 
 
 
 
 
 
4388077
 
 
 
43bea2e
 
4388077
 
43bea2e
 
 
 
4388077
43bea2e
 
4388077
 
 
 
 
 
43bea2e
4388077
43bea2e
4388077
43bea2e
4388077
43bea2e
4dc5c73
4388077
 
4dc5c73
4388077
4dc5c73
2551578
43bea2e
4dc5c73
4388077
 
4dc5c73
4388077
 
 
 
 
 
 
43bea2e
 
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
import gradio as gr
import requests
import os

# API key from Hugging Face Space secret
API_KEY = os.environ.get("API_KEY")
API_URL = "https://api.deepseek.com/v1/chat/completions"  # Replace if different

# DeepSeek V3 Chatbot function
def deepseek_chat(user_message, chat_history):
    if not API_KEY:
        return "❌ API Key not found. Please set API_KEY in HF Space secrets."

    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    # Append the user message to the conversation history
    chat_history.append({"role": "user", "content": user_message})

    payload = {
        "model": "deepseek-coder:33b",
        "messages": chat_history,
        "temperature": 0.7,
        "stream": False
    }

    try:
        # Make request to the DeepSeek V3 API for the chatbot response
        response = requests.post(API_URL, headers=headers, json=payload)
        result = response.json()
        
        # Extract the AI response
        ai_message = result["choices"][0]["message"]["content"]
        
        # Add the AI's response to the chat history
        chat_history.append({"role": "assistant", "content": ai_message})

        return ai_message, chat_history
    except Exception as e:
        return f"❌ DeepSeek Error: {e}", chat_history

# Gradio UI setup for the chatbot
with gr.Blocks(css="footer {display: none !important}") as demo:
    gr.Markdown("""
    # 🤖 DeepSeek V3 Chatbot
    ### Chat with the AI powered by DeepSeek V3!
    
    Type a message and start chatting with the AI.
    """, elem_id="header")
    
    with gr.Row():
        with gr.Column(scale=5):
            # Text input for user message
            user_message = gr.Textbox(label="Your Message", placeholder="Type something...", interactive=True)
        with gr.Column(scale=2):
            # Chat history and bot response
            chatbot_output = gr.Chatbot(label="Chat History", elem_id="chatbot-output")
            # Submit button
            submit_button = gr.Button("Send", elem_id="send-button")
    
    # Define the action when the button is clicked
    submit_button.click(fn=deepseek_chat, inputs=[user_message, chatbot_output], outputs=[chatbot_output, chatbot_output])

demo.launch()