Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import io
|
| 4 |
-
import contextlib
|
| 5 |
import os
|
| 6 |
|
| 7 |
# API key from Hugging Face Space secret
|
| 8 |
API_KEY = os.environ.get("API_KEY")
|
| 9 |
API_URL = "https://api.deepseek.com/v1/chat/completions" # Replace if different
|
| 10 |
|
| 11 |
-
# DeepSeek V3
|
| 12 |
-
def
|
| 13 |
if not API_KEY:
|
| 14 |
return "β API Key not found. Please set API_KEY in HF Space secrets."
|
| 15 |
|
|
@@ -17,56 +15,52 @@ def deepseek_reply(code_input):
|
|
| 17 |
"Authorization": f"Bearer {API_KEY}",
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
payload = {
|
| 21 |
"model": "deepseek-coder:33b",
|
| 22 |
-
"messages":
|
| 23 |
-
|
| 24 |
-
{"role": "user", "content": code_input}
|
| 25 |
-
],
|
| 26 |
-
"temperature": 0.2,
|
| 27 |
"stream": False
|
| 28 |
}
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 32 |
result = response.json()
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
def execute_code(code_input):
|
| 39 |
-
buffer = io.StringIO()
|
| 40 |
-
try:
|
| 41 |
-
with contextlib.redirect_stdout(buffer):
|
| 42 |
-
exec(code_input, {})
|
| 43 |
-
return buffer.getvalue()
|
| 44 |
except Exception as e:
|
| 45 |
-
return f"β
|
| 46 |
|
| 47 |
-
# Gradio UI setup
|
| 48 |
with gr.Blocks(css="footer {display: none !important}") as demo:
|
| 49 |
gr.Markdown("""
|
| 50 |
-
#
|
| 51 |
-
###
|
| 52 |
|
| 53 |
-
|
| 54 |
""", elem_id="header")
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column(scale=5):
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
with gr.Column(scale=2):
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Action when button is clicked
|
| 69 |
-
run_btn.click(fn=deepseek_reply, inputs=code, outputs=deepseek_out)
|
| 70 |
-
run_btn.click(fn=execute_code, inputs=code, outputs=exec_out)
|
| 71 |
|
| 72 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# API key from Hugging Face Space secret
|
| 6 |
API_KEY = os.environ.get("API_KEY")
|
| 7 |
API_URL = "https://api.deepseek.com/v1/chat/completions" # Replace if different
|
| 8 |
|
| 9 |
+
# DeepSeek V3 Chatbot function
|
| 10 |
+
def deepseek_chat(user_message, chat_history):
|
| 11 |
if not API_KEY:
|
| 12 |
return "β API Key not found. Please set API_KEY in HF Space secrets."
|
| 13 |
|
|
|
|
| 15 |
"Authorization": f"Bearer {API_KEY}",
|
| 16 |
"Content-Type": "application/json"
|
| 17 |
}
|
| 18 |
+
|
| 19 |
+
# Append the user message to the conversation history
|
| 20 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 21 |
+
|
| 22 |
payload = {
|
| 23 |
"model": "deepseek-coder:33b",
|
| 24 |
+
"messages": chat_history,
|
| 25 |
+
"temperature": 0.7,
|
|
|
|
|
|
|
|
|
|
| 26 |
"stream": False
|
| 27 |
}
|
| 28 |
|
| 29 |
try:
|
| 30 |
+
# Make request to the DeepSeek V3 API for the chatbot response
|
| 31 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 32 |
result = response.json()
|
| 33 |
+
|
| 34 |
+
# Extract the AI response
|
| 35 |
+
ai_message = result["choices"][0]["message"]["content"]
|
| 36 |
+
|
| 37 |
+
# Add the AI's response to the chat history
|
| 38 |
+
chat_history.append({"role": "assistant", "content": ai_message})
|
| 39 |
|
| 40 |
+
return ai_message, chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
+
return f"β DeepSeek Error: {e}", chat_history
|
| 43 |
|
| 44 |
+
# Gradio UI setup for the chatbot
|
| 45 |
with gr.Blocks(css="footer {display: none !important}") as demo:
|
| 46 |
gr.Markdown("""
|
| 47 |
+
# π€ DeepSeek V3 Chatbot
|
| 48 |
+
### Chat with the AI powered by DeepSeek V3!
|
| 49 |
|
| 50 |
+
Type a message and start chatting with the AI.
|
| 51 |
""", elem_id="header")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
with gr.Column(scale=5):
|
| 55 |
+
# Text input for user message
|
| 56 |
+
user_message = gr.Textbox(label="Your Message", placeholder="Type something...", interactive=True)
|
| 57 |
with gr.Column(scale=2):
|
| 58 |
+
# Chat history and bot response
|
| 59 |
+
chatbot_output = gr.Chatbot(label="Chat History", elem_id="chatbot-output")
|
| 60 |
+
# Submit button
|
| 61 |
+
submit_button = gr.Button("Send", elem_id="send-button")
|
| 62 |
+
|
| 63 |
+
# Define the action when the button is clicked
|
| 64 |
+
submit_button.click(fn=deepseek_chat, inputs=[user_message, chatbot_output], outputs=[chatbot_output, chatbot_output])
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
demo.launch()
|