Update add.py
Browse files
add.py
CHANGED
|
@@ -6,11 +6,23 @@ import shutil
|
|
| 6 |
from pathlib import Path
|
| 7 |
import mimetypes
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
# Create uploads directory
|
| 11 |
UPLOADS_FOLDER = "uploads"
|
| 12 |
os.makedirs(UPLOADS_FOLDER, exist_ok=True)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def process_file(file_obj, file_name, action):
|
| 15 |
"""
|
| 16 |
Process uploaded file: return original or create zip
|
|
@@ -462,10 +474,15 @@ with gr.Blocks(title="File Upload & Download Manager") as iface:
|
|
| 462 |
|
| 463 |
# Launch the app
|
| 464 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
iface.launch(
|
| 466 |
-
server_name="
|
| 467 |
-
server_port=
|
| 468 |
show_error=True,
|
| 469 |
share=False,
|
| 470 |
-
theme=gr.themes.Soft()
|
| 471 |
)
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
import mimetypes
|
| 8 |
import time
|
| 9 |
+
import socket
|
| 10 |
|
| 11 |
# Create uploads directory
|
| 12 |
UPLOADS_FOLDER = "uploads"
|
| 13 |
os.makedirs(UPLOADS_FOLDER, exist_ok=True)
|
| 14 |
|
| 15 |
+
def find_available_port(start_port=7860, max_attempts=10):
|
| 16 |
+
"""Find an available port starting from start_port"""
|
| 17 |
+
for port in range(start_port, start_port + max_attempts):
|
| 18 |
+
try:
|
| 19 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 20 |
+
s.bind(('', port))
|
| 21 |
+
return port
|
| 22 |
+
except OSError:
|
| 23 |
+
continue
|
| 24 |
+
return start_port # Fallback to start_port if none found
|
| 25 |
+
|
| 26 |
def process_file(file_obj, file_name, action):
|
| 27 |
"""
|
| 28 |
Process uploaded file: return original or create zip
|
|
|
|
| 474 |
|
| 475 |
# Launch the app
|
| 476 |
if __name__ == "__main__":
|
| 477 |
+
# Find an available port
|
| 478 |
+
available_port = find_available_port(start_port=7860, max_attempts=20)
|
| 479 |
+
|
| 480 |
+
print(f"Starting server on port {available_port}...")
|
| 481 |
+
|
| 482 |
iface.launch(
|
| 483 |
+
server_name="0.0.0.0", # Changed to 0.0.0.0 for better compatibility
|
| 484 |
+
server_port=available_port, # Use dynamically found port
|
| 485 |
show_error=True,
|
| 486 |
share=False,
|
| 487 |
+
theme=gr.themes.Soft()
|
| 488 |
)
|