Update app.py
Browse files
app.py
CHANGED
|
@@ -81,8 +81,9 @@ def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
| 81 |
)
|
| 82 |
|
| 83 |
for chunk in stream:
|
| 84 |
-
if chunk.choices
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
except Exception as api_error:
|
| 88 |
error_msg = str(api_error)
|
|
@@ -106,7 +107,19 @@ def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
| 106 |
"Trying backup models automatically...\n\n"
|
| 107 |
f"Error details: {error_msg}"
|
| 108 |
)
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Try to convert to XML
|
| 112 |
try:
|
|
@@ -118,8 +131,17 @@ def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
|
|
| 118 |
|
| 119 |
except ValueError as e:
|
| 120 |
return "", "", str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
|
| 124 |
def convert_json_to_xml(json_input):
|
| 125 |
"""
|
|
@@ -378,17 +400,26 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="๐ฎ BesiegeField
|
|
| 378 |
with gr.Tabs():
|
| 379 |
# Tab 1: AI Generation
|
| 380 |
with gr.Tab("AI Generation"):
|
| 381 |
-
gr.
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
with gr.Row():
|
| 394 |
with gr.Column():
|
|
@@ -424,9 +455,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="๐ฎ BesiegeField
|
|
| 424 |
info="Maximum length of generation"
|
| 425 |
)
|
| 426 |
|
| 427 |
-
|
| 428 |
-
generate_btn = gr.Button("๐ Generate Machine", variant="primary", size="lg")
|
| 429 |
-
help_btn = gr.Button("๐ Help Video", variant="secondary")
|
| 430 |
|
| 431 |
status_output = gr.Markdown(label="Status")
|
| 432 |
|
|
@@ -455,96 +484,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="๐ฎ BesiegeField
|
|
| 455 |
file_path = save_xml_to_file(xml_str) if xml_str else None
|
| 456 |
return ai_resp, xml_str, status, file_path
|
| 457 |
|
| 458 |
-
# Help Modal
|
| 459 |
-
help_modal = gr.HTML(visible=False, value="""
|
| 460 |
-
<div id="help-modal" style="
|
| 461 |
-
position: fixed;
|
| 462 |
-
z-index: 2000;
|
| 463 |
-
left: 0;
|
| 464 |
-
top: 0;
|
| 465 |
-
width: 100%;
|
| 466 |
-
height: 100%;
|
| 467 |
-
background-color: rgba(0, 0, 0, 0.8);
|
| 468 |
-
display: flex;
|
| 469 |
-
align-items: center;
|
| 470 |
-
justify-content: center;
|
| 471 |
-
">
|
| 472 |
-
<div style="
|
| 473 |
-
background: white;
|
| 474 |
-
border-radius: 15px;
|
| 475 |
-
width: 90%;
|
| 476 |
-
max-width: 900px;
|
| 477 |
-
padding: 30px;
|
| 478 |
-
">
|
| 479 |
-
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
|
| 480 |
-
<h2 style="color: #333; margin: 0;">๐ Help Tutorial</h2>
|
| 481 |
-
<button onclick="this.closest('#help-modal').style.display='none'" style="
|
| 482 |
-
background: none;
|
| 483 |
-
border: none;
|
| 484 |
-
font-size: 30px;
|
| 485 |
-
color: #666;
|
| 486 |
-
cursor: pointer;
|
| 487 |
-
width: 40px;
|
| 488 |
-
height: 40px;
|
| 489 |
-
border-radius: 50%;
|
| 490 |
-
">×</button>
|
| 491 |
-
</div>
|
| 492 |
-
<div style="position: relative; width: 100%; padding-bottom: 56.25%; background: #000; border-radius: 10px; overflow: hidden;">
|
| 493 |
-
<video controls style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;">
|
| 494 |
-
<source src="help.mp4" type="video/mp4">
|
| 495 |
-
Your browser does not support video playback.
|
| 496 |
-
</video>
|
| 497 |
-
</div>
|
| 498 |
-
</div>
|
| 499 |
-
</div>
|
| 500 |
-
""")
|
| 501 |
-
|
| 502 |
-
def show_help():
|
| 503 |
-
return gr.HTML(visible=True, value="""
|
| 504 |
-
<div id="help-modal" style="
|
| 505 |
-
position: fixed;
|
| 506 |
-
z-index: 2000;
|
| 507 |
-
left: 0;
|
| 508 |
-
top: 0;
|
| 509 |
-
width: 100%;
|
| 510 |
-
height: 100%;
|
| 511 |
-
background-color: rgba(0, 0, 0, 0.8);
|
| 512 |
-
display: flex;
|
| 513 |
-
align-items: center;
|
| 514 |
-
justify-content: center;
|
| 515 |
-
">
|
| 516 |
-
<div style="
|
| 517 |
-
background: white;
|
| 518 |
-
border-radius: 15px;
|
| 519 |
-
width: 90%;
|
| 520 |
-
max-width: 900px;
|
| 521 |
-
padding: 30px;
|
| 522 |
-
">
|
| 523 |
-
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
|
| 524 |
-
<h2 style="color: #333; margin: 0;">๐ Help Tutorial</h2>
|
| 525 |
-
<button onclick="this.closest('#help-modal').style.display='none'" style="
|
| 526 |
-
background: none;
|
| 527 |
-
border: none;
|
| 528 |
-
font-size: 30px;
|
| 529 |
-
color: #666;
|
| 530 |
-
cursor: pointer;
|
| 531 |
-
width: 40px;
|
| 532 |
-
height: 40px;
|
| 533 |
-
border-radius: 50%;
|
| 534 |
-
">×</button>
|
| 535 |
-
</div>
|
| 536 |
-
<div style="position: relative; width: 100%; padding-bottom: 56.25%; background: #000; border-radius: 10px; overflow: hidden;">
|
| 537 |
-
<video controls autoplay style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;">
|
| 538 |
-
<source src="help.mp4" type="video/mp4">
|
| 539 |
-
Your browser does not support video playback.
|
| 540 |
-
</video>
|
| 541 |
-
</div>
|
| 542 |
-
</div>
|
| 543 |
-
</div>
|
| 544 |
-
""")
|
| 545 |
-
|
| 546 |
-
help_btn.click(fn=show_help, outputs=help_modal)
|
| 547 |
-
|
| 548 |
generate_btn.click(
|
| 549 |
fn=generate_and_save,
|
| 550 |
inputs=[user_input, temperature, max_tokens],
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
for chunk in stream:
|
| 84 |
+
if chunk.choices and len(chunk.choices) > 0:
|
| 85 |
+
if chunk.choices[0].delta and chunk.choices[0].delta.content:
|
| 86 |
+
response += chunk.choices[0].delta.content
|
| 87 |
|
| 88 |
except Exception as api_error:
|
| 89 |
error_msg = str(api_error)
|
|
|
|
| 107 |
"Trying backup models automatically...\n\n"
|
| 108 |
f"Error details: {error_msg}"
|
| 109 |
)
|
| 110 |
+
elif "list index out of range" in error_msg:
|
| 111 |
+
return "", "", (
|
| 112 |
+
"โ API Response Error!\n\n"
|
| 113 |
+
"The model returned an unexpected response format.\n"
|
| 114 |
+
"This might be due to:\n"
|
| 115 |
+
"- Model is still loading\n"
|
| 116 |
+
"- Temporary API issue\n"
|
| 117 |
+
"- Rate limiting\n\n"
|
| 118 |
+
"Please try again in a moment.\n\n"
|
| 119 |
+
f"Error details: {error_msg}"
|
| 120 |
+
)
|
| 121 |
+
else:
|
| 122 |
+
return "", "", f"โ API Error: {error_msg}"
|
| 123 |
|
| 124 |
# Try to convert to XML
|
| 125 |
try:
|
|
|
|
| 131 |
|
| 132 |
except ValueError as e:
|
| 133 |
return "", "", str(e)
|
| 134 |
+
except IndexError as e:
|
| 135 |
+
return "", "", (
|
| 136 |
+
"โ Response parsing error!\n\n"
|
| 137 |
+
"The API response format was unexpected.\n"
|
| 138 |
+
"Please try again. If this persists, the model may be temporarily unavailable.\n\n"
|
| 139 |
+
f"Error: {str(e)}"
|
| 140 |
+
)
|
| 141 |
except Exception as e:
|
| 142 |
+
import traceback
|
| 143 |
+
error_details = traceback.format_exc()
|
| 144 |
+
return "", "", f"โ Generation failed: {str(e)}\n\nDetails:\n{error_details}"
|
| 145 |
|
| 146 |
def convert_json_to_xml(json_input):
|
| 147 |
"""
|
|
|
|
| 400 |
with gr.Tabs():
|
| 401 |
# Tab 1: AI Generation
|
| 402 |
with gr.Tab("AI Generation"):
|
| 403 |
+
with gr.Row():
|
| 404 |
+
with gr.Column(scale=5):
|
| 405 |
+
gr.HTML("""
|
| 406 |
+
<div class="info-box">
|
| 407 |
+
<h4>๐ก How to Use</h4>
|
| 408 |
+
<ul>
|
| 409 |
+
<li>Describe the machine you want to create</li>
|
| 410 |
+
<li>Click "Generate Machine"</li>
|
| 411 |
+
<li>Wait for AI to generate (30-120 seconds)</li>
|
| 412 |
+
<li>Download the generated .bsg file</li>
|
| 413 |
+
</ul>
|
| 414 |
+
</div>
|
| 415 |
+
""")
|
| 416 |
+
with gr.Column(scale=1):
|
| 417 |
+
help_video = gr.Video(
|
| 418 |
+
label="๐ Help Tutorial",
|
| 419 |
+
value="help.mp4" if os.path.exists("help.mp4") else None,
|
| 420 |
+
autoplay=False,
|
| 421 |
+
visible=os.path.exists("help.mp4")
|
| 422 |
+
)
|
| 423 |
|
| 424 |
with gr.Row():
|
| 425 |
with gr.Column():
|
|
|
|
| 455 |
info="Maximum length of generation"
|
| 456 |
)
|
| 457 |
|
| 458 |
+
generate_btn = gr.Button("๐ Generate Machine", variant="primary", size="lg")
|
|
|
|
|
|
|
| 459 |
|
| 460 |
status_output = gr.Markdown(label="Status")
|
| 461 |
|
|
|
|
| 484 |
file_path = save_xml_to_file(xml_str) if xml_str else None
|
| 485 |
return ai_resp, xml_str, status, file_path
|
| 486 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
generate_btn.click(
|
| 488 |
fn=generate_and_save,
|
| 489 |
inputs=[user_input, temperature, max_tokens],
|