J Z commited on
Commit
0e43856
·
verified ·
1 Parent(s): b32e50d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +182 -65
app.py CHANGED
@@ -1,84 +1,201 @@
1
  import os
2
  import sys
 
 
 
3
  import atexit
4
  import signal
5
- import subprocess
6
- import time
7
 
8
- # Add the DarwinChatbot scripts directory to the Python path
9
  DARWIN_SCRIPTS_PATH = "/data/DarwinChatbot/scripts"
10
  sys.path.append(DARWIN_SCRIPTS_PATH)
11
 
12
- # HF Spaces expects the app to run on port 7860
13
- NICEGUI_PORT = 7860
14
- DARWIN_APP_PROCESS = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- def start_darwin_app():
17
- """Start the Darwin app as a subprocess configured to use the HF Spaces port"""
18
- global DARWIN_APP_PROCESS
19
 
20
- # Environment variables to configure NiceGUI
21
- env = os.environ.copy()
22
- env["NICEGUI_PORT"] = str(NICEGUI_PORT)
23
 
24
- # Make sure these env vars are set for NiceGUI to work properly in HF Spaces
25
- env["PYTHONUNBUFFERED"] = "1" # Ensures Python output is unbuffered
26
- env["NICEGUI_HOST"] = "0.0.0.0" # Bind to all network interfaces
 
27
 
28
- # Change to the DarwinChatbot scripts directory
29
- os.chdir(DARWIN_SCRIPTS_PATH)
 
 
30
 
31
- # Start the original app.py as a subprocess
32
- DARWIN_APP_PROCESS = subprocess.Popen(
33
- [sys.executable, "app.py"],
34
- env=env,
35
- cwd=DARWIN_SCRIPTS_PATH,
36
- stdout=sys.stdout, # Forward stdout
37
- stderr=sys.stderr # Forward stderr
38
- )
39
 
40
- print(f"Started Darwin app with PID {DARWIN_APP_PROCESS.pid}")
41
- return DARWIN_APP_PROCESS
42
-
43
- def stop_darwin_app():
44
- """Stop the Darwin app subprocess"""
45
- global DARWIN_APP_PROCESS
46
- if DARWIN_APP_PROCESS:
47
- print(f"Stopping Darwin app with PID {DARWIN_APP_PROCESS.pid}")
48
- DARWIN_APP_PROCESS.terminate()
49
- try:
50
- DARWIN_APP_PROCESS.wait(timeout=5)
51
- except subprocess.TimeoutExpired:
52
- DARWIN_APP_PROCESS.kill()
53
- DARWIN_APP_PROCESS = None
54
-
55
- def cleanup():
56
- """Cleanup function to ensure the Darwin app is properly shut down"""
57
- stop_darwin_app()
58
-
59
- # Register cleanup handlers
60
- atexit.register(cleanup)
61
- signal.signal(signal.SIGTERM, lambda sig, frame: cleanup())
62
- signal.signal(signal.SIGINT, lambda sig, frame: cleanup())
63
-
64
- if __name__ == "__main__":
65
- # Start the Darwin app
66
- darwin_process = start_darwin_app()
67
 
68
- # Print startup message
69
- print(f"Darwin Avatar started on port {NICEGUI_PORT}")
70
- print("Access it at: https://your-huggingface-space.hf.space/")
 
 
 
71
 
72
- try:
73
- # Keep the main process running, but forward signals to the child
74
- while darwin_process.poll() is None:
 
 
 
 
75
  time.sleep(1)
76
 
