ffbeqwbe commited on
Commit
778e7d7
·
1 Parent(s): a933907
Files changed (1) hide show
  1. app.py +47 -38
app.py CHANGED
@@ -1,69 +1,78 @@
1
- # app.py for Hugging Face Spaces (place in /home/user/app)
2
- import gradio as gr
3
- import subprocess
4
  import os
 
5
  import sys
6
- import threading
7
  import time
8
 
9
  # Path to your main application
10
  DARWIN_APP_PATH = "/data/DarwinChatbot"
11
-
12
- # Port your NiceGUI app is using
13
- NICEGUI_PORT = 8080
14
 
15
  def run_nicegui_app():
16
- """Run the NiceGUI app in a separate process"""
17
  # Add the Darwin app path to Python path
18
  sys.path.insert(0, DARWIN_APP_PATH)
19
 
20
  # Change to the app directory
21
  os.chdir(DARWIN_APP_PATH)
22
 
23
- # Start the NiceGUI application
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  try:
 
 
25
  process = subprocess.Popen(
26
- [sys.executable, "app.py"],
27
  stdout=subprocess.PIPE,
28
  stderr=subprocess.STDOUT,
29
  text=True,
30
- env={**os.environ, "PORT": str(NICEGUI_PORT), "HOST": "0.0.0.0"}
31
  )
32
 
 
 
33
  # Log output from the NiceGUI app
34
  while True:
35
  output = process.stdout.readline()
36
  if output:
37
  print(f"[NiceGUI] {output.strip()}")
38
  if process.poll() is not None:
 
39
  break
40
  except Exception as e:
41
- print(f"Error starting NiceGUI app: {e}")
42
-
43
- # Start NiceGUI in a separate thread
44
- nicegui_thread = threading.Thread(target=run_nicegui_app, daemon=True)
45
- nicegui_thread.start()
46
 
47
- # Give NiceGUI time to start
48
- time.sleep(5)
49
-
50
- # Create a simple Gradio interface that embeds the NiceGUI app in an iframe
51
- def create_gradio_interface():
52
- with gr.Blocks() as interface:
53
- gr.Markdown("# Darwin Chatbot")
54
- gr.HTML(f'''
55
- <iframe
56
- src="http://localhost:{NICEGUI_PORT}"
57
- width="100%"
58
- height="800px"
59
- frameborder="0"
60
- allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
61
- allowfullscreen
62
- ></iframe>
63
- ''')
64
 
65
- return interface
66
-
67
- # Launch the Gradio interface
68
- demo = create_gradio_interface()
69
- demo.launch(server_port=7860, server_name="0.0.0.0")
 
 
 
 
 
 
 
 
1
  import os
2
+ import subprocess
3
  import sys
 
4
  import time
5
 
6
  # Path to your main application
7
  DARWIN_APP_PATH = "/data/DarwinChatbot"
8
+ DARWIN_SCRIPT_PATH = os.path.join(DARWIN_APP_PATH, "scripts", "app.py")
 
 
9
 
10
  def run_nicegui_app():
11
+ """Run the NiceGUI app directly on port 7860"""
12
  # Add the Darwin app path to Python path
13
  sys.path.insert(0, DARWIN_APP_PATH)
14
 
15
  # Change to the app directory
16
  os.chdir(DARWIN_APP_PATH)
17
 
18
+ # Set environment variables for the NiceGUI app
19
+ env = {
20
+ **os.environ,
21
+ "PORT": "7860", # Use the Hugging Face Spaces port directly
22
+ "HOST": "0.0.0.0", # Listen on all interfaces
23
+ "NICEGUI_DISABLE_RELOAD": "1", # Disable hot-reload
24
+ "NICEGUI_NO_AUTO_RELOAD": "1" # Also try this environment variable
25
+ }
26
+
27
+ # Try to patch NiceGUI behavior by creating a small script
28
+ patch_script = """
29
+ # Add this to the top of your app
30
+ from nicegui import app as nicegui_app
31
+ # Disable auto reload
32
+ nicegui_app.app.config['AUTO_RELOAD_WAIT'] = False
33
+ nicegui_app.app.config['AUTO_RELOAD'] = False
34
+ # Then continue with the regular import
35
+ from scripts.app import *
36
+ """
37
+
38
+ patch_path = os.path.join(DARWIN_APP_PATH, "run_patched.py")
39
+ with open(patch_path, "w") as f:
40
+ f.write(patch_script)
41
+
42
+ # Start the NiceGUI application with the patched script
43
  try:
44
+ print(f"[SPACES] Starting NiceGUI app with disabled hot-reload")
45
+
46
  process = subprocess.Popen(
47
+ [sys.executable, patch_path],
48
  stdout=subprocess.PIPE,
49
  stderr=subprocess.STDOUT,
50
  text=True,
51
+ env=env
52
  )
53
 
54
+ print("[SPACES] Started NiceGUI app process")
55
+
56
  # Log output from the NiceGUI app
57
  while True:
58
  output = process.stdout.readline()
59
  if output:
60
  print(f"[NiceGUI] {output.strip()}")
61
  if process.poll() is not None:
62
+ print("[SPACES] NiceGUI process exited with code:", process.returncode)
63
  break
64
  except Exception as e:
65
+ print(f"[SPACES] Error starting NiceGUI app: {e}")
 
 
 
 
66
 
67
+ if __name__ == "__main__":
68
+ print("[SPACES] Starting Darwin Chatbot application")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ # Start NiceGUI in the main thread
71
+ # This will keep the script running and output logs
72
+ run_nicegui_app()
73
+
74
+ # If the NiceGUI app exits, keep the process alive
75
+ print("[SPACES] NiceGUI app exited, keeping container alive")
76
+ while True:
77
+ time.sleep(60)
78
+ print("[SPACES] Container still running, waiting for restart...")