ffbeqwbe commited on
Commit
7052390
·
1 Parent(s): b0cfcf1
Files changed (1) hide show
  1. app.py +29 -37
app.py CHANGED
@@ -1,60 +1,50 @@
 
1
  import os
2
  import subprocess
3
  import sys
 
4
  import time
5
 
6
- # Path to your main application - CORRECT PATH
7
  DARWIN_APP_PATH = "/data/DarwinChatbot"
8
- SCRIPT_DIR = os.path.join(DARWIN_APP_PATH, "scripts")
9
- SCRIPT_PATH = os.path.join(SCRIPT_DIR, "app.py")
10
 
11
  def run_nicegui_app():
12
  """Run the NiceGUI app directly on port 7860"""
13
- # Check if the script exists first
14
- if not os.path.exists(SCRIPT_PATH):
15
- print(f"[SPACES] ERROR: Script not found at {SCRIPT_PATH}")
16
- print("[SPACES] Listing available files:")
17
-
18
- # List files in the main directory
19
- print(f"Files in {DARWIN_APP_PATH}:")
20
- if os.path.exists(DARWIN_APP_PATH):
21
- print(os.listdir(DARWIN_APP_PATH))
22
-
23
- # List files in the scripts directory if it exists
24
- print(f"Files in {SCRIPT_DIR}:")
25
- if os.path.exists(SCRIPT_DIR):
26
- print(os.listdir(SCRIPT_DIR))
27
-
28
- return
29
-
30
- # Add the paths to Python path
31
  sys.path.insert(0, DARWIN_APP_PATH)
32
- sys.path.insert(0, SCRIPT_DIR)
33
 
34
- # Change to the scripts directory to ensure imports work correctly
35
- os.chdir(SCRIPT_DIR)
36
 
37
  # Set environment variables for the NiceGUI app
38
  env = {
39
  **os.environ,
40
  "PORT": "7860", # Use the Hugging Face Spaces port directly
41
- "HOST": "0.0.0.0", # Listen on all interfaces
42
- "NICEGUI_DISABLE_RELOAD": "1", # Disable hot-reload
43
- "NICEGUI_NO_AUTO_RELOAD": "1" # Also try this environment variable
44
  }
45
 
46
  # Start the NiceGUI application
47
  try:
48
- print(f"[SPACES] Starting NiceGUI app from {SCRIPT_PATH}")
49
 
50
- # Directly run the script from the scripts directory
 
 
 
 
 
 
 
 
 
 
51
  process = subprocess.Popen(
52
- [sys.executable, SCRIPT_PATH],
53
  stdout=subprocess.PIPE,
54
  stderr=subprocess.STDOUT,
55
  text=True,
56
- env=env,
57
- cwd=SCRIPT_DIR # Set the working directory explicitly
58
  )
59
 
60
  print("[SPACES] Started NiceGUI app process")
@@ -63,9 +53,9 @@ def run_nicegui_app():
63
  while True:
64
  output = process.stdout.readline()
65
  if output:
66
- print(f"[APP] {output.strip()}")
67
  if process.poll() is not None:
68
- print("[SPACES] Process exited with code:", process.returncode)
69
  break
70
  except Exception as e:
71
  print(f"[SPACES] Error starting NiceGUI app: {e}")
@@ -73,11 +63,13 @@ def run_nicegui_app():
73
  if __name__ == "__main__":
74
  print("[SPACES] Starting Darwin Chatbot application")
75
 
76
- # Start the app in the main thread
 
77
  run_nicegui_app()
78
 
79
- # If the app exits, keep the process alive
80
- print("[SPACES] App exited, keeping container alive")
 
81
  while True:
82
  time.sleep(60)
83
  print("[SPACES] Container still running, waiting for restart...")
 
1
+ # app.py for Hugging Face Spaces (place in /home/user/app)
2
  import os
3
  import subprocess
4
  import sys
5
+ import threading
6
  import time
7
 
8
+ # Path to your main application
9
  DARWIN_APP_PATH = "/data/DarwinChatbot"
10
+ DARWIN_SCRIPT_PATH = os.path.join(DARWIN_APP_PATH, "scripts", "app.py")
 
11
 
12
  def run_nicegui_app():
13
  """Run the NiceGUI app directly on port 7860"""
14
+ # Add the Darwin app path to Python path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  sys.path.insert(0, DARWIN_APP_PATH)
 
16
 
17
+ # Change to the app directory
18
+ os.chdir(DARWIN_APP_PATH)
19
 
20
  # Set environment variables for the NiceGUI app
21
  env = {
22
  **os.environ,
23
  "PORT": "7860", # Use the Hugging Face Spaces port directly
24
+ "HOST": "0.0.0.0" # Listen on all interfaces
 
 
25
  }
26
 
27
  # Start the NiceGUI application
28
  try:
29
+ print(f"[SPACES] Starting NiceGUI app from {DARWIN_SCRIPT_PATH}")
30
 
31
+ # Verify the script exists
32
+ if not os.path.exists(DARWIN_SCRIPT_PATH):
33
+ print(f"[SPACES] ERROR: Script not found at {DARWIN_SCRIPT_PATH}")
34
+ print("[SPACES] Available files in scripts directory:")
35
+ scripts_dir = os.path.join(DARWIN_APP_PATH, "scripts")
36
+ if os.path.exists(scripts_dir):
37
+ print(os.listdir(scripts_dir))
38
+ else:
39
+ print(f"[SPACES] Scripts directory not found at {scripts_dir}")
40
+ return
41
+
42
  process = subprocess.Popen(
43
+ [sys.executable, DARWIN_SCRIPT_PATH],
44
  stdout=subprocess.PIPE,
45
  stderr=subprocess.STDOUT,
46
  text=True,
47
+ env=env
 
48
  )
49
 
50
  print("[SPACES] Started NiceGUI app process")
 
53
  while True:
54
  output = process.stdout.readline()
55
  if output:
56
+ print(f"[NiceGUI] {output.strip()}")
57
  if process.poll() is not None:
58
+ print("[SPACES] NiceGUI process exited with code:", process.returncode)
59
  break
60
  except Exception as e:
61
  print(f"[SPACES] Error starting NiceGUI app: {e}")
 
63
  if __name__ == "__main__":
64
  print("[SPACES] Starting Darwin Chatbot application")
65
 
66
+ # Start NiceGUI in the main thread
67
+ # This will keep the script running and output logs
68
  run_nicegui_app()
69
 
70
+ # If the NiceGUI app exits, keep the process alive
71
+ # (This should rarely happen unless there's an error)
72
+ print("[SPACES] NiceGUI app exited, keeping container alive")
73
  while True:
74
  time.sleep(60)
75
  print("[SPACES] Container still running, waiting for restart...")