Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from openai import OpenAI
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
)
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def chat_with_deepseek(message, history):
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
# Convert Gradio history
|
| 15 |
messages = []
|
| 16 |
for user_msg, bot_msg in history:
|
| 17 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -75,7 +86,7 @@ with gr.Blocks(theme="soft", css="""
|
|
| 75 |
)
|
| 76 |
submit_btn = gr.Button("Send", elem_id="submitbtn")
|
| 77 |
|
| 78 |
-
#
|
| 79 |
submit_btn.click(
|
| 80 |
fn=chat_with_deepseek,
|
| 81 |
inputs=[user_input, chatbot],
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
+
try:
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
except ImportError:
|
| 7 |
+
raise RuntimeError("⚠️ Missing dependency. Make sure 'openai' is listed in requirements.txt")
|
|
|
|
| 8 |
|
| 9 |
+
# Check if HF_TOKEN exists
|
| 10 |
+
api_key = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
client = None
|
| 13 |
+
if api_key:
|
| 14 |
+
client = OpenAI(
|
| 15 |
+
base_url="https://router.huggingface.co/v1",
|
| 16 |
+
api_key=api_key,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Function to handle chat
|
| 20 |
def chat_with_deepseek(message, history):
|
| 21 |
+
if not client:
|
| 22 |
+
return "❌ HF_TOKEN not found. Please set it in 'Settings → Repository secrets' of your Space."
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
+
# Convert Gradio history to OpenAI messages format
|
| 26 |
messages = []
|
| 27 |
for user_msg, bot_msg in history:
|
| 28 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 86 |
)
|
| 87 |
submit_btn = gr.Button("Send", elem_id="submitbtn")
|
| 88 |
|
| 89 |
+
# Bind events
|
| 90 |
submit_btn.click(
|
| 91 |
fn=chat_with_deepseek,
|
| 92 |
inputs=[user_input, chatbot],
|