Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -375,101 +375,127 @@ modern_css = """
|
|
| 375 |
# Create the interface with password protection
|
| 376 |
def create_interface():
|
| 377 |
with gr.Blocks(css=modern_css, title="Python to C++ Code Optimizer", theme=gr.themes.Soft()) as app:
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
with gr.Column(scale=1):
|
| 401 |
-
gr.Markdown("### π Python Code Input")
|
| 402 |
-
python_input = gr.Textbox(
|
| 403 |
-
label="Python Code:",
|
| 404 |
-
value=default_python,
|
| 405 |
-
lines=15,
|
| 406 |
-
elem_classes=["python-input"],
|
| 407 |
-
placeholder="Enter your Python code here..."
|
| 408 |
-
)
|
| 409 |
-
|
| 410 |
-
with gr.Row():
|
| 411 |
-
model_selector = gr.Dropdown(
|
| 412 |
-
["GPT-4o", "Claude-3.5-Sonnet"],
|
| 413 |
-
label="Select AI Model",
|
| 414 |
-
value="GPT-4o",
|
| 415 |
-
elem_classes=["model-selector"]
|
| 416 |
-
)
|
| 417 |
-
|
| 418 |
-
convert_button = gr.Button("β¨ Convert to C++", elem_classes=["modern-button"])
|
| 419 |
-
|
| 420 |
-
with gr.Column(scale=1):
|
| 421 |
-
gr.Markdown("### β‘ Optimized C++ Code")
|
| 422 |
-
cpp_output = gr.Textbox(
|
| 423 |
-
label="Generated C++ Code:",
|
| 424 |
-
lines=15,
|
| 425 |
-
elem_classes=["cpp-output"],
|
| 426 |
-
interactive=False
|
| 427 |
-
)
|
| 428 |
-
|
| 429 |
-
# Execution Section
|
| 430 |
-
gr.Markdown("---")
|
| 431 |
-
gr.Markdown("## π Code Execution & Performance Comparison")
|
| 432 |
-
|
| 433 |
-
with gr.Row():
|
| 434 |
-
with gr.Column():
|
| 435 |
-
gr.Markdown("### π Python Output")
|
| 436 |
-
run_python_button = gr.Button("βΆοΈ Run Python", elem_classes=["run-button"])
|
| 437 |
-
python_output = gr.Textbox(
|
| 438 |
-
label="Python Execution Output:",
|
| 439 |
-
lines=5,
|
| 440 |
-
elem_classes=["python-output"],
|
| 441 |
-
interactive=False
|
| 442 |
-
)
|
| 443 |
-
|
| 444 |
-
with gr.Column():
|
| 445 |
-
gr.Markdown("### β‘ C++ Output")
|
| 446 |
-
run_cpp_button = gr.Button("βΆοΈ Run C++", elem_classes=["run-button"])
|
| 447 |
-
cpp_execution_output = gr.Textbox(
|
| 448 |
-
label="C++ Execution Output:",
|
| 449 |
-
lines=5,
|
| 450 |
-
elem_classes=["cpp-output-result"],
|
| 451 |
-
interactive=False
|
| 452 |
-
)
|
| 453 |
-
|
| 454 |
-
# Event handlers
|
| 455 |
-
convert_button.click(
|
| 456 |
-
fn=optimize,
|
| 457 |
-
inputs=[python_input, model_selector],
|
| 458 |
-
outputs=cpp_output
|
| 459 |
-
)
|
| 460 |
-
|
| 461 |
-
run_python_button.click(
|
| 462 |
-
fn=execute_python,
|
| 463 |
-
inputs=python_input,
|
| 464 |
-
outputs=python_output
|
| 465 |
-
)
|
| 466 |
-
|
| 467 |
-
run_cpp_button.click(
|
| 468 |
-
fn=execute_cpp,
|
| 469 |
-
inputs=cpp_output,
|
| 470 |
-
outputs=cpp_execution_output
|
| 471 |
)
|
| 472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
return app
|
| 474 |
|
| 475 |
# Launch the app
|
|
@@ -489,7 +515,6 @@ if __name__ == "__main__":
|
|
| 489 |
print("π Launching Python to C++ Code Optimizer on Hugging Face Spaces")
|
| 490 |
print("π Password protection enabled")
|
| 491 |
app.launch(
|
| 492 |
-
auth=[("user", APP_PASSWORD)],
|
| 493 |
show_error=True
|
| 494 |
)
|
| 495 |
else:
|
|
@@ -513,7 +538,6 @@ if __name__ == "__main__":
|
|
| 513 |
app.launch(
|
| 514 |
server_name="127.0.0.1",
|
| 515 |
server_port=port,
|
| 516 |
-
auth=[("user", APP_PASSWORD)],
|
| 517 |
show_error=True
|
| 518 |
)
|
| 519 |
|
|
|
|
| 375 |
# Create the interface with password protection
|
| 376 |
def create_interface():
|
| 377 |
with gr.Blocks(css=modern_css, title="Python to C++ Code Optimizer", theme=gr.themes.Soft()) as app:
|
| 378 |
+
authorized = gr.State(False)
|
| 379 |
+
|
| 380 |
+
def check_password(pw):
|
| 381 |
+
ok = pw == APP_PASSWORD
|
| 382 |
+
return (
|
| 383 |
+
gr.update(visible=not ok), # login hidden when ok
|
| 384 |
+
gr.update(visible=ok), # main shown when ok
|
| 385 |
+
gr.update(value="" if ok else "Invalid password", visible=not ok)
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
# Login gate
|
| 389 |
+
with gr.Group(visible=True) as login_group:
|
| 390 |
+
gr.Markdown("### π Enter Password to Continue")
|
| 391 |
+
with gr.Row():
|
| 392 |
+
pw = gr.Textbox(label="Password", type="password", placeholder="Enter password")
|
| 393 |
+
login_btn = gr.Button("Login", elem_classes=["modern-button"])
|
| 394 |
+
login_error = gr.Markdown(visible=False)
|
| 395 |
+
|
| 396 |
+
login_btn.click(
|
| 397 |
+
fn=check_password,
|
| 398 |
+
inputs=[pw],
|
| 399 |
+
outputs=[login_group, main_group, login_error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
)
|
| 401 |
+
|
| 402 |
+
# Main UI wrapped for toggling visibility
|
| 403 |
+
with gr.Group(visible=False) as main_group:
|
| 404 |
+
main_group.elem_id = "main_group"
|
| 405 |
+
# Header Section
|
| 406 |
+
gr.HTML("""
|
| 407 |
+
<div class="modern-header">
|
| 408 |
+
<h1>π Python to C++ Code Optimizer</h1>
|
| 409 |
+
<p>AI-powered code conversion with real-time execution and performance analysis</p>
|
| 410 |
+
</div>
|
| 411 |
+
""")
|
| 412 |
+
|
| 413 |
+
# Security Warning
|
| 414 |
+
gr.HTML("""
|
| 415 |
+
<div class="security-warning">
|
| 416 |
+
<h3 style="color: #dc2626; margin: 0 0 8px 0;">β οΈ Security Warning</h3>
|
| 417 |
+
<p style="margin: 0; color: #991b1b; font-weight: 500;">
|
| 418 |
+
This interface executes arbitrary code. <strong>Only run code from trusted sources.</strong><br>
|
| 419 |
+
Malicious code can harm your system. Use at your own risk.
|
| 420 |
+
</p>
|
| 421 |
+
</div>
|
| 422 |
+
""")
|
| 423 |
+
|
| 424 |
+
# Main Content Area
|
| 425 |
+
with gr.Row():
|
| 426 |
+
with gr.Column(scale=1):
|
| 427 |
+
gr.Markdown("### π Python Code Input")
|
| 428 |
+
python_input = gr.Textbox(
|
| 429 |
+
label="Python Code:",
|
| 430 |
+
value=default_python,
|
| 431 |
+
lines=15,
|
| 432 |
+
elem_classes=["python-input"],
|
| 433 |
+
placeholder="Enter your Python code here..."
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
with gr.Row():
|
| 437 |
+
model_selector = gr.Dropdown(
|
| 438 |
+
["GPT-4o", "Claude-3.5-Sonnet"],
|
| 439 |
+
label="Select AI Model",
|
| 440 |
+
value="GPT-4o",
|
| 441 |
+
elem_classes=["model-selector"]
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
convert_button = gr.Button("β¨ Convert to C++", elem_classes=["modern-button"])
|
| 445 |
+
|
| 446 |
+
with gr.Column(scale=1):
|
| 447 |
+
gr.Markdown("### β‘ Optimized C++ Code")
|
| 448 |
+
cpp_output = gr.Textbox(
|
| 449 |
+
label="Generated C++ Code:",
|
| 450 |
+
lines=15,
|
| 451 |
+
elem_classes=["cpp-output"],
|
| 452 |
+
interactive=False
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
# Execution Section
|
| 456 |
+
gr.Markdown("---")
|
| 457 |
+
gr.Markdown("## π Code Execution & Performance Comparison")
|
| 458 |
+
|
| 459 |
+
with gr.Row():
|
| 460 |
+
with gr.Column():
|
| 461 |
+
gr.Markdown("### π Python Output")
|
| 462 |
+
run_python_button = gr.Button("βΆοΈ Run Python", elem_classes=["run-button"])
|
| 463 |
+
python_output = gr.Textbox(
|
| 464 |
+
label="Python Execution Output:",
|
| 465 |
+
lines=5,
|
| 466 |
+
elem_classes=["python-output"],
|
| 467 |
+
interactive=False
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
with gr.Column():
|
| 471 |
+
gr.Markdown("### β‘ C++ Output")
|
| 472 |
+
run_cpp_button = gr.Button("βΆοΈ Run C++", elem_classes=["run-button"])
|
| 473 |
+
cpp_execution_output = gr.Textbox(
|
| 474 |
+
label="C++ Execution Output:",
|
| 475 |
+
lines=5,
|
| 476 |
+
elem_classes=["cpp-output-result"],
|
| 477 |
+
interactive=False
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
# Event handlers
|
| 481 |
+
convert_button.click(
|
| 482 |
+
fn=optimize,
|
| 483 |
+
inputs=[python_input, model_selector],
|
| 484 |
+
outputs=cpp_output
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
run_python_button.click(
|
| 488 |
+
fn=execute_python,
|
| 489 |
+
inputs=python_input,
|
| 490 |
+
outputs=python_output
|
| 491 |
+
)
|
| 492 |
+
|
| 493 |
+
run_cpp_button.click(
|
| 494 |
+
fn=execute_cpp,
|
| 495 |
+
inputs=cpp_output,
|
| 496 |
+
outputs=cpp_execution_output
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
return app
|
| 500 |
|
| 501 |
# Launch the app
|
|
|
|
| 515 |
print("π Launching Python to C++ Code Optimizer on Hugging Face Spaces")
|
| 516 |
print("π Password protection enabled")
|
| 517 |
app.launch(
|
|
|
|
| 518 |
show_error=True
|
| 519 |
)
|
| 520 |
else:
|
|
|
|
| 538 |
app.launch(
|
| 539 |
server_name="127.0.0.1",
|
| 540 |
server_port=port,
|
|
|
|
| 541 |
show_error=True
|
| 542 |
)
|
| 543 |
|