Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,17 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
API_URL = "https://api.deepseek.com/v1/chat/completions"
|
| 7 |
-
API_KEY = os.getenv("
|
| 8 |
|
| 9 |
headers = {
|
| 10 |
"Authorization": f"Bearer {API_KEY}",
|
| 11 |
"Content-Type": "application/json"
|
| 12 |
}
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
messages = [
|
| 16 |
-
{"role": "system", "content": "Anda adalah asisten AI yang membantu pengguna."},
|
| 17 |
-
*[{"role": "user", "content": h[0]} for h in history],
|
| 18 |
-
{"role": "user", "content": prompt}
|
| 19 |
-
]
|
| 20 |
|
| 21 |
try:
|
| 22 |
response = requests.post(
|
|
@@ -25,45 +21,34 @@ def chat_with_deepseek(prompt, history=[]):
|
|
| 25 |
json={
|
| 26 |
"model": "deepseek-chat",
|
| 27 |
"messages": messages,
|
| 28 |
-
"temperature": 0.7
|
| 29 |
-
|
| 30 |
-
|
| 31 |
)
|
| 32 |
response.raise_for_status()
|
| 33 |
return response.json()["choices"][0]["message"]["content"]
|
| 34 |
except Exception as e:
|
| 35 |
-
return f"
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
with gr.Blocks(
|
| 39 |
-
gr.Markdown("
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
submit_btn = gr.Button("Kirim")
|
| 46 |
-
clear_btn = gr.Button("Bersihkan")
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
"Jelaskan teori relativitas secara sederhana",
|
| 52 |
-
"Bantu saya buat kode Python untuk web scraping",
|
| 53 |
-
"Apa keunggulan DeepSeek dibanding model lain?"
|
| 54 |
-
],
|
| 55 |
-
inputs=msg
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
# Fungsi handler
|
| 59 |
-
def respond(message, chat_history):
|
| 60 |
-
bot_message = chat_with_deepseek(message, chat_history)
|
| 61 |
-
chat_history.append((message, bot_message))
|
| 62 |
-
return "", chat_history
|
| 63 |
-
|
| 64 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 65 |
-
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 66 |
-
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 67 |
-
|
| 68 |
-
# Untuk Hugging Face Spaces
|
| 69 |
-
demo.launch(debug=True)
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Configuration
|
| 6 |
API_URL = "https://api.deepseek.com/v1/chat/completions"
|
| 7 |
+
API_KEY = os.getenv("sk-cdfd20997de44ba4a7719217b51452ff") # Set in Spaces Secrets
|
| 8 |
|
| 9 |
headers = {
|
| 10 |
"Authorization": f"Bearer {API_KEY}",
|
| 11 |
"Content-Type": "application/json"
|
| 12 |
}
|
| 13 |
|
| 14 |
+
def query_deepseek(prompt, history=None):
|
| 15 |
+
messages = [{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
try:
|
| 18 |
response = requests.post(
|
|
|
|
| 21 |
json={
|
| 22 |
"model": "deepseek-chat",
|
| 23 |
"messages": messages,
|
| 24 |
+
"temperature": 0.7
|
| 25 |
+
},
|
| 26 |
+
timeout=30
|
| 27 |
)
|
| 28 |
response.raise_for_status()
|
| 29 |
return response.json()["choices"][0]["message"]["content"]
|
| 30 |
except Exception as e:
|
| 31 |
+
return f"⚠️ Error: {str(e)}"
|
| 32 |
|
| 33 |
+
# Gradio Interface
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## 🤖 DeepSeek API Chat")
|
| 36 |
|
| 37 |
+
with gr.Tab("Chat"):
|
| 38 |
+
chatbot = gr.Chatbot()
|
| 39 |
+
message = gr.Textbox(label="Your Message")
|
| 40 |
+
send = gr.Button("Send")
|
| 41 |
+
|
| 42 |
+
def respond(message, chat_history):
|
| 43 |
+
bot_message = query_deepseek(message)
|
| 44 |
+
chat_history.append((message, bot_message))
|
| 45 |
+
return "", chat_history
|
| 46 |
+
|
| 47 |
+
message.submit(respond, [message, chatbot], [message, chatbot])
|
| 48 |
+
send.click(respond, [message, chatbot], [message, chatbot])
|
| 49 |
|
| 50 |
+
gr.Markdown("Note: Using official DeepSeek API")
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# For Hugging Face Spaces deployment
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|