Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import requests
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
|
|
@@ -66,5 +66,118 @@ interface = gr.ChatInterface(
|
|
| 66 |
css=css
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
interface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
'''import requests
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
| 66 |
css=css
|
| 67 |
)
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)'''
|
| 71 |
+
|
| 72 |
+
import requests
|
| 73 |
+
import os
|
| 74 |
+
import gradio as gr
|
| 75 |
+
|
| 76 |
+
# Fetch the API token from environment variable
|
| 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 |
+
# DeepSeek-R1 model endpoint
|
| 82 |
+
MODEL_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
| 83 |
+
|
| 84 |
+
# Headers for authentication
|
| 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 |
+
payload = {
|
| 93 |
+
"inputs": prompt,
|
| 94 |
+
"parameters": {
|
| 95 |
+
"max_new_tokens": 100, # Limit response length
|
| 96 |
+
"temperature": 0.6, # Control creativity
|
| 97 |
+
"top_p": 0.9 # Nucleus sampling
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
|
| 103 |
+
response.raise_for_status()
|
| 104 |
+
result = response.json()
|
| 105 |
+
# Extract only the generated text, avoiding duplication
|
| 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 |
+
# Construct the prompt with history if needed, but avoid repeating the response
|
| 115 |
+
if history:
|
| 116 |
+
prompt = "\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in history]) + f"\nUser: {user_input}\nAssistant: "
|
| 117 |
+
else:
|
| 118 |
+
prompt = f"User: {user_input}\nAssistant: "
|
| 119 |
+
|
| 120 |
+
response = query_deepseek(prompt)
|
| 121 |
+
# Ensure only the new response is returned, not the entire prompt
|
| 122 |
+
if response.startswith(prompt):
|
| 123 |
+
response = response[len(prompt):].strip()
|
| 124 |
+
return response
|
| 125 |
+
|
| 126 |
+
# Custom CSS for an attractive and unique interface
|
| 127 |
+
css = """
|
| 128 |
+
body {
|
| 129 |
+
background: linear-gradient(135deg, #1e3c72, #2a5298); /* Gradient background */
|
| 130 |
+
font-family: 'Arial', sans-serif;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
.gr-chat-interface {
|
| 134 |
+
border-radius: 15px;
|
| 135 |
+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
|
| 136 |
+
background: rgba(255, 255, 255, 0.9);
|
| 137 |
+
padding: 20px;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
button {
|
| 141 |
+
background-color: #007bff !important; /* Vibrant blue buttons */
|
| 142 |
+
color: white !important;
|
| 143 |
+
border: none !important;
|
| 144 |
+
padding: 12px 24px !important;
|
| 145 |
+
border-radius: 25px !important; /* Rounded buttons */
|
| 146 |
+
font-weight: bold;
|
| 147 |
+
transition: all 0.3s ease;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
button:hover {
|
| 151 |
+
background-color: #0056b3 !important; /* Darker blue on hover */
|
| 152 |
+
transform: scale(1.05); /* Slight zoom effect */
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
input, textarea {
|
| 156 |
+
border-radius: 10px !important;
|
| 157 |
+
border: 2px solid #007bff !important;
|
| 158 |
+
padding: 10px !important;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
.title {
|
| 162 |
+
color: #ffffff;
|
| 163 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
|
| 164 |
+
font-size: 2em;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
.description {
|
| 168 |
+
color: #e0e0e0;
|
| 169 |
+
font-style: italic;
|
| 170 |
+
}
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
# Create Gradio interface
|
| 174 |
+
interface = gr.ChatInterface(
|
| 175 |
+
fn=chat_interface,
|
| 176 |
+
title="LocoBot: DeepSeek-R1 Chatbot",
|
| 177 |
+
description="Chat with DeepSeek-R1 powered by Hugging Face Inference API.",
|
| 178 |
+
theme="default",
|
| 179 |
+
css=css
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
if __name__ == "__main__":
|
| 183 |
interface.launch(server_name="0.0.0.0", server_port=7860)
|