Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
from typing import List, Tuple
|
| 5 |
|
| 6 |
-
# Configuration - Set
|
| 7 |
API_URL = "https://api.deepseek.com/v1/chat/completions"
|
| 8 |
-
API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
| 9 |
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {API_KEY}",
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
"""
|
| 17 |
-
Query DeepSeek API with proper error handling
|
| 18 |
-
"""
|
| 19 |
-
if chat_history is None:
|
| 20 |
-
chat_history = []
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
-
#
|
| 24 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 25 |
-
for user_msg, bot_msg in
|
| 26 |
messages.extend([
|
| 27 |
{"role": "user", "content": user_msg},
|
| 28 |
{"role": "assistant", "content": bot_msg}
|
|
@@ -43,49 +38,55 @@ def query_deepseek_api(message: str, chat_history: List[Tuple[str, str]] = None)
|
|
| 43 |
response.raise_for_status()
|
| 44 |
return response.json()["choices"][0]["message"]["content"]
|
| 45 |
|
| 46 |
-
except requests.exceptions.RequestException as e:
|
| 47 |
-
return f"🚨 API Error: {str(e)}"
|
| 48 |
except Exception as e:
|
| 49 |
-
return f"⚠️
|
| 50 |
|
| 51 |
-
# Gradio
|
| 52 |
-
def
|
| 53 |
-
with gr.Blocks(
|
| 54 |
-
gr.Markdown("
|
| 55 |
|
| 56 |
-
chatbot = gr.Chatbot(
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
chat_history.append((message, bot_message))
|
| 66 |
return "", chat_history
|
| 67 |
-
|
| 68 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
gr.Examples(
|
| 73 |
examples=[
|
| 74 |
-
"Explain quantum computing simply",
|
| 75 |
-
"Write a Python function for Fibonacci
|
| 76 |
-
"What
|
| 77 |
],
|
| 78 |
inputs=msg
|
| 79 |
)
|
| 80 |
|
| 81 |
return demo
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
app = create_chat_interface()
|
| 85 |
-
|
| 86 |
if __name__ == "__main__":
|
|
|
|
| 87 |
app.launch(
|
| 88 |
server_name="0.0.0.0",
|
| 89 |
server_port=7860,
|
| 90 |
-
|
|
|
|
| 91 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
from typing import List, Tuple, Any
|
| 5 |
|
| 6 |
+
# Configuration - Set API key in Hugging Face Spaces Secrets
|
| 7 |
API_URL = "https://api.deepseek.com/v1/chat/completions"
|
| 8 |
+
API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
| 9 |
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {API_KEY}",
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
| 14 |
|
| 15 |
+
def query_deepseek(message: str, history: List[Tuple[str, str]]) -> str:
|
| 16 |
+
"""Query DeepSeek API with robust error handling"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
+
# Build conversation history
|
| 19 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 20 |
+
for user_msg, bot_msg in history:
|
| 21 |
messages.extend([
|
| 22 |
{"role": "user", "content": user_msg},
|
| 23 |
{"role": "assistant", "content": bot_msg}
|
|
|
|
| 38 |
response.raise_for_status()
|
| 39 |
return response.json()["choices"][0]["message"]["content"]
|
| 40 |
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
+
return f"⚠️ Error: {str(e)}"
|
| 43 |
|
| 44 |
+
# Gradio interface with explicit component definitions
|
| 45 |
+
def create_interface():
|
| 46 |
+
with gr.Blocks(analytics_enabled=False) as demo: # Disable analytics to prevent schema issues
|
| 47 |
+
gr.Markdown("## 🤖 DeepSeek API Chat")
|
| 48 |
|
| 49 |
+
chatbot = gr.Chatbot(
|
| 50 |
+
label="Conversation",
|
| 51 |
+
bubble_full_width=False
|
| 52 |
+
)
|
| 53 |
+
msg = gr.Textbox(
|
| 54 |
+
label="Your Message",
|
| 55 |
+
placeholder="Type here...",
|
| 56 |
+
scale=7
|
| 57 |
+
)
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
+
submit = gr.Button("Send", variant="primary")
|
| 61 |
+
clear = gr.Button("Clear")
|
| 62 |
+
|
| 63 |
+
# Fixed chat handler with type-safe implementation
|
| 64 |
+
def respond(message: str, chat_history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
| 65 |
+
bot_message = query_deepseek(message, chat_history)
|
| 66 |
chat_history.append((message, bot_message))
|
| 67 |
return "", chat_history
|
| 68 |
+
|
| 69 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 70 |
+
submit.click(respond, [msg, chatbot], [msg, chatbot])
|
| 71 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 72 |
|
| 73 |
gr.Examples(
|
| 74 |
examples=[
|
| 75 |
+
["Explain quantum computing simply"],
|
| 76 |
+
["Write a Python function for Fibonacci"],
|
| 77 |
+
["What makes DeepSeek special?"]
|
| 78 |
],
|
| 79 |
inputs=msg
|
| 80 |
)
|
| 81 |
|
| 82 |
return demo
|
| 83 |
|
| 84 |
+
# Launch with explicit configuration
|
|
|
|
|
|
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
app = create_interface()
|
| 87 |
app.launch(
|
| 88 |
server_name="0.0.0.0",
|
| 89 |
server_port=7860,
|
| 90 |
+
show_error=True,
|
| 91 |
+
enable_queue=True # Prevents timeout issues
|
| 92 |
)
|