Update app.py
Browse files
app.py
CHANGED
@@ -97,7 +97,7 @@ def install_system_packages(packages_content, progress=gr.Progress()):
|
|
97 |
current_package += 1
|
98 |
progress(current_package / total_packages, desc=f"Installing: {line}")
|
99 |
|
100 |
-
# Try to install package
|
101 |
install_process = subprocess.run(
|
102 |
["apt-get", "update", "-qq"],
|
103 |
capture_output=True,
|
@@ -177,9 +177,9 @@ def compile_with_nuitka(code, requirements, packages, compilation_mode, output_e
|
|
177 |
|
178 |
# Create unique ID for this compilation
|
179 |
job_id = str(uuid.uuid4())
|
180 |
-
base_dir = os.path.join(os.
|
181 |
job_dir = os.path.join(base_dir, job_id)
|
182 |
-
output_dir = os.path.join(os.
|
183 |
|
184 |
# Create directories
|
185 |
ensure_dir(job_dir)
|
@@ -230,58 +230,49 @@ def compile_with_nuitka(code, requirements, packages, compilation_mode, output_e
|
|
230 |
base_cmd.append("--static-libpython=yes")
|
231 |
return base_cmd
|
232 |
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
"
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
"
|
252 |
-
"
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
"
|
262 |
-
|
263 |
-
|
264 |
-
"
|
265 |
-
"
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
"--python-flag=no_site",
|
273 |
-
script_path,
|
274 |
-
f"--output-dir={output_dir}"
|
275 |
-
], use_static=True),
|
276 |
-
"creates_runner": False
|
277 |
-
}
|
278 |
-
}
|
279 |
-
|
280 |
-
selected_option = compile_options[compilation_mode]
|
281 |
|
282 |
# Run compilation
|
283 |
process = subprocess.Popen(
|
284 |
-
|
285 |
stdout=subprocess.PIPE,
|
286 |
stderr=subprocess.STDOUT,
|
287 |
text=True,
|
@@ -358,7 +349,7 @@ def compile_with_nuitka(code, requirements, packages, compilation_mode, output_e
|
|
358 |
# Add system info to result
|
359 |
result_summary = f"""
|
360 |
Compilation Details:
|
361 |
-
- Mode: {
|
362 |
- Nuitka Version: {nuitka_version}
|
363 |
- Exit Code: {process.returncode}
|
364 |
- Output Path: {binary_path}
|
@@ -429,14 +420,14 @@ with gr.Blocks(title="Nuitka Python Compiler", theme=gr.themes.Soft()) as app:
|
|
429 |
missing_deps = check_dependencies()
|
430 |
|
431 |
if has_static:
|
432 |
-
gr.
|
433 |
else:
|
434 |
-
gr.
|
435 |
|
436 |
if missing_deps:
|
437 |
-
gr.
|
438 |
else:
|
439 |
-
gr.
|
440 |
|
441 |
with gr.Tabs():
|
442 |
with gr.TabItem("π§ Compiler"):
|
@@ -482,13 +473,14 @@ print('Compilation was optimized for your environment!')""",
|
|
482 |
label="packages.txt content"
|
483 |
)
|
484 |
|
|
|
485 |
compilation_mode = gr.Dropdown(
|
486 |
choices=[
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
],
|
491 |
-
value="
|
492 |
label="Compilation Mode"
|
493 |
)
|
494 |
|
@@ -498,23 +490,24 @@ print('Compilation was optimized for your environment!')""",
|
|
498 |
label="Output File Extension"
|
499 |
)
|
500 |
|
501 |
-
gr.
|
502 |
if check_static_libpython():
|
503 |
-
gr.
|
504 |
else:
|
505 |
-
gr.
|
506 |
|
507 |
compile_btn = gr.Button("π Compile with Nuitka", variant="primary")
|
508 |
|
509 |
# Results section
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
|
|
518 |
|
519 |
# Variables to store state
|
520 |
current_binary_path = gr.State(None)
|
@@ -532,19 +525,19 @@ print('Compilation was optimized for your environment!')""",
|
|
532 |
shutil.copy2(binary_path, download_path)
|
533 |
|
534 |
return (
|
535 |
-
gr.update(
|
536 |
-
gr.update(value=
|
537 |
-
gr.update(value=
|
538 |
-
gr.update(
|
539 |
binary_path, # current_binary_path state
|
540 |
True # compilation_success state
|
541 |
)
|
542 |
else:
|
543 |
return (
|
544 |
-
gr.update(
|
545 |
-
gr.update(value=
|
|
|
546 |
gr.update(visible=False), # download_file
|
547 |
-
gr.update(visible=False), # run_btn
|
548 |
None, # current_binary_path state
|
549 |
False # compilation_success state
|
550 |
)
|
@@ -552,14 +545,14 @@ print('Compilation was optimized for your environment!')""",
|
|
552 |
def handle_run(binary_path):
|
553 |
if binary_path:
|
554 |
output = run_compiled_binary(binary_path)
|
555 |
-
return gr.update(value=output
|
556 |
else:
|
557 |
-
return gr.update(value="No binary available to run."
|
558 |
|
559 |
compile_btn.click(
|
560 |
handle_compilation,
|
561 |
inputs=[code_input, requirements_input, packages_input, compilation_mode, output_extension],
|
562 |
-
outputs=[result_summary, compile_logs, download_file,
|
563 |
)
|
564 |
|
565 |
run_btn.click(
|
@@ -652,4 +645,4 @@ print('Compilation was optimized for your environment!')""",
|
|
652 |
gr.Markdown("π€ Created by Claude 3.7 Sonnet | π Powered by Nuitka with Smart Compilation")
|
653 |
|
654 |
if __name__ == "__main__":
|
655 |
-
app.launch(
|
|
|
97 |
current_package += 1
|
98 |
progress(current_package / total_packages, desc=f"Installing: {line}")
|
99 |
|
100 |
+
# Try to install package (Note: this might not work in HF Spaces)
|
101 |
install_process = subprocess.run(
|
102 |
["apt-get", "update", "-qq"],
|
103 |
capture_output=True,
|
|
|
177 |
|
178 |
# Create unique ID for this compilation
|
179 |
job_id = str(uuid.uuid4())
|
180 |
+
base_dir = os.path.join(os.getcwd(), "user_code")
|
181 |
job_dir = os.path.join(base_dir, job_id)
|
182 |
+
output_dir = os.path.join(os.getcwd(), "compiled_output", job_id)
|
183 |
|
184 |
# Create directories
|
185 |
ensure_dir(job_dir)
|
|
|
230 |
base_cmd.append("--static-libpython=yes")
|
231 |
return base_cmd
|
232 |
|
233 |
+
# Map compilation mode strings to actual configurations
|
234 |
+
if compilation_mode == "Maximum Compatibility (Recommended)":
|
235 |
+
cmd = build_cmd([
|
236 |
+
sys.executable, "-m", "nuitka",
|
237 |
+
"--standalone",
|
238 |
+
"--onefile", # Single portable file
|
239 |
+
"--show-progress",
|
240 |
+
"--remove-output",
|
241 |
+
"--follow-imports",
|
242 |
+
"--assume-yes-for-downloads", # Auto-download missing dependencies
|
243 |
+
"--python-flag=no_site", # Reduce dependencies
|
244 |
+
script_path,
|
245 |
+
f"--output-dir={output_dir}"
|
246 |
+
], use_static=True)
|
247 |
+
mode_name = "Maximum Compatibility Binary"
|
248 |
+
elif compilation_mode == "Portable Binary":
|
249 |
+
cmd = build_cmd([
|
250 |
+
sys.executable, "-m", "nuitka",
|
251 |
+
"--show-progress",
|
252 |
+
"--remove-output",
|
253 |
+
"--assume-yes-for-downloads",
|
254 |
+
"--python-flag=no_site",
|
255 |
+
script_path,
|
256 |
+
f"--output-dir={output_dir}"
|
257 |
+
], use_static=True)
|
258 |
+
mode_name = "Portable Non-Standalone"
|
259 |
+
else: # Standalone Binary
|
260 |
+
cmd = build_cmd([
|
261 |
+
sys.executable, "-m", "nuitka",
|
262 |
+
"--standalone",
|
263 |
+
"--onefile",
|
264 |
+
"--show-progress",
|
265 |
+
"--remove-output",
|
266 |
+
"--assume-yes-for-downloads",
|
267 |
+
"--python-flag=no_site",
|
268 |
+
script_path,
|
269 |
+
f"--output-dir={output_dir}"
|
270 |
+
], use_static=True)
|
271 |
+
mode_name = "Standalone Binary"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
# Run compilation
|
274 |
process = subprocess.Popen(
|
275 |
+
cmd,
|
276 |
stdout=subprocess.PIPE,
|
277 |
stderr=subprocess.STDOUT,
|
278 |
text=True,
|
|
|
349 |
# Add system info to result
|
350 |
result_summary = f"""
|
351 |
Compilation Details:
|
352 |
+
- Mode: {mode_name}
|
353 |
- Nuitka Version: {nuitka_version}
|
354 |
- Exit Code: {process.returncode}
|
355 |
- Output Path: {binary_path}
|
|
|
420 |
missing_deps = check_dependencies()
|
421 |
|
422 |
if has_static:
|
423 |
+
gr.Markdown("π― **Static Libpython Available!** Maximum portability enabled.")
|
424 |
else:
|
425 |
+
gr.Markdown("π§ **Using alternative portable options.** Static libpython not available.")
|
426 |
|
427 |
if missing_deps:
|
428 |
+
gr.Markdown(f"β οΈ **Missing dependencies:** {', '.join(missing_deps)}")
|
429 |
else:
|
430 |
+
gr.Markdown("β
**All required dependencies available!**")
|
431 |
|
432 |
with gr.Tabs():
|
433 |
with gr.TabItem("π§ Compiler"):
|
|
|
473 |
label="packages.txt content"
|
474 |
)
|
475 |
|
476 |
+
# Fixed dropdown choices
|
477 |
compilation_mode = gr.Dropdown(
|
478 |
choices=[
|
479 |
+
"Maximum Compatibility (Recommended)",
|
480 |
+
"Portable Binary",
|
481 |
+
"Standalone Binary"
|
482 |
],
|
483 |
+
value="Maximum Compatibility (Recommended)",
|
484 |
label="Compilation Mode"
|
485 |
)
|
486 |
|
|
|
490 |
label="Output File Extension"
|
491 |
)
|
492 |
|
493 |
+
gr.Markdown(f"π **Compiling with Python {get_current_python_version()}**")
|
494 |
if check_static_libpython():
|
495 |
+
gr.Markdown("π **Static libpython will be used!**")
|
496 |
else:
|
497 |
+
gr.Markdown("π§ **Using portable compilation flags**")
|
498 |
|
499 |
compile_btn = gr.Button("π Compile with Nuitka", variant="primary")
|
500 |
|
501 |
# Results section
|
502 |
+
with gr.Column(visible=False) as results_section:
|
503 |
+
result_summary = gr.Textbox(label="Compilation Results", lines=10)
|
504 |
+
compile_logs = gr.Textbox(label="Compilation Logs", lines=10)
|
505 |
+
download_file = gr.File(label="Download Compiled Binary")
|
506 |
+
|
507 |
+
# Test run section
|
508 |
+
with gr.Row():
|
509 |
+
run_btn = gr.Button("π§ͺ Test Run")
|
510 |
+
run_output = gr.Textbox(label="Execution Output", lines=8)
|
511 |
|
512 |
# Variables to store state
|
513 |
current_binary_path = gr.State(None)
|
|
|
525 |
shutil.copy2(binary_path, download_path)
|
526 |
|
527 |
return (
|
528 |
+
gr.update(visible=True), # results_section
|
529 |
+
gr.update(value=summary), # result_summary
|
530 |
+
gr.update(value=logs), # compile_logs
|
531 |
+
gr.update(value=download_path), # download_file
|
532 |
binary_path, # current_binary_path state
|
533 |
True # compilation_success state
|
534 |
)
|
535 |
else:
|
536 |
return (
|
537 |
+
gr.update(visible=True), # results_section
|
538 |
+
gr.update(value=summary), # result_summary
|
539 |
+
gr.update(value=logs), # compile_logs
|
540 |
gr.update(visible=False), # download_file
|
|
|
541 |
None, # current_binary_path state
|
542 |
False # compilation_success state
|
543 |
)
|
|
|
545 |
def handle_run(binary_path):
|
546 |
if binary_path:
|
547 |
output = run_compiled_binary(binary_path)
|
548 |
+
return gr.update(value=output)
|
549 |
else:
|
550 |
+
return gr.update(value="No binary available to run.")
|
551 |
|
552 |
compile_btn.click(
|
553 |
handle_compilation,
|
554 |
inputs=[code_input, requirements_input, packages_input, compilation_mode, output_extension],
|
555 |
+
outputs=[results_section, result_summary, compile_logs, download_file, current_binary_path, compilation_success]
|
556 |
)
|
557 |
|
558 |
run_btn.click(
|
|
|
645 |
gr.Markdown("π€ Created by Claude 3.7 Sonnet | π Powered by Nuitka with Smart Compilation")
|
646 |
|
647 |
if __name__ == "__main__":
|
648 |
+
app.launch()
|