Update app.py
Browse files
app.py
CHANGED
|
@@ -17,15 +17,15 @@ try:
|
|
| 17 |
except:
|
| 18 |
pass
|
| 19 |
|
| 20 |
-
# Available
|
| 21 |
AVAILABLE_MODELS = [
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
]
|
| 27 |
|
| 28 |
-
# Initialize HF client
|
| 29 |
def create_client():
|
| 30 |
"""Create HF Inference Client"""
|
| 31 |
# Get token from environment variable
|
|
@@ -41,17 +41,21 @@ def create_client():
|
|
| 41 |
"4. Name: HF_TOKEN\n"
|
| 42 |
"5. Value: Your token (get from https://huggingface.co/settings/tokens)\n"
|
| 43 |
"6. Save and restart Space\n\n"
|
| 44 |
-
"
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# Try models in order
|
| 48 |
for model in AVAILABLE_MODELS:
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
continue
|
| 53 |
|
| 54 |
-
# Fallback to first model
|
|
|
|
| 55 |
return InferenceClient(model=AVAILABLE_MODELS[0], token=hf_token)
|
| 56 |
|
| 57 |
def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
@@ -78,7 +82,7 @@ def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
| 78 |
{"role": "user", "content": user_prompt}
|
| 79 |
]
|
| 80 |
|
| 81 |
-
#
|
| 82 |
response = ""
|
| 83 |
try:
|
| 84 |
for message in client.chat_completion(
|
|
@@ -89,18 +93,27 @@ def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
| 89 |
):
|
| 90 |
if message.choices[0].delta.content:
|
| 91 |
response += message.choices[0].delta.content
|
|
|
|
| 92 |
except Exception as api_error:
|
| 93 |
error_msg = str(api_error)
|
| 94 |
-
if "401" in error_msg or "Unauthorized" in error_msg:
|
| 95 |
return "", "", (
|
| 96 |
"❌ Authentication Failed!\n\n"
|
| 97 |
-
"Please
|
| 98 |
-
"1.
|
| 99 |
-
"2.
|
| 100 |
-
"3. Add
|
| 101 |
" - Name: HF_TOKEN\n"
|
| 102 |
-
" - Value: your token\n"
|
| 103 |
"4. Restart Space\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
f"Error details: {error_msg}"
|
| 105 |
)
|
| 106 |
raise
|
|
@@ -147,44 +160,36 @@ def save_xml_to_file(xml_content):
|
|
| 147 |
f.write(xml_content)
|
| 148 |
return output_path
|
| 149 |
|
| 150 |
-
# Custom CSS matching index.html
|
| 151 |
custom_css = """
|
| 152 |
-
|
| 153 |
-
margin: 0;
|
| 154 |
-
padding: 0;
|
| 155 |
-
box-sizing: border-box;
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
.gradio-container {
|
| 159 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
| 160 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 161 |
-
min-height: 100vh;
|
| 162 |
}
|
| 163 |
|
| 164 |
-
|
|
|
|
| 165 |
max-width: 1200px !important;
|
| 166 |
-
margin:
|
| 167 |
-
background: white !important;
|
| 168 |
-
border-radius: 20px !important;
|
| 169 |
-
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3) !important;
|
| 170 |
-
overflow: hidden !important;
|
| 171 |
}
|
| 172 |
|
| 173 |
-
.
|
|
|
|
| 174 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 175 |
color: white;
|
| 176 |
padding: 30px;
|
| 177 |
text-align: center;
|
| 178 |
-
border-radius:
|
| 179 |
margin-bottom: 20px;
|
| 180 |
}
|
| 181 |
|
| 182 |
-
.header h1 {
|
| 183 |
font-size: 2.5em;
|
| 184 |
margin-bottom: 10px;
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
-
.header p {
|
| 188 |
font-size: 1.1em;
|
| 189 |
opacity: 0.9;
|
| 190 |
}
|
|
@@ -198,6 +203,7 @@ custom_css = """
|
|
| 198 |
margin-top: 10px;
|
| 199 |
}
|
| 200 |
|
|
|
|
| 201 |
.info-box {
|
| 202 |
background: #e7f3ff;
|
| 203 |
border-left: 4px solid #2196F3;
|
|
@@ -209,27 +215,27 @@ custom_css = """
|
|
| 209 |
.info-box h4 {
|
| 210 |
margin-bottom: 10px;
|
| 211 |
color: #1976D2;
|
|
|
|
| 212 |
}
|
| 213 |
|
| 214 |
.info-box ul {
|
| 215 |
margin-left: 20px;
|
| 216 |
line-height: 1.8;
|
|
|
|
| 217 |
}
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
padding: 12px 15px !important;
|
| 222 |
border: 2px solid #e0e0e0 !important;
|
| 223 |
border-radius: 8px !important;
|
| 224 |
font-size: 16px !important;
|
| 225 |
-
transition: border-color 0.3s !important;
|
| 226 |
}
|
| 227 |
|
| 228 |
-
|
| 229 |
-
outline: none !important;
|
| 230 |
border-color: #667eea !important;
|
| 231 |
}
|
| 232 |
|
|
|
|
| 233 |
.gr-button {
|
| 234 |
padding: 15px 30px !important;
|
| 235 |
font-size: 16px !important;
|
|
@@ -240,6 +246,7 @@ textarea:focus, input:focus {
|
|
| 240 |
transition: all 0.3s !important;
|
| 241 |
text-transform: uppercase !important;
|
| 242 |
letter-spacing: 0.5px !important;
|
|
|
|
| 243 |
}
|
| 244 |
|
| 245 |
.gr-button-primary {
|
|
@@ -262,15 +269,35 @@ textarea:focus, input:focus {
|
|
| 262 |
transform: translateY(-2px) !important;
|
| 263 |
}
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
border: 2px solid #e0e0e0;
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
}
|
| 272 |
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
text-align: center;
|
| 275 |
margin-top: 30px;
|
| 276 |
padding: 20px;
|
|
@@ -278,27 +305,71 @@ textarea:focus, input:focus {
|
|
| 278 |
border-radius: 10px;
|
| 279 |
}
|
| 280 |
|
| 281 |
-
.footer p {
|
| 282 |
color: #666;
|
| 283 |
margin: 5px 0;
|
|
|
|
| 284 |
}
|
| 285 |
|
| 286 |
-
.
|
| 287 |
-
|
|
|
|
| 288 |
}
|
| 289 |
|
| 290 |
-
.
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
font-weight: 500 !important;
|
| 294 |
-
color: #
|
| 295 |
-
|
| 296 |
-
transition: all 0.3s !important;
|
| 297 |
}
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
}
|
| 303 |
"""
|
| 304 |
|
|
@@ -307,7 +378,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="🎮 Besiege Machi
|
|
| 307 |
|
| 308 |
# Header
|
| 309 |
gr.HTML("""
|
| 310 |
-
<div class="header">
|
| 311 |
<h1>🎮 Besiege Machine Generator</h1>
|
| 312 |
<p>Generate your Besiege machine designs with AI</p>
|
| 313 |
<span class="badge">✨ Powered by DeepSeek AI</span>
|
|
@@ -445,15 +516,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="🎮 Besiege Machi
|
|
| 445 |
|
| 446 |
# Footer
|
| 447 |
gr.HTML(f"""
|
| 448 |
-
<div class="footer">
|
| 449 |
<p>
|
| 450 |
-
🤖 Using <a href="https://huggingface.co/{AVAILABLE_MODELS[0]}" target="_blank">{AVAILABLE_MODELS[0]}</a>
|
| 451 |
</p>
|
| 452 |
<p>
|
| 453 |
⚠️ Note: Generated machines may require adjustments in-game
|
| 454 |
</p>
|
| 455 |
-
<p style="color: #999;
|
| 456 |
-
💡
|
| 457 |
</p>
|
| 458 |
</div>
|
| 459 |
""")
|
|
|
|
| 17 |
except:
|
| 18 |
pass
|
| 19 |
|
| 20 |
+
# Available models on HF Inference API (confirmed working)
|
| 21 |
AVAILABLE_MODELS = [
|
| 22 |
+
"Qwen/Qwen2.5-72B-Instruct", # Qwen 72B - Very good performance
|
| 23 |
+
"meta-llama/Llama-3.2-3B-Instruct", # Llama 3B - Fast and reliable
|
| 24 |
+
"mistralai/Mistral-7B-Instruct-v0.3", # Mistral 7B - Good balance
|
| 25 |
+
"microsoft/Phi-3-mini-4k-instruct", # Phi 3 Mini - Small and fast
|
| 26 |
]
|
| 27 |
|
| 28 |
+
# Initialize HF Inference client
|
| 29 |
def create_client():
|
| 30 |
"""Create HF Inference Client"""
|
| 31 |
# Get token from environment variable
|
|
|
|
| 41 |
"4. Name: HF_TOKEN\n"
|
| 42 |
"5. Value: Your token (get from https://huggingface.co/settings/tokens)\n"
|
| 43 |
"6. Save and restart Space\n\n"
|
| 44 |
+
"Token should have READ permission and start with 'hf_'"
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Try models in priority order
|
| 48 |
for model in AVAILABLE_MODELS:
|
| 49 |
try:
|
| 50 |
+
client = InferenceClient(model=model, token=hf_token)
|
| 51 |
+
print(f"✅ Successfully connected to model: {model}")
|
| 52 |
+
return client
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"⚠️ Failed to connect to {model}: {e}")
|
| 55 |
continue
|
| 56 |
|
| 57 |
+
# Fallback to first model if all failed
|
| 58 |
+
print(f"⚠️ All models failed, using fallback: {AVAILABLE_MODELS[0]}")
|
| 59 |
return InferenceClient(model=AVAILABLE_MODELS[0], token=hf_token)
|
| 60 |
|
| 61 |
def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
|
|
| 82 |
{"role": "user", "content": user_prompt}
|
| 83 |
]
|
| 84 |
|
| 85 |
+
# Call HF Inference API
|
| 86 |
response = ""
|
| 87 |
try:
|
| 88 |
for message in client.chat_completion(
|
|
|
|
| 93 |
):
|
| 94 |
if message.choices[0].delta.content:
|
| 95 |
response += message.choices[0].delta.content
|
| 96 |
+
|
| 97 |
except Exception as api_error:
|
| 98 |
error_msg = str(api_error)
|
| 99 |
+
if "401" in error_msg or "Unauthorized" in error_msg or "authentication" in error_msg.lower():
|
| 100 |
return "", "", (
|
| 101 |
"❌ Authentication Failed!\n\n"
|
| 102 |
+
"Please set your HF Token in Space Settings:\n\n"
|
| 103 |
+
"1. Get token from https://huggingface.co/settings/tokens\n"
|
| 104 |
+
"2. Go to Space Settings → Repository secrets\n"
|
| 105 |
+
"3. Add secret:\n"
|
| 106 |
" - Name: HF_TOKEN\n"
|
| 107 |
+
" - Value: your token (starts with 'hf_')\n"
|
| 108 |
"4. Restart Space\n\n"
|
| 109 |
+
"💡 Token is FREE and only needs READ permission!\n\n"
|
| 110 |
+
f"Error details: {error_msg}"
|
| 111 |
+
)
|
| 112 |
+
elif "404" in error_msg:
|
| 113 |
+
return "", "", (
|
| 114 |
+
"❌ Model Not Found!\n\n"
|
| 115 |
+
"The current model is not available on Inference API.\n"
|
| 116 |
+
"Trying backup models automatically...\n\n"
|
| 117 |
f"Error details: {error_msg}"
|
| 118 |
)
|
| 119 |
raise
|
|
|
|
| 160 |
f.write(xml_content)
|
| 161 |
return output_path
|
| 162 |
|
| 163 |
+
# Custom CSS matching index.html style
|
| 164 |
custom_css = """
|
| 165 |
+
/* Global Styles */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
.gradio-container {
|
| 167 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
|
|
|
|
|
|
| 168 |
}
|
| 169 |
|
| 170 |
+
/* Remove default padding/margin for better control */
|
| 171 |
+
.contain {
|
| 172 |
max-width: 1200px !important;
|
| 173 |
+
margin: 0 auto !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
|
| 176 |
+
/* Header styles matching index.html */
|
| 177 |
+
.header-box {
|
| 178 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 179 |
color: white;
|
| 180 |
padding: 30px;
|
| 181 |
text-align: center;
|
| 182 |
+
border-radius: 10px;
|
| 183 |
margin-bottom: 20px;
|
| 184 |
}
|
| 185 |
|
| 186 |
+
.header-box h1 {
|
| 187 |
font-size: 2.5em;
|
| 188 |
margin-bottom: 10px;
|
| 189 |
+
font-weight: 600;
|
| 190 |
}
|
| 191 |
|
| 192 |
+
.header-box p {
|
| 193 |
font-size: 1.1em;
|
| 194 |
opacity: 0.9;
|
| 195 |
}
|
|
|
|
| 203 |
margin-top: 10px;
|
| 204 |
}
|
| 205 |
|
| 206 |
+
/* Info box styles */
|
| 207 |
.info-box {
|
| 208 |
background: #e7f3ff;
|
| 209 |
border-left: 4px solid #2196F3;
|
|
|
|
| 215 |
.info-box h4 {
|
| 216 |
margin-bottom: 10px;
|
| 217 |
color: #1976D2;
|
| 218 |
+
font-weight: 600;
|
| 219 |
}
|
| 220 |
|
| 221 |
.info-box ul {
|
| 222 |
margin-left: 20px;
|
| 223 |
line-height: 1.8;
|
| 224 |
+
color: #333;
|
| 225 |
}
|
| 226 |
|
| 227 |
+
/* Input styles */
|
| 228 |
+
.gr-textbox, .gr-text-input {
|
|
|
|
| 229 |
border: 2px solid #e0e0e0 !important;
|
| 230 |
border-radius: 8px !important;
|
| 231 |
font-size: 16px !important;
|
|
|
|
| 232 |
}
|
| 233 |
|
| 234 |
+
.gr-textbox:focus, .gr-text-input:focus {
|
|
|
|
| 235 |
border-color: #667eea !important;
|
| 236 |
}
|
| 237 |
|
| 238 |
+
/* Button styles matching index.html */
|
| 239 |
.gr-button {
|
| 240 |
padding: 15px 30px !important;
|
| 241 |
font-size: 16px !important;
|
|
|
|
| 246 |
transition: all 0.3s !important;
|
| 247 |
text-transform: uppercase !important;
|
| 248 |
letter-spacing: 0.5px !important;
|
| 249 |
+
min-width: 150px !important;
|
| 250 |
}
|
| 251 |
|
| 252 |
.gr-button-primary {
|
|
|
|
| 269 |
transform: translateY(-2px) !important;
|
| 270 |
}
|
| 271 |
|
| 272 |
+
/* Tab styles */
|
| 273 |
+
.tabs {
|
| 274 |
+
border-bottom: 2px solid #e0e0e0 !important;
|
| 275 |
+
margin-bottom: 20px !important;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
button[role="tab"] {
|
| 279 |
+
padding: 12px 24px !important;
|
| 280 |
+
font-size: 16px !important;
|
| 281 |
+
font-weight: 500 !important;
|
| 282 |
+
color: #666 !important;
|
| 283 |
+
border: none !important;
|
| 284 |
+
background: transparent !important;
|
| 285 |
+
cursor: pointer !important;
|
| 286 |
+
transition: all 0.3s !important;
|
| 287 |
+
border-bottom: 3px solid transparent !important;
|
| 288 |
}
|
| 289 |
|
| 290 |
+
button[role="tab"][aria-selected="true"] {
|
| 291 |
+
color: #667eea !important;
|
| 292 |
+
border-bottom-color: #667eea !important;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
button[role="tab"]:hover {
|
| 296 |
+
color: #667eea !important;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
/* Footer styles */
|
| 300 |
+
.footer-box {
|
| 301 |
text-align: center;
|
| 302 |
margin-top: 30px;
|
| 303 |
padding: 20px;
|
|
|
|
| 305 |
border-radius: 10px;
|
| 306 |
}
|
| 307 |
|
| 308 |
+
.footer-box p {
|
| 309 |
color: #666;
|
| 310 |
margin: 5px 0;
|
| 311 |
+
font-size: 0.95em;
|
| 312 |
}
|
| 313 |
|
| 314 |
+
.footer-box a {
|
| 315 |
+
color: #667eea;
|
| 316 |
+
text-decoration: none;
|
| 317 |
}
|
| 318 |
|
| 319 |
+
.footer-box a:hover {
|
| 320 |
+
text-decoration: underline;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/* Accordion styles */
|
| 324 |
+
.gr-accordion {
|
| 325 |
+
border: 2px solid #e0e0e0 !important;
|
| 326 |
+
border-radius: 8px !important;
|
| 327 |
+
margin-top: 15px !important;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
/* Output text area styles */
|
| 331 |
+
.gr-text-input textarea {
|
| 332 |
+
font-family: 'Consolas', 'Monaco', monospace !important;
|
| 333 |
+
font-size: 14px !important;
|
| 334 |
+
line-height: 1.6 !important;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
/* Markdown output styles */
|
| 338 |
+
.gr-markdown {
|
| 339 |
+
padding: 15px !important;
|
| 340 |
+
background: #f8f9fa !important;
|
| 341 |
+
border-radius: 8px !important;
|
| 342 |
+
border: 1px solid #e0e0e0 !important;
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
/* Label styles */
|
| 346 |
+
label {
|
| 347 |
font-weight: 500 !important;
|
| 348 |
+
color: #555 !important;
|
| 349 |
+
margin-bottom: 8px !important;
|
|
|
|
| 350 |
}
|
| 351 |
|
| 352 |
+
/* Examples section */
|
| 353 |
+
.gr-examples {
|
| 354 |
+
margin-top: 15px !important;
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
/* Make sure content doesn't overflow */
|
| 358 |
+
.gr-row, .gr-column {
|
| 359 |
+
width: 100% !important;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
/* Adjust spacing */
|
| 363 |
+
.gr-box {
|
| 364 |
+
padding: 20px !important;
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
/* File component styles */
|
| 368 |
+
.gr-file {
|
| 369 |
+
margin-top: 15px !important;
|
| 370 |
+
padding: 15px !important;
|
| 371 |
+
border: 2px dashed #e0e0e0 !important;
|
| 372 |
+
border-radius: 8px !important;
|
| 373 |
}
|
| 374 |
"""
|
| 375 |
|
|
|
|
| 378 |
|
| 379 |
# Header
|
| 380 |
gr.HTML("""
|
| 381 |
+
<div class="header-box">
|
| 382 |
<h1>🎮 Besiege Machine Generator</h1>
|
| 383 |
<p>Generate your Besiege machine designs with AI</p>
|
| 384 |
<span class="badge">✨ Powered by DeepSeek AI</span>
|
|
|
|
| 516 |
|
| 517 |
# Footer
|
| 518 |
gr.HTML(f"""
|
| 519 |
+
<div class="footer-box">
|
| 520 |
<p>
|
| 521 |
+
🤖 Using <a href="https://huggingface.co/{AVAILABLE_MODELS[0]}" target="_blank">{AVAILABLE_MODELS[0]}</a> (HF Inference API)
|
| 522 |
</p>
|
| 523 |
<p>
|
| 524 |
⚠️ Note: Generated machines may require adjustments in-game
|
| 525 |
</p>
|
| 526 |
+
<p style="color: #999;">
|
| 527 |
+
💡 Free to use with HF Token | 🔑 Get token: <a href="https://huggingface.co/settings/tokens" target="_blank">huggingface.co/settings/tokens</a>
|
| 528 |
</p>
|
| 529 |
</div>
|
| 530 |
""")
|