Update app.py
Browse files
app.py
CHANGED
|
@@ -82,7 +82,6 @@ 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 for authentication
|
| 86 |
HEADERS = {
|
| 87 |
"Authorization": f"Bearer {API_TOKEN}",
|
| 88 |
"Content-Type": "application/json"
|
|
@@ -90,12 +89,17 @@ HEADERS = {
|
|
| 90 |
|
| 91 |
def query_deepseek(prompt):
|
| 92 |
"""Send a prompt to DeepSeek-R1 and get a response in English."""
|
|
|
|
|
|
|
|
|
|
| 93 |
payload = {
|
| 94 |
-
"inputs":
|
| 95 |
"parameters": {
|
| 96 |
"max_new_tokens": 100,
|
| 97 |
"temperature": 0.7,
|
| 98 |
-
"top_p": 0.9
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
|
@@ -105,11 +109,13 @@ def query_deepseek(prompt):
|
|
| 105 |
result = response.json()
|
| 106 |
generated_text = result[0]["generated_text"].strip()
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
| 109 |
if "Assistant:" in generated_text:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return generated_text
|
| 113 |
except requests.exceptions.RequestException as e:
|
| 114 |
return f"Error: API request failed - {str(e)}"
|
| 115 |
except (KeyError, IndexError):
|
|
@@ -117,195 +123,16 @@ def query_deepseek(prompt):
|
|
| 117 |
|
| 118 |
def chat_interface(user_input, history):
|
| 119 |
"""Handle chatbot interaction with Gradio."""
|
| 120 |
-
prompt = f"User: {user_input}\nAssistant: "
|
| 121 |
response = query_deepseek(prompt)
|
| 122 |
return response if response else "I apologize, but I couldn't generate a response. Please try again!"
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
css = """
|
| 126 |
-
@keyframes gradient {
|
| 127 |
-
0% { background-position: 0% 50%; }
|
| 128 |
-
50% { background-position: 100% 50%; }
|
| 129 |
-
100% { background-position: 0% 50%; }
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
body {
|
| 133 |
-
background: linear-gradient(-45deg, #0f172a, #581c87, #1e1b4b, #0f172a);
|
| 134 |
-
background-size: 400% 400%;
|
| 135 |
-
animation: gradient 15s ease infinite;
|
| 136 |
-
color: #ffffff;
|
| 137 |
-
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
-
.container {
|
| 141 |
-
max-width: 800px !important;
|
| 142 |
-
margin: 0 auto !important;
|
| 143 |
-
padding: 20px !important;
|
| 144 |
-
position: relative !important;
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
.gr-chat-interface {
|
| 148 |
-
background: rgba(255, 255, 255, 0.05);
|
| 149 |
-
backdrop-filter: blur(10px);
|
| 150 |
-
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 151 |
-
border-radius: 24px;
|
| 152 |
-
padding: 25px;
|
| 153 |
-
box-shadow:
|
| 154 |
-
0 8px 32px rgba(0, 0, 0, 0.2),
|
| 155 |
-
0 0 0 1px rgba(255, 255, 255, 0.05);
|
| 156 |
-
position: relative;
|
| 157 |
-
overflow: hidden;
|
| 158 |
-
}
|
| 159 |
-
|
| 160 |
-
.gr-chat-interface::before {
|
| 161 |
-
content: '';
|
| 162 |
-
position: absolute;
|
| 163 |
-
top: -50%;
|
| 164 |
-
right: -50%;
|
| 165 |
-
width: 100%;
|
| 166 |
-
height: 100%;
|
| 167 |
-
background: radial-gradient(circle, rgba(147, 51, 234, 0.1) 0%, transparent 70%);
|
| 168 |
-
z-index: -1;
|
| 169 |
-
}
|
| 170 |
-
|
| 171 |
-
.gr-chat-interface::after {
|
| 172 |
-
content: '';
|
| 173 |
-
position: absolute;
|
| 174 |
-
bottom: -50%;
|
| 175 |
-
left: -50%;
|
| 176 |
-
width: 100%;
|
| 177 |
-
height: 100%;
|
| 178 |
-
background: radial-gradient(circle, rgba(59, 130, 246, 0.1) 0%, transparent 70%);
|
| 179 |
-
z-index: -1;
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
.title {
|
| 183 |
-
background: linear-gradient(to right, #c084fc, #f0abfc, #93c5fd);
|
| 184 |
-
-webkit-background-clip: text;
|
| 185 |
-
background-clip: text;
|
| 186 |
-
color: transparent;
|
| 187 |
-
font-size: 2.5em;
|
| 188 |
-
text-align: center;
|
| 189 |
-
margin-bottom: 10px;
|
| 190 |
-
font-weight: bold;
|
| 191 |
-
animation: gradient 6s ease infinite;
|
| 192 |
-
background-size: 200% auto;
|
| 193 |
-
}
|
| 194 |
-
|
| 195 |
-
.description {
|
| 196 |
-
color: rgba(255, 255, 255, 0.7);
|
| 197 |
-
text-align: center;
|
| 198 |
-
font-size: 1.1em;
|
| 199 |
-
margin-bottom: 30px;
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
.chatbot {
|
| 203 |
-
background: linear-gradient(to bottom, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.2));
|
| 204 |
-
border: 1px solid rgba(255, 255, 255, 0.05);
|
| 205 |
-
border-radius: 16px;
|
| 206 |
-
padding: 20px;
|
| 207 |
-
margin-bottom: 20px;
|
| 208 |
-
height: 500px;
|
| 209 |
-
overflow-y: auto;
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
.message {
|
| 213 |
-
padding: 12px 20px;
|
| 214 |
-
margin: 8px 0;
|
| 215 |
-
border-radius: 16px;
|
| 216 |
-
animation: fadeIn 0.3s ease-out;
|
| 217 |
-
backdrop-filter: blur(5px);
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
-
@keyframes fadeIn {
|
| 221 |
-
from {
|
| 222 |
-
opacity: 0;
|
| 223 |
-
transform: translateY(10px);
|
| 224 |
-
}
|
| 225 |
-
to {
|
| 226 |
-
opacity: 1;
|
| 227 |
-
transform: translateY(0);
|
| 228 |
-
}
|
| 229 |
-
}
|
| 230 |
-
|
| 231 |
-
.user-message {
|
| 232 |
-
background: linear-gradient(to right, rgba(147, 51, 234, 0.1), rgba(236, 72, 153, 0.1));
|
| 233 |
-
border-left: 4px solid #c084fc;
|
| 234 |
-
margin-left: 20px;
|
| 235 |
-
}
|
| 236 |
-
|
| 237 |
-
.bot-message {
|
| 238 |
-
background: linear-gradient(to right, rgba(59, 130, 246, 0.1), rgba(14, 165, 233, 0.1));
|
| 239 |
-
border-right: 4px solid #60a5fa;
|
| 240 |
-
margin-right: 20px;
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
input[type="text"] {
|
| 244 |
-
background: rgba(0, 0, 0, 0.2) !important;
|
| 245 |
-
border: 2px solid rgba(255, 255, 255, 0.1) !important;
|
| 246 |
-
border-radius: 12px !important;
|
| 247 |
-
color: #ffffff !important;
|
| 248 |
-
padding: 15px !important;
|
| 249 |
-
font-size: 1.1em !important;
|
| 250 |
-
transition: all 0.3s ease !important;
|
| 251 |
-
backdrop-filter: blur(5px) !important;
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
input[type="text"]:focus {
|
| 255 |
-
border-color: #c084fc !important;
|
| 256 |
-
box-shadow: 0 0 15px rgba(147, 51, 234, 0.2) !important;
|
| 257 |
-
}
|
| 258 |
-
|
| 259 |
-
#submit_btn {
|
| 260 |
-
background: linear-gradient(135deg, #c084fc 0%, #60a5fa 100%) !important;
|
| 261 |
-
color: #ffffff !important;
|
| 262 |
-
border: none !important;
|
| 263 |
-
padding: 12px 30px !important;
|
| 264 |
-
border-radius: 12px !important;
|
| 265 |
-
font-size: 1.1em !important;
|
| 266 |
-
font-weight: 600 !important;
|
| 267 |
-
transition: all 0.3s ease !important;
|
| 268 |
-
text-transform: uppercase !important;
|
| 269 |
-
letter-spacing: 1px !important;
|
| 270 |
-
backdrop-filter: blur(5px) !important;
|
| 271 |
-
}
|
| 272 |
-
|
| 273 |
-
#submit_btn:hover {
|
| 274 |
-
transform: translateY(-2px) !important;
|
| 275 |
-
box-shadow: 0 5px 15px rgba(147, 51, 234, 0.3) !important;
|
| 276 |
-
background: linear-gradient(135deg, #a855f7 0%, #3b82f6 100%) !important;
|
| 277 |
-
}
|
| 278 |
-
|
| 279 |
-
#submit_btn:active {
|
| 280 |
-
transform: translateY(1px) !important;
|
| 281 |
-
}
|
| 282 |
-
|
| 283 |
-
/* Custom Scrollbar */
|
| 284 |
-
::-webkit-scrollbar {
|
| 285 |
-
width: 8px;
|
| 286 |
-
}
|
| 287 |
-
|
| 288 |
-
::-webkit-scrollbar-track {
|
| 289 |
-
background: rgba(0, 0, 0, 0.2);
|
| 290 |
-
border-radius: 4px;
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
::-webkit-scrollbar-thumb {
|
| 294 |
-
background: linear-gradient(to bottom, #c084fc, #60a5fa);
|
| 295 |
-
border-radius: 4px;
|
| 296 |
-
}
|
| 297 |
-
|
| 298 |
-
::-webkit-scrollbar-thumb:hover {
|
| 299 |
-
background: linear-gradient(to bottom, #a855f7, #3b82f6);
|
| 300 |
-
}
|
| 301 |
-
"""
|
| 302 |
-
|
| 303 |
-
# Create Gradio interface with enhanced styling
|
| 304 |
interface = gr.ChatInterface(
|
| 305 |
fn=chat_interface,
|
| 306 |
title="Neural Nexus AI",
|
| 307 |
description="Your gateway to intelligent conversation",
|
| 308 |
-
css=css,
|
| 309 |
submit_btn=gr.Button("Send", elem_id="submit_btn"),
|
| 310 |
textbox=gr.Textbox(
|
| 311 |
placeholder="Send a message...",
|
|
|
|
| 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"
|
|
|
|
| 89 |
|
| 90 |
def query_deepseek(prompt):
|
| 91 |
"""Send a prompt to DeepSeek-R1 and get a response in English."""
|
| 92 |
+
# Add explicit English instruction to the prompt
|
| 93 |
+
english_prompt = f"{prompt}\nPlease respond in English only:"
|
| 94 |
+
|
| 95 |
payload = {
|
| 96 |
+
"inputs": english_prompt,
|
| 97 |
"parameters": {
|
| 98 |
"max_new_tokens": 100,
|
| 99 |
"temperature": 0.7,
|
| 100 |
+
"top_p": 0.9,
|
| 101 |
+
"do_sample": True,
|
| 102 |
+
"response_format": "text"
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
|
|
|
| 109 |
result = response.json()
|
| 110 |
generated_text = result[0]["generated_text"].strip()
|
| 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: API request failed - {str(e)}"
|
| 121 |
except (KeyError, IndexError):
|
|
|
|
| 123 |
|
| 124 |
def chat_interface(user_input, history):
|
| 125 |
"""Handle chatbot interaction with Gradio."""
|
| 126 |
+
prompt = f"User: {user_input}\nAssistant: Generate a response in English to: {user_input}"
|
| 127 |
response = query_deepseek(prompt)
|
| 128 |
return response if response else "I apologize, but I couldn't generate a response. Please try again!"
|
| 129 |
|
| 130 |
+
# Create Gradio interface with the provided CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
interface = gr.ChatInterface(
|
| 132 |
fn=chat_interface,
|
| 133 |
title="Neural Nexus AI",
|
| 134 |
description="Your gateway to intelligent conversation",
|
| 135 |
+
css=css, # Using the CSS you provided
|
| 136 |
submit_btn=gr.Button("Send", elem_id="submit_btn"),
|
| 137 |
textbox=gr.Textbox(
|
| 138 |
placeholder="Send a message...",
|