euler314 commited on
Commit
e295487
Β·
verified Β·
1 Parent(s): 9bc509e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -83
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.path.dirname(os.path.abspath(__file__)), "user_code")
181
  job_dir = os.path.join(base_dir, job_id)
182
- output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "compiled_output", job_id)
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
- compile_options = {
234
- "max_compatibility": {
235
- "name": "Maximum Compatibility Binary",
236
- "cmd": build_cmd([
237
- sys.executable, "-m", "nuitka",
238
- "--standalone",
239
- "--onefile", # Single portable file
240
- "--show-progress",
241
- "--remove-output",
242
- "--follow-imports",
243
- "--assume-yes-for-downloads", # Auto-download missing dependencies
244
- "--python-flag=no_site", # Reduce dependencies
245
- script_path,
246
- f"--output-dir={output_dir}"
247
- ], use_static=True),
248
- "creates_runner": False
249
- },
250
- "portable": {
251
- "name": "Portable Non-Standalone",
252
- "cmd": build_cmd([
253
- sys.executable, "-m", "nuitka",
254
- "--show-progress",
255
- "--remove-output",
256
- "--assume-yes-for-downloads",
257
- "--python-flag=no_site",
258
- script_path,
259
- f"--output-dir={output_dir}"
260
- ], use_static=True),
261
- "creates_runner": False
262
- },
263
- "standalone": {
264
- "name": "Standalone Binary",
265
- "cmd": build_cmd([
266
- sys.executable, "-m", "nuitka",
267
- "--standalone",
268
- "--onefile",
269
- "--show-progress",
270
- "--remove-output",
271
- "--assume-yes-for-downloads",
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
- selected_option['cmd'],
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: {selected_option['name']}
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.Info("🎯 Static Libpython Available! Maximum portability enabled.")
433
  else:
434
- gr.Info("πŸ”§ Using alternative portable options. Static libpython not available.")
435
 
436
  if missing_deps:
437
- gr.Warning(f"⚠️ Missing dependencies: {', '.join(missing_deps)}")
438
  else:
439
- gr.Info("βœ… All required dependencies available!")
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
- ("max_compatibility", "Maximum Compatibility (Recommended)"),
488
- ("portable", "Portable Binary"),
489
- ("standalone", "Standalone Binary")
490
  ],
491
- value="max_compatibility",
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.Info(f"πŸ“ Compiling with Python {get_current_python_version()}")
502
  if check_static_libpython():
503
- gr.Success("πŸ”— Static libpython will be used!")
504
  else:
505
- gr.Info("πŸ”§ Using portable compilation flags")
506
 
507
  compile_btn = gr.Button("πŸš€ Compile with Nuitka", variant="primary")
508
 
509
  # Results section
510
- result_summary = gr.Textbox(label="Compilation Results", lines=10, visible=False)
511
- compile_logs = gr.Textbox(label="Compilation Logs", lines=10, visible=False)
512
- download_file = gr.File(label="Download Compiled Binary", visible=False)
513
-
514
- # Test run section
515
- with gr.Row():
516
- run_btn = gr.Button("πŸ§ͺ Test Run", visible=False)
517
- run_output = gr.Textbox(label="Execution Output", lines=8, visible=False)
 
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(value=summary, visible=True), # result_summary
536
- gr.update(value=logs, visible=True), # compile_logs
537
- gr.update(value=download_path, visible=True), # download_file
538
- gr.update(visible=True), # run_btn
539
  binary_path, # current_binary_path state
540
  True # compilation_success state
541
  )
542
  else:
543
  return (
544
- gr.update(value=summary, visible=True), # result_summary
545
- gr.update(value=logs, visible=True), # compile_logs
 
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, visible=True)
556
  else:
557
- return gr.update(value="No binary available to run.", visible=True)
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, run_btn, current_binary_path, compilation_success]
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(share=True)
 
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()