Update app.py
Browse files
app.py
CHANGED
|
@@ -74,7 +74,7 @@ import requests
|
|
| 74 |
import os
|
| 75 |
import gradio as gr
|
| 76 |
|
| 77 |
-
# Fetch the API token from environment variable
|
| 78 |
API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 79 |
if not API_TOKEN:
|
| 80 |
raise ValueError("HF_API_TOKEN not found. Please set it in the Space Secrets.")
|
|
@@ -82,64 +82,228 @@ if not API_TOKEN:
|
|
| 82 |
# DeepSeek-R1 model endpoint
|
| 83 |
MODEL_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
| 84 |
|
|
|
|
| 85 |
HEADERS = {
|
| 86 |
"Authorization": f"Bearer {API_TOKEN}",
|
| 87 |
"Content-Type": "application/json"
|
| 88 |
}
|
| 89 |
|
| 90 |
def query_deepseek(prompt):
|
| 91 |
-
"""Send a prompt to DeepSeek-R1 and get a response
|
| 92 |
-
# Add explicit English instruction to the prompt
|
| 93 |
-
english_prompt = f"{prompt}\nPlease respond in English only:"
|
| 94 |
-
|
| 95 |
payload = {
|
| 96 |
-
"inputs":
|
| 97 |
"parameters": {
|
| 98 |
-
"max_new_tokens": 100,
|
| 99 |
-
"temperature": 0.
|
| 100 |
-
"top_p": 0.9
|
| 101 |
-
"do_sample": True,
|
| 102 |
-
"response_format": "text"
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
try:
|
| 107 |
response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
|
| 108 |
-
response.raise_for_status()
|
| 109 |
result = response.json()
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# Clean up the response by removing prompt and instructions
|
| 113 |
-
if "Please respond in English only:" in generated_text:
|
| 114 |
-
generated_text = generated_text.split("Please respond in English only:")[1].strip()
|
| 115 |
-
if "Assistant:" in generated_text:
|
| 116 |
-
generated_text = generated_text.split("Assistant:")[-1].strip()
|
| 117 |
-
|
| 118 |
-
return generated_text
|
| 119 |
except requests.exceptions.RequestException as e:
|
| 120 |
-
return f"Error:
|
| 121 |
except (KeyError, IndexError):
|
| 122 |
return "Error: Unexpected response format from API"
|
| 123 |
|
| 124 |
def chat_interface(user_input, history):
|
| 125 |
"""Handle chatbot interaction with Gradio."""
|
| 126 |
-
prompt = f"User: {user_input}\nAssistant:
|
| 127 |
response = query_deepseek(prompt)
|
| 128 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
interface = gr.ChatInterface(
|
| 132 |
fn=chat_interface,
|
| 133 |
-
title="
|
| 134 |
-
description="
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
textbox=gr.Textbox(
|
| 138 |
-
placeholder="Send a message...",
|
| 139 |
-
container=False,
|
| 140 |
-
autofocus=True
|
| 141 |
-
),
|
| 142 |
)
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
-
interface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
import os
|
| 75 |
import gradio as gr
|
| 76 |
|
| 77 |
+
# Fetch the API token from the environment variable set in Hugging Face Space Secrets
|
| 78 |
API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 79 |
if not API_TOKEN:
|
| 80 |
raise ValueError("HF_API_TOKEN not found. Please set it in the Space Secrets.")
|
|
|
|
| 82 |
# DeepSeek-R1 model endpoint
|
| 83 |
MODEL_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
| 84 |
|
| 85 |
+
# Headers for authentication
|
| 86 |
HEADERS = {
|
| 87 |
"Authorization": f"Bearer {API_TOKEN}",
|
| 88 |
"Content-Type": "application/json"
|
| 89 |
}
|
| 90 |
|
| 91 |
def query_deepseek(prompt):
|
| 92 |
+
"""Send a prompt to DeepSeek-R1 and get a response."""
|
|
|
|
|
|
|
|
|
|
| 93 |
payload = {
|
| 94 |
+
"inputs": prompt,
|
| 95 |
"parameters": {
|
| 96 |
+
"max_new_tokens": 100, # Limit response length
|
| 97 |
+
"temperature": 0.6, # Control creativity
|
| 98 |
+
"top_p": 0.9 # Nucleus sampling
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
try:
|
| 103 |
response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
|
| 104 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 105 |
result = response.json()
|
| 106 |
+
return result[0]["generated_text"].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
except requests.exceptions.RequestException as e:
|
| 108 |
+
return f"Error: Could not connect to the API - {str(e)}"
|
| 109 |
except (KeyError, IndexError):
|
| 110 |
return "Error: Unexpected response format from API"
|
| 111 |
|
| 112 |
def chat_interface(user_input, history):
|
| 113 |
"""Handle chatbot interaction with Gradio."""
|
| 114 |
+
prompt = f"User: {user_input}\nAssistant: "
|
| 115 |
response = query_deepseek(prompt)
|
| 116 |
+
return response
|
| 117 |
+
|
| 118 |
+
css = """
|
| 119 |
+
@keyframes gradient {
|
| 120 |
+
0% { background-position: 0% 50%; }
|
| 121 |
+
50% { background-position: 100% 50%; }
|
| 122 |
+
100% { background-position: 0% 50%; }
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
body {
|
| 126 |
+
background: linear-gradient(-45deg, #0f172a, #581c87, #1e1b4b, #0f172a);
|
| 127 |
+
background-size: 400% 400%;
|
| 128 |
+
animation: gradient 15s ease infinite;
|
| 129 |
+
color: #ffffff;
|
| 130 |
+
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
.container {
|
| 134 |
+
max-width: 800px !important;
|
| 135 |
+
margin: 0 auto !important;
|
| 136 |
+
padding: 20px !important;
|
| 137 |
+
position: relative !important;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
.gr-chat-interface {
|
| 141 |
+
background: rgba(255, 255, 255, 0.05);
|
| 142 |
+
backdrop-filter: blur(10px);
|
| 143 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 144 |
+
border-radius: 24px;
|
| 145 |
+
padding: 25px;
|
| 146 |
+
box-shadow:
|
| 147 |
+
0 8px 32px rgba(0, 0, 0, 0.2),
|
| 148 |
+
0 0 0 1px rgba(255, 255, 255, 0.05);
|
| 149 |
+
position: relative;
|
| 150 |
+
overflow: hidden;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
.gr-chat-interface::before {
|
| 154 |
+
content: '';
|
| 155 |
+
position: absolute;
|
| 156 |
+
top: -50%;
|
| 157 |
+
right: -50%;
|
| 158 |
+
width: 100%;
|
| 159 |
+
height: 100%;
|
| 160 |
+
background: radial-gradient(circle, rgba(147, 51, 234, 0.1) 0%, transparent 70%);
|
| 161 |
+
z-index: -1;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.gr-chat-interface::after {
|
| 165 |
+
content: '';
|
| 166 |
+
position: absolute;
|
| 167 |
+
bottom: -50%;
|
| 168 |
+
left: -50%;
|
| 169 |
+
width: 100%;
|
| 170 |
+
height: 100%;
|
| 171 |
+
background: radial-gradient(circle, rgba(59, 130, 246, 0.1) 0%, transparent 70%);
|
| 172 |
+
z-index: -1;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.title {
|
| 176 |
+
background: linear-gradient(to right, #c084fc, #f0abfc, #93c5fd);
|
| 177 |
+
-webkit-background-clip: text;
|
| 178 |
+
background-clip: text;
|
| 179 |
+
color: transparent;
|
| 180 |
+
font-size: 2.5em;
|
| 181 |
+
text-align: center;
|
| 182 |
+
margin-bottom: 10px;
|
| 183 |
+
font-weight: bold;
|
| 184 |
+
animation: gradient 6s ease infinite;
|
| 185 |
+
background-size: 200% auto;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
.description {
|
| 189 |
+
color: rgba(255, 255, 255, 0.7);
|
| 190 |
+
text-align: center;
|
| 191 |
+
font-size: 1.1em;
|
| 192 |
+
margin-bottom: 30px;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
.chatbot {
|
| 196 |
+
background: linear-gradient(to bottom, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.2));
|
| 197 |
+
border: 1px solid rgba(255, 255, 255, 0.05);
|
| 198 |
+
border-radius: 16px;
|
| 199 |
+
padding: 20px;
|
| 200 |
+
margin-bottom: 20px;
|
| 201 |
+
height: 500px;
|
| 202 |
+
overflow-y: auto;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.message {
|
| 206 |
+
padding: 12px 20px;
|
| 207 |
+
margin: 8px 0;
|
| 208 |
+
border-radius: 16px;
|
| 209 |
+
animation: fadeIn 0.3s ease-out;
|
| 210 |
+
backdrop-filter: blur(5px);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
@keyframes fadeIn {
|
| 214 |
+
from {
|
| 215 |
+
opacity: 0;
|
| 216 |
+
transform: translateY(10px);
|
| 217 |
+
}
|
| 218 |
+
to {
|
| 219 |
+
opacity: 1;
|
| 220 |
+
transform: translateY(0);
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
.user-message {
|
| 225 |
+
background: linear-gradient(to right, rgba(147, 51, 234, 0.1), rgba(236, 72, 153, 0.1));
|
| 226 |
+
border-left: 4px solid #c084fc;
|
| 227 |
+
margin-left: 20px;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
.bot-message {
|
| 231 |
+
background: linear-gradient(to right, rgba(59, 130, 246, 0.1), rgba(14, 165, 233, 0.1));
|
| 232 |
+
border-right: 4px solid #60a5fa;
|
| 233 |
+
margin-right: 20px;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
input[type="text"] {
|
| 237 |
+
background: rgba(0, 0, 0, 0.2) !important;
|
| 238 |
+
border: 2px solid rgba(255, 255, 255, 0.1) !important;
|
| 239 |
+
border-radius: 12px !important;
|
| 240 |
+
color: #ffffff !important;
|
| 241 |
+
padding: 15px !important;
|
| 242 |
+
font-size: 1.1em !important;
|
| 243 |
+
transition: all 0.3s ease !important;
|
| 244 |
+
backdrop-filter: blur(5px) !important;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
input[type="text"]:focus {
|
| 248 |
+
border-color: #c084fc !important;
|
| 249 |
+
box-shadow: 0 0 15px rgba(147, 51, 234, 0.2) !important;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
#submit_btn {
|
| 253 |
+
background: linear-gradient(135deg, #c084fc 0%, #60a5fa 100%) !important;
|
| 254 |
+
color: #ffffff !important;
|
| 255 |
+
border: none !important;
|
| 256 |
+
padding: 12px 30px !important;
|
| 257 |
+
border-radius: 12px !important;
|
| 258 |
+
font-size: 1.1em !important;
|
| 259 |
+
font-weight: 600 !important;
|
| 260 |
+
transition: all 0.3s ease !important;
|
| 261 |
+
text-transform: uppercase !important;
|
| 262 |
+
letter-spacing: 1px !important;
|
| 263 |
+
backdrop-filter: blur(5px) !important;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
#submit_btn:hover {
|
| 267 |
+
transform: translateY(-2px) !important;
|
| 268 |
+
box-shadow: 0 5px 15px rgba(147, 51, 234, 0.3) !important;
|
| 269 |
+
background: linear-gradient(135deg, #a855f7 0%, #3b82f6 100%) !important;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
#submit_btn:active {
|
| 273 |
+
transform: translateY(1px) !important;
|
| 274 |
+
}
|
| 275 |
|
| 276 |
+
/* Custom Scrollbar */
|
| 277 |
+
::-webkit-scrollbar {
|
| 278 |
+
width: 8px;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
::-webkit-scrollbar-track {
|
| 282 |
+
background: rgba(0, 0, 0, 0.2);
|
| 283 |
+
border-radius: 4px;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
::-webkit-scrollbar-thumb {
|
| 287 |
+
background: linear-gradient(to bottom, #c084fc, #60a5fa);
|
| 288 |
+
border-radius: 4px;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
::-webkit-scrollbar-thumb:hover {
|
| 292 |
+
background: linear-gradient(to bottom, #a855f7, #3b82f6);
|
| 293 |
+
}
|
| 294 |
+
"""
|
| 295 |
+
|
| 296 |
+
# Create Gradio interface (no unsupported arguments)
|
| 297 |
interface = gr.ChatInterface(
|
| 298 |
fn=chat_interface,
|
| 299 |
+
title="LocoBot: DeepSeek-R1 Chatbot",
|
| 300 |
+
description="Chat with DeepSeek-R1 powered by Hugging Face Inference API.",
|
| 301 |
+
theme="default",
|
| 302 |
+
css=css
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
)
|
| 304 |
|
| 305 |
if __name__ == "__main__":
|
| 306 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
|