77
- # If we get here, the process has exited
78
- exit_code = darwin_process.returncode
79
- print(f"Darwin app exited with code {exit_code}")
80
- sys.exit(exit_code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- except KeyboardInterrupt:
83
- print("Received interrupt, shutting down...")
84
- cleanup()
 
1
  import os
2
  import sys
3
+ import json
4
+ import threading
5
+ import time
6
  import atexit
7
  import signal
8
+ import gradio as gr
 
9
 
10
+ # Add DarwinChatbot scripts directory to Python path
11
  DARWIN_SCRIPTS_PATH = "/data/DarwinChatbot/scripts"
12
  sys.path.append(DARWIN_SCRIPTS_PATH)
13
 
14
+ # Now import your Darwin project modules
15
+ from Dual_LLM import generate_darwin_response
16
+ from LivePortraitIdle import generate_fixed_chunks
17
+ from LatentSync import run_latentsync_inference
18
+ from SparkTTS import run_tts
19
+ from PlaylistManager import idle_playlist_maker, response_playlist_maker, create_lipsync_playlist
20
+
21
+ # Constants from your original app
22
+ REPO_DIR = os.path.abspath(os.path.join(DARWIN_SCRIPTS_PATH, ".."))
23
+ STREAM_LIVE_DIR = os.path.join(REPO_DIR, "stream", "live")
24
+ STREAM_SPEECH_DIR = os.path.join(REPO_DIR, "stream", "speech")
25
+ PLAYLIST_PATH = os.path.join(REPO_DIR, "stream", "playlist", "playlist.json")
26
+
27
+ # Thread management
28
+ _main_thread = None
29
+ _thread_lock = threading.Lock()
30
+ _shutdown_flag = False
31
+ awaiting_response = False
32
+ user_prompt = ""
33
+ log_entries = []
34
+
35
+ def get_latest_video_from_live_dir():
36
+ """Get the most recent video from the live directory"""
37
+ import glob
38
+ video_files = glob.glob(os.path.join(STREAM_LIVE_DIR, "*.mp4"))
39
+ if not video_files:
40
+ return None
41
+ return max(video_files, key=os.path.getctime)
42
+
43
+ def log_message(message):
44
+ """Add a message to the log"""
45
+ log_entries.append(message)
46
+ print(message)
47
+ return "\n".join(log_entries[-30:]) # Return last 30 log entries
48
+
49
+ def idle_mode():
50
+ log_message("[MAIN] Starting idle mode...")
51
+ idle_playlist_maker()
52
+ log_message("[MAIN] Idle mode finished, waiting for response trigger...")
53
+ return
54
 
55
+ def response_mode(prompt):
56
+ global user_prompt
57
+ log_message("[MAIN] Starting response mode...")
58
 
59
+ if not prompt:
60
+ log_message("[WARNING] No user prompt provided, using default")
61
+ prompt = "Tell me about your theory of evolution."
62
 
63
+ # Step 1: Generate response from the LLM
64
+ log_message("[LLM] Generating Darwin's response...")
65
+ llm_response = generate_darwin_response(prompt)
66
+ log_message(f"[LLM] Response generated: {llm_response[:100]}...")
67
 
68
+ # Step 2: Convert response to speech using TTS
69
+ log_message("[TTS] Converting text to speech...")
70
+ speech_file = run_tts(text=llm_response)
71
+ log_message(f"[TTS] Speech generated: {speech_file}")
72
 
73
+ # Step 3: Generate lip-synced video
74
+ log_message("[SYNC] Running lip sync...")
75
+ success = run_latentsync_inference()
76
+ log_message(f"[SYNC] Lip sync completed with status: {success}")
 
 
 
 
77
 
78
+ # Step 4: Update playlist with the latest video
79
+ if success:
80
+ latest_video = get_latest_video_from_live_dir()
81
+ if latest_video:
82
+ log_message(f"[PLAYBACK] Found latest video: {latest_video}")
83
+
84
+ # Create a playlist with just this video
85
+ relative_path = os.path.relpath(latest_video, REPO_DIR)
86
+ if relative_path.startswith("stream\\") or relative_path.startswith("stream/"):
87
+ relative_path = relative_path[7:] # Skip past "stream/" or "stream\"
88
+
89
+ web_path = relative_path.replace("\\", "/")
90
+ playlist = [web_path]
91
+
92
+ with open(PLAYLIST_PATH, "w") as f:
93
+ json.dump(playlist, f)
94
+
95
+ log_message(f"[PLAYLIST] Updated playlist with single video: {web_path}")
96
+ # Return the path to the latest video so Gradio can display it
97
+ return latest_video, llm_response, log_message("[MAIN] Response completed")
98
+ else:
99
+ log_message("[ERROR] No video found in live directory after sync completion")
 
 
 
 
 
100
 
101
+ return None, llm_response, log_message("[MAIN] Response completed with errors")
102
+
103
+ def main_loop():
104
+ global awaiting_response
105
+ thread_id = threading.get_ident()
106
+ log_message(f"[THREAD] Main loop running in thread {thread_id}")
107
 
108
+ while not _shutdown_flag:
109
+ awaiting_response = False
110
+ idle_mode()
111
+
112
+ # Wait here for response button to be pressed
113
+ log_message("[MAIN] Idle finished, waiting for trigger")
114
+ while not awaiting_response and not _shutdown_flag:
115
  time.sleep(1)
116
 
117
+ # Check if we got shutdown while waiting
118
+ if _shutdown_flag:
119
+ break
120
+
121
+ log_message(f"[THREAD] Thread {thread_id} shutting down")
122
+
123
+ def start_main_thread():
124
+ global _main_thread
125
+ with _thread_lock:
126
+ if _main_thread is None or not _main_thread.is_alive():
127
+ _main_thread = threading.Thread(target=main_loop, daemon=True)
128
+ _main_thread.start()
129
+ log_message(f"[INIT] Main thread started with ID {_main_thread.ident}")
130
+ return True
131
+ else:
132
+ log_message(f"[INIT] Main thread already running with ID {_main_thread.ident}")
133
+ return False
134
+
135
+ def shutdown():
136
+ global _shutdown_flag
137
+ _shutdown_flag = True
138
+ log_message("[APP] Shutdown initiated, waiting for threads to terminate...")
139
+ if _main_thread and _main_thread.is_alive():
140
+ _main_thread.join(timeout=5)
141
+ log_message("[APP] Shutdown complete")
142
+
143
+ # Register shutdown handler
144
+ atexit.register(shutdown)
145
+ signal.signal(signal.SIGTERM, lambda sig, frame: shutdown())
146
+ signal.signal(signal.SIGINT, lambda sig, frame: shutdown())
147
+
148
+ def trigger_response(prompt, logs):
149
+ global awaiting_response, user_prompt
150
+ user_prompt = prompt
151
+ awaiting_response = True
152
+ logs = log_message(f"[UI] Response triggered by user with prompt: {prompt[:50]}...")
153
+
154
+ # Process the response directly
155
+ video_path, response_text, updated_logs = response_mode(prompt)
156
+
157
+ # If we got a video, return it
158
+ if video_path and os.path.exists(video_path):
159
+ return video_path, response_text, updated_logs
160
+ else:
161
+ return None, response_text, updated_logs
162
+
163
+ def create_gradio_interface():
164
+ with gr.Blocks() as demo:
165
+ gr.Markdown("# Darwin Avatar")
166
+
167
+ with gr.Row():
168
+ with gr.Column(scale=2):
169
+ video_output = gr.Video(label="Darwin Avatar")
170
+
171
+ with gr.Column(scale=1):
172
+ text_input = gr.Textbox(label="Your Question", placeholder="Ask Darwin something...")
173
+ text_output = gr.Textbox(label="Darwin's Response", interactive=False)
174
+ submit_btn = gr.Button("Ask Darwin")
175
+ logs = gr.Textbox(label="System Logs", interactive=False, lines=10)
176
+
177
+ # Initialize logs
178
+ logs.update(value=log_message("[INIT] Gradio interface initialized"))
179
+
180
+ # Set up the submit button
181
+ submit_btn.click(
182
+ fn=trigger_response,
183
+ inputs=[text_input, logs],
184
+ outputs=[video_output, text_output, logs]
185
+ )
186
+
187
+ # Start the background thread when the interface loads
188
+ demo.load(lambda: log_message("[INIT] Starting background processing thread..."))
189
+ start_main_thread()
190
+
191
+ return demo
192
+
193
+ if __name__ == "__main__":
194
+ # Ensure the required directories exist
195
+ os.makedirs(STREAM_LIVE_DIR, exist_ok=True)
196
+ os.makedirs(STREAM_SPEECH_DIR, exist_ok=True)
197
+ os.makedirs(os.path.dirname(PLAYLIST_PATH), exist_ok=True)
198
 
199
+ # Create and launch the Gradio interface
200
+ demo = create_gradio_interface()
201
+ demo.launch(server_name="0.0.0.0", server_port=7860)