Update app.py
Browse files
app.py
CHANGED
|
@@ -74,15 +74,12 @@ import requests
|
|
| 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.")
|
| 81 |
|
| 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"
|
|
@@ -93,17 +90,24 @@ def query_deepseek(prompt):
|
|
| 93 |
payload = {
|
| 94 |
"inputs": prompt,
|
| 95 |
"parameters": {
|
| 96 |
-
"max_new_tokens": 100,
|
| 97 |
-
"temperature": 0.6,
|
| 98 |
-
"top_p": 0.9
|
|
|
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
try:
|
| 103 |
response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
|
| 104 |
-
response.raise_for_status()
|
| 105 |
result = response.json()
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
except requests.exceptions.RequestException as e:
|
| 108 |
return f"Error: Could not connect to the API - {str(e)}"
|
| 109 |
except (KeyError, IndexError):
|
|
@@ -115,6 +119,7 @@ def chat_interface(user_input, history):
|
|
| 115 |
response = query_deepseek(prompt)
|
| 116 |
return response
|
| 117 |
|
|
|
|
| 118 |
css = """
|
| 119 |
@keyframes gradient {
|
| 120 |
0% { background-position: 0% 50%; }
|
|
@@ -293,7 +298,6 @@ input[type="text"]:focus {
|
|
| 293 |
}
|
| 294 |
"""
|
| 295 |
|
| 296 |
-
# Create Gradio interface (no unsupported arguments)
|
| 297 |
interface = gr.ChatInterface(
|
| 298 |
fn=chat_interface,
|
| 299 |
title="LocoBot: DeepSeek-R1 Chatbot",
|
|
@@ -303,7 +307,4 @@ interface = gr.ChatInterface(
|
|
| 303 |
)
|
| 304 |
|
| 305 |
if __name__ == "__main__":
|
| 306 |
-
interface.launch(server_name="0.0.0.0", server_port=7860)
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
| 74 |
import os
|
| 75 |
import gradio as gr
|
| 76 |
|
|
|
|
| 77 |
API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 78 |
if not API_TOKEN:
|
| 79 |
raise ValueError("HF_API_TOKEN not found. Please set it in the Space Secrets.")
|
| 80 |
|
|
|
|
| 81 |
MODEL_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
| 82 |
|
|
|
|
| 83 |
HEADERS = {
|
| 84 |
"Authorization": f"Bearer {API_TOKEN}",
|
| 85 |
"Content-Type": "application/json"
|
|
|
|
| 90 |
payload = {
|
| 91 |
"inputs": prompt,
|
| 92 |
"parameters": {
|
| 93 |
+
"max_new_tokens": 100,
|
| 94 |
+
"temperature": 0.6,
|
| 95 |
+
"top_p": 0.9,
|
| 96 |
+
"repetition_penalty": 1.2 # Added to reduce repetition
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
try:
|
| 101 |
response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
|
| 102 |
+
response.raise_for_status()
|
| 103 |
result = response.json()
|
| 104 |
+
|
| 105 |
+
# Extract only the new response part
|
| 106 |
+
full_response = result[0]["generated_text"].strip()
|
| 107 |
+
if "Assistant: " in full_response:
|
| 108 |
+
# Split and get only the last assistant response
|
| 109 |
+
return full_response.split("Assistant: ")[-1].strip()
|
| 110 |
+
return full_response
|
| 111 |
except requests.exceptions.RequestException as e:
|
| 112 |
return f"Error: Could not connect to the API - {str(e)}"
|
| 113 |
except (KeyError, IndexError):
|
|
|
|
| 119 |
response = query_deepseek(prompt)
|
| 120 |
return response
|
| 121 |
|
| 122 |
+
# Keep your original CSS unchanged
|
| 123 |
css = """
|
| 124 |
@keyframes gradient {
|
| 125 |
0% { background-position: 0% 50%; }
|
|
|
|
| 298 |
}
|
| 299 |
"""
|
| 300 |
|
|
|
|
| 301 |
interface = gr.ChatInterface(
|
| 302 |
fn=chat_interface,
|
| 303 |
title="LocoBot: DeepSeek-R1 Chatbot",
|
|
|
|
| 307 |
)
|
| 308 |
|
| 309 |
if __name__ == "__main__":
|
| 310 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|