3v324v23 commited on
Commit
bdcb812
·
1 Parent(s): 881779c

Исправление запуска Playground UI на HuggingFace Space с проблемами прав доступа

Browse files
Files changed (1) hide show
  1. fallback.py +74 -6
fallback.py CHANGED
@@ -569,12 +569,45 @@ def run_playground():
569
  env["NEXT_PUBLIC_EDIT_GRAPH_MODE"] = "true"
570
  env["NEXT_PUBLIC_DISABLE_CAMERA"] = "true"
571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
  playground_dir = Path("/app/playground")
573
  if not playground_dir.exists():
574
  logger.error(f"Playground directory not found at {playground_dir}")
575
  raise FileNotFoundError(f"Playground directory not found at {playground_dir}")
576
 
577
- cmd = ["pnpm", "dev"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
  logger.info(f"Running command in {playground_dir}: {' '.join(cmd)}")
579
 
580
  # Запуск процесса Playground
@@ -597,12 +630,47 @@ def run_playground():
597
  threading.Thread(target=log_output, args=(playground_process.stderr, "PLAYGROUND ERROR"), daemon=True).start()
598
 
599
  # Проверка, что процесс запустился
600
- time.sleep(3)
601
  if playground_process.poll() is not None:
602
- logger.error(f"Playground UI failed to start with exit code {playground_process.returncode}")
603
- raise RuntimeError(f"Playground UI failed to start with exit code {playground_process.returncode}")
604
-
605
- logger.info("Playground UI started successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
606
  return playground_process
607
 
608
  def main():
 
569
  env["NEXT_PUBLIC_EDIT_GRAPH_MODE"] = "true"
570
  env["NEXT_PUBLIC_DISABLE_CAMERA"] = "true"
571
 
572
+ # Настройка PNPM для использования временной директории для кэша
573
+ pnpm_home = "/tmp/pnpm-store"
574
+ os.makedirs(pnpm_home, exist_ok=True)
575
+ env["PNPM_HOME"] = pnpm_home
576
+ env["HOME"] = "/tmp" # Установка HOME в /tmp для всех инструментов
577
+ env["XDG_CONFIG_HOME"] = "/tmp/.config"
578
+ env["XDG_CACHE_HOME"] = "/tmp/.cache"
579
+ env["XDG_DATA_HOME"] = "/tmp/.local/share"
580
+
581
+ # Создаем необходимые директории
582
+ os.makedirs("/tmp/.config", exist_ok=True)
583
+ os.makedirs("/tmp/.cache", exist_ok=True)
584
+ os.makedirs("/tmp/.local/share", exist_ok=True)
585
+
586
+ logger.info(f"Set PNPM_HOME to {pnpm_home}")
587
+ logger.info(f"Set HOME to {env['HOME']}")
588
+
589
  playground_dir = Path("/app/playground")
590
  if not playground_dir.exists():
591
  logger.error(f"Playground directory not found at {playground_dir}")
592
  raise FileNotFoundError(f"Playground directory not found at {playground_dir}")
593
 
594
+ # Создаем .npmrc файл в директории playground для настройки npm
595
+ npmrc_path = playground_dir / ".npmrc"
596
+ try:
597
+ with open(npmrc_path, 'w') as f:
598
+ f.write(f"cache=/tmp/.npm-cache\n")
599
+ f.write(f"tmp=/tmp/.npm-tmp\n")
600
+ logger.info(f"Created .npmrc file at {npmrc_path}")
601
+ except Exception as e:
602
+ logger.warning(f"Could not create .npmrc: {e}")
603
+
604
+ # Создаем директории для npm
605
+ os.makedirs("/tmp/.npm-cache", exist_ok=True)
606
+ os.makedirs("/tmp/.npm-tmp", exist_ok=True)
607
+
608
+ # Пробуем сначала запустить с помощью npx next dev
609
+ logger.info("Trying to start Playground with npx next dev...")
610
+ cmd = ["npx", "next", "dev", "--port", "7860"]
611
  logger.info(f"Running command in {playground_dir}: {' '.join(cmd)}")
612
 
613
  # Запуск процесса Playground
 
630
  threading.Thread(target=log_output, args=(playground_process.stderr, "PLAYGROUND ERROR"), daemon=True).start()
631
 
632
  # Проверка, что процесс запустился
633
+ time.sleep(5)
634
  if playground_process.poll() is not None:
635
+ logger.warning(f"npx next dev failed with code {playground_process.returncode}, trying alternative method...")
636
+
637
+ # Если первый метод не работает, пробуем запустить напрямую node
638
+ logger.info("Trying to start Playground with node directly...")
639
+
640
+ # Ищем файл .next/server/app/page.js
641
+ next_page_js = playground_dir / ".next/server/app/page.js"
642
+ if next_page_js.exists():
643
+ cmd = ["node", str(next_page_js)]
644
+ logger.info(f"Running command: {' '.join(cmd)}")
645
+
646
+ playground_process = subprocess.Popen(
647
+ cmd,
648
+ cwd=str(playground_dir),
649
+ env=env,
650
+ stdout=subprocess.PIPE,
651
+ stderr=subprocess.PIPE,
652
+ text=True,
653
+ bufsize=1,
654
+ )
655
+
656
+ threading.Thread(target=log_output, args=(playground_process.stdout, "PLAYGROUND (NODE)"), daemon=True).start()
657
+ threading.Thread(target=log_output, args=(playground_process.stderr, "PLAYGROUND ERROR (NODE)"), daemon=True).start()
658
+
659
+ time.sleep(3)
660
+ if playground_process.poll() is not None:
661
+ logger.error(f"All Playground UI launch methods failed")
662
+ raise RuntimeError(f"All Playground UI launch methods failed")
663
+ else:
664
+ logger.info("Skipping Playground launch as it's not critical for API functionality")
665
+ logger.info("API server and proxy server will continue running")
666
+ # Создаем заглушку для процесса
667
+ playground_process = subprocess.Popen(
668
+ ["tail", "-f", "/dev/null"], # Команда, которая никогда не завершится
669
+ stdout=subprocess.PIPE,
670
+ stderr=subprocess.PIPE,
671
+ )
672
+
673
+ logger.info("Playground UI started successfully or skipped")
674
  return playground_process
675
 
676
  def main():