Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,38 +2,50 @@ import http.server
|
|
| 2 |
import socketserver
|
| 3 |
import os
|
| 4 |
import subprocess
|
| 5 |
-
import threading
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
-
# Define the port for the
|
| 9 |
PORT = 7860
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class MyHandler(http.server.SimpleHTTPRequestHandler):
|
| 13 |
def do_GET(self):
|
| 14 |
-
# Serve client.html when the root URL is requested
|
| 15 |
if self.path == '/':
|
| 16 |
self.path = '/client.html'
|
| 17 |
return http.server.SimpleHTTPRequestHandler.do_GET(self)
|
| 18 |
|
| 19 |
-
# Function to run EchoAudio.py in a separate thread
|
| 20 |
-
def run_echo_audio():
|
| 21 |
-
print("Starting EchoAudio.py...")
|
| 22 |
-
# This runs the script as a subprocess
|
| 23 |
-
subprocess.run(["python", "EchoAudio.py"])
|
| 24 |
-
|
| 25 |
if __name__ == "__main__":
|
| 26 |
-
# Start the EchoAudio.py script in a
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
| 35 |
|
| 36 |
-
# Start the
|
| 37 |
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
|
| 38 |
-
print("Serving client.html on port
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import socketserver
|
| 3 |
import os
|
| 4 |
import subprocess
|
|
|
|
| 5 |
import time
|
| 6 |
+
import threading
|
| 7 |
|
| 8 |
+
# Define the public-facing port for the HTTP server
|
| 9 |
PORT = 7860
|
| 10 |
|
| 11 |
+
# Command to run EchoAudio.py
|
| 12 |
+
ECHO_AUDIO_CMD = ["python", "EchoAudio.py"]
|
| 13 |
+
|
| 14 |
+
# Function to run the EchoAudio.py script
|
| 15 |
+
def run_echo_audio():
|
| 16 |
+
print("Starting EchoAudio.py...")
|
| 17 |
+
# This runs the script in the background and waits for it to complete.
|
| 18 |
+
# It's a blocking call, but since it's in a separate thread, it's fine.
|
| 19 |
+
subprocess.run(ECHO_AUDIO_CMD)
|
| 20 |
+
print("EchoAudio.py process has finished.")
|
| 21 |
+
|
| 22 |
+
# Custom handler to serve client.html
|
| 23 |
class MyHandler(http.server.SimpleHTTPRequestHandler):
|
| 24 |
def do_GET(self):
|
|
|
|
| 25 |
if self.path == '/':
|
| 26 |
self.path = '/client.html'
|
| 27 |
return http.server.SimpleHTTPRequestHandler.do_GET(self)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
+
# Start the EchoAudio.py script in a separate thread
|
| 31 |
+
echo_audio_thread = threading.Thread(target=run_echo_audio)
|
| 32 |
+
echo_audio_thread.start()
|
| 33 |
|
| 34 |
+
# Give the WebSocket servers a moment to initialize
|
| 35 |
+
print("Waiting for WebSocket servers to start...")
|
| 36 |
+
time.sleep(10) # ⏳ You might need to adjust this value
|
| 37 |
+
print("Continuing with HTTP server startup.")
|
| 38 |
|
| 39 |
+
# Change to the directory where your files are located
|
| 40 |
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
| 41 |
|
| 42 |
+
# Start the HTTP server on the public port
|
| 43 |
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
|
| 44 |
+
print(f"Serving client.html on port {PORT}")
|
| 45 |
+
try:
|
| 46 |
+
httpd.serve_forever()
|
| 47 |
+
except KeyboardInterrupt:
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
# Wait for the background thread to finish
|
| 51 |
+
echo_audio_thread.join()
|