Spaces:
Build error
Build error
Commit
·
3df0e86
1
Parent(s):
f465388
add incredibly hacky gradxpress launcher
Browse files
app.py
CHANGED
|
@@ -1,14 +1,96 @@
|
|
| 1 |
import subprocess
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
# Spawn the process
|
| 10 |
-
process = subprocess.Popen(command, cwd="./mmapi", stdout=log_file, stderr=subprocess.STDOUT,
|
| 11 |
-
stdin=subprocess.DEVNULL, close_fds=True,
|
| 12 |
-
start_new_session=True)
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import os
|
| 3 |
+
import socket
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
+
# Function to check if a port is in use
|
| 8 |
+
def is_port_in_use(port):
|
| 9 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 10 |
+
return sock.connect_ex(('localhost', port)) == 0
|
| 11 |
|
| 12 |
+
# Log file path
|
| 13 |
+
log_file_path = 'log.txt'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Log the startup time
|
| 16 |
+
with open(log_file_path, 'a') as log_file:
|
| 17 |
+
log_file.write(f"\n[{datetime.now()}] Script started.\n")
|
| 18 |
+
|
| 19 |
+
# Port number to check
|
| 20 |
+
port_to_check = 7860
|
| 21 |
+
|
| 22 |
+
# Check if the port is already in use
|
| 23 |
+
if is_port_in_use(port_to_check):
|
| 24 |
+
print("API service already running, enjoy!")
|
| 25 |
+
with open(log_file_path, 'a') as log_file:
|
| 26 |
+
log_file.write(f"[{datetime.now()}] API service already running on port {port_to_check}, exiting script.\n")
|
| 27 |
+
else:
|
| 28 |
+
# Define the command to run
|
| 29 |
+
command = ['node', 'api.js']
|
| 30 |
+
|
| 31 |
+
# Log the startup information
|
| 32 |
+
with open(log_file_path, 'a') as log_file:
|
| 33 |
+
log_file.write(f"[{datetime.now()}] No service found on port {port_to_check}. Starting API service...\n")
|
| 34 |
+
|
| 35 |
+
# Open the log file for writing the process output
|
| 36 |
+
with open(log_file_path, 'a') as log_file:
|
| 37 |
+
# Spawn the node process if the port is not in use
|
| 38 |
+
process = subprocess.Popen(command, stdout=log_file, stderr=subprocess.STDOUT,
|
| 39 |
+
stdin=subprocess.DEVNULL, close_fds=True,
|
| 40 |
+
start_new_session=True)
|
| 41 |
+
|
| 42 |
+
# Log the process spawn success
|
| 43 |
+
with open(log_file_path, 'a') as log_file:
|
| 44 |
+
log_file.write(f"[{datetime.now()}] API service started and running in the background.\n")
|
| 45 |
+
|
| 46 |
+
# Gradio app to display the HTML documentation
|
| 47 |
+
documentation_html = """
|
| 48 |
+
<!DOCTYPE html>
|
| 49 |
+
<html lang="en">
|
| 50 |
+
<head>
|
| 51 |
+
<meta charset="UTF-8">
|
| 52 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 53 |
+
<title>API Documentation</title>
|
| 54 |
+
<style>
|
| 55 |
+
body {
|
| 56 |
+
font-family: Arial, sans-serif;
|
| 57 |
+
line-height: 1.6;
|
| 58 |
+
max-width: 800px;
|
| 59 |
+
margin: 20px auto;
|
| 60 |
+
padding: 0 20px;
|
| 61 |
+
color: #333;
|
| 62 |
+
}
|
| 63 |
+
h1 {
|
| 64 |
+
color: #0056b3;
|
| 65 |
+
}
|
| 66 |
+
code {
|
| 67 |
+
background: #f4f4f4;
|
| 68 |
+
padding: 2px 6px;
|
| 69 |
+
border-radius: 4px;
|
| 70 |
+
}
|
| 71 |
+
pre {
|
| 72 |
+
background: #f4f4f4;
|
| 73 |
+
padding: 10px;
|
| 74 |
+
border-radius: 4px;
|
| 75 |
+
overflow-x: auto;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
</head>
|
| 79 |
+
<body>
|
| 80 |
+
<h1>API Documentation</h1>
|
| 81 |
+
<p>This page documents the available APIs for the Node.js service, detailing endpoints, parameters, and expected responses.</p>
|
| 82 |
+
<!-- Content omitted for brevity -->
|
| 83 |
+
</body>
|
| 84 |
+
</html>
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
# Create a Gradio interface that displays the HTML documentation
|
| 88 |
+
def show_documentation():
|
| 89 |
+
return documentation_html
|
| 90 |
+
|
| 91 |
+
doc_app = gr.Blocks()
|
| 92 |
+
with doc_app:
|
| 93 |
+
gr.HTML(show_documentation())
|
| 94 |
+
|
| 95 |
+
# Launch the Gradio app
|
| 96 |
+
doc_app.launch(server_port=7861, server_name="0.0.0.0")
|