ffbeqwbe commited on
Commit
33896d9
·
2 Parent(s): 4e6a7033a994c9

Merge branch 'main' of https://huggingface.co/spaces/ffbeqwbe/DarwinChatbotFinal

Browse files
Files changed (1) hide show
  1. app.py +61 -51
app.py CHANGED
@@ -1,83 +1,93 @@
 
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")
61
-
62
  # Log output from the 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}")
72
 
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_ollama_serve():
13
+ """Start Ollama’s local inference server."""
14
+ try:
15
+ print("[SPACES] Starting Ollama serve...")
16
+ # Launch ollama serve (will keep running)
17
+ proc = subprocess.Popen(
18
+ ["ollama", "serve"],
19
+ stdout=subprocess.PIPE,
20
+ stderr=subprocess.STDOUT,
21
+ text=True,
22
+ env=os.environ
23
+ )
24
+ # Optionally log Ollama output
25
+ while True:
26
+ line = proc.stdout.readline()
27
+ if not line and proc.poll() is not None:
28
+ break
29
+ if line:
30
+ print(f"[OLLAMA] {line.strip()}")
31
+ print("[SPACES] Ollama serve exited with code", proc.returncode)
32
+ except Exception as e:
33
+ print(f"[SPACES] Error launching Ollama serve: {e}")
34
 
35
  def run_nicegui_app():
36
+ """Run the NiceGUI app directly on port 7860."""
37
+ # Add the Darwin app path to Python path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  sys.path.insert(0, DARWIN_APP_PATH)
39
+ # Change to the app directory
40
+ os.chdir(DARWIN_APP_PATH)
 
 
 
41
  # Set environment variables for the NiceGUI app
42
  env = {
43
+ **os.environ,
44
+ "PORT": "7860", # Use the Hugging Face Spaces port directly
45
+ "HOST": "0.0.0.0", # Listen on all interfaces
 
 
46
  }
 
 
47
  try:
48
+ print(f"[SPACES] Starting NiceGUI app from {DARWIN_SCRIPT_PATH}")
49
+ # Verify the script exists
50
+ if not os.path.exists(DARWIN_SCRIPT_PATH):
51
+ print(f"[SPACES] ERROR: Script not found at {DARWIN_SCRIPT_PATH}")
52
+ print("[SPACES] Available files in scripts directory:")
53
+ scripts_dir = os.path.join(DARWIN_APP_PATH, "scripts")
54
+ if os.path.exists(scripts_dir):
55
+ print(os.listdir(scripts_dir))
56
+ else:
57
+ print(f"[SPACES] Scripts directory not found at {scripts_dir}")
58
+ return
59
+
60
  process = subprocess.Popen(
61
+ [sys.executable, DARWIN_SCRIPT_PATH],
62
  stdout=subprocess.PIPE,
63
  stderr=subprocess.STDOUT,
64
  text=True,
65
+ env=env
 
66
  )
 
67
  print("[SPACES] Started NiceGUI app process")
 
68
  # Log output from the NiceGUI app
69
  while True:
70
  output = process.stdout.readline()
71
  if output:
72
+ print(f"[NiceGUI] {output.strip()}")
73
  if process.poll() is not None:
74
+ print("[SPACES] NiceGUI process exited with code:", process.returncode)
75
  break
76
  except Exception as e:
77
  print(f"[SPACES] Error starting NiceGUI app: {e}")
78
 
79
  if __name__ == "__main__":
80
+ print("[SPACES] Initializing Darwin Chatbot application")
81
+
82
+ # 1) Start Ollama serve in a daemon thread
83
+ ollama_thread = threading.Thread(target=run_ollama_serve, daemon=True)
84
+ ollama_thread.start()
85
+
86
+ # 2) Then start the NiceGUI frontend (blocks until exit)
87
  run_nicegui_app()
88
+
89
+ # If the NiceGUI app ever exits, keep container alive
90
+ print("[SPACES] NiceGUI app exited, keeping container alive")
91
  while True:
92
  time.sleep(60)
93
+ print("[SPACES] Container still running, waiting for restart...")