DeepsiteXBolt / app.py
Humzasheikh01's picture
Update app.py
4388077 verified
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()