Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import socketserver
|
|
7 |
import threading
|
8 |
import subprocess
|
9 |
import time
|
|
|
10 |
from dotenv import load_dotenv
|
11 |
from PIL import Image
|
12 |
import io
|
@@ -22,30 +23,54 @@ INSTAGRAM_USER_ID = os.getenv('INSTAGRAM_USER_ID')
|
|
22 |
HUGGINGFACE_API_TOKEN = os.getenv('HUGGINGFACE_API_TOKEN')
|
23 |
UPLOAD_FOLDER = 'uploads'
|
24 |
PORT = 8000
|
|
|
|
|
25 |
|
26 |
# Ensure upload folder exists
|
27 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
29 |
# Start local HTTP server to serve images
|
30 |
-
|
31 |
-
httpd
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Start ngrok to create a public URL
|
37 |
-
ngrok_process = None
|
38 |
-
|
39 |
def start_ngrok():
|
40 |
global ngrok_process
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
# Hugging Face captioning pipeline
|
48 |
-
caption_pipeline = pipeline(
|
|
|
|
|
|
|
|
|
49 |
|
50 |
@app.route('/')
|
51 |
def index():
|
@@ -105,18 +130,33 @@ def upload_image():
|
|
105 |
# Clean up ngrok
|
106 |
if ngrok_process:
|
107 |
ngrok_process.terminate()
|
|
|
108 |
|
109 |
@app.route('/shutdown', methods=['POST'])
|
110 |
def shutdown():
|
111 |
-
httpd
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
if __name__ == '__main__':
|
117 |
try:
|
118 |
-
|
|
|
|
|
|
|
119 |
finally:
|
120 |
-
httpd
|
|
|
|
|
121 |
if ngrok_process:
|
122 |
ngrok_process.terminate()
|
|
|
7 |
import threading
|
8 |
import subprocess
|
9 |
import time
|
10 |
+
import socket
|
11 |
from dotenv import load_dotenv
|
12 |
from PIL import Image
|
13 |
import io
|
|
|
23 |
HUGGINGFACE_API_TOKEN = os.getenv('HUGGINGFACE_API_TOKEN')
|
24 |
UPLOAD_FOLDER = 'uploads'
|
25 |
PORT = 8000
|
26 |
+
httpd = None
|
27 |
+
ngrok_process = None
|
28 |
|
29 |
# Ensure upload folder exists
|
30 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
31 |
|
32 |
+
# Check if port is available
|
33 |
+
def is_port_available(port):
|
34 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
35 |
+
return s.connect_ex(('localhost', port)) != 0
|
36 |
+
|
37 |
# Start local HTTP server to serve images
|
38 |
+
def start_http_server():
|
39 |
+
global httpd
|
40 |
+
Handler = http.server.SimpleHTTPRequestHandler
|
41 |
+
try:
|
42 |
+
if is_port_available(PORT):
|
43 |
+
httpd = socketserver.TCPServer(("", PORT), Handler)
|
44 |
+
server_thread = threading.Thread(target=httpd.serve_forever)
|
45 |
+
server_thread.daemon = True
|
46 |
+
server_thread.start()
|
47 |
+
print(f"HTTP server started on port {PORT}")
|
48 |
+
else:
|
49 |
+
raise OSError(f"Port {PORT} is already in use. Please free the port or choose another.")
|
50 |
+
except Exception as e:
|
51 |
+
print(f"Failed to start HTTP server: {e}")
|
52 |
+
raise
|
53 |
|
54 |
# Start ngrok to create a public URL
|
|
|
|
|
55 |
def start_ngrok():
|
56 |
global ngrok_process
|
57 |
+
try:
|
58 |
+
ngrok_process = subprocess.Popen(["ngrok", "http", str(PORT)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
59 |
+
time.sleep(2) # Wait for ngrok to start
|
60 |
+
resp = requests.get("http://localhost:4040/api/tunnels")
|
61 |
+
public_url = resp.json()["tunnels"][0]["public_url"]
|
62 |
+
print(f"Ngrok tunnel started at {public_url}")
|
63 |
+
return public_url
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Failed to start ngrok: {e}")
|
66 |
+
raise
|
67 |
|
68 |
+
# Hugging Face captioning pipeline with warning suppression
|
69 |
+
caption_pipeline = pipeline(
|
70 |
+
"image-to-text",
|
71 |
+
model="Salesforce/blip-image-captioning-base",
|
72 |
+
clean_up_tokenization_spaces=True # Suppress FutureWarning
|
73 |
+
)
|
74 |
|
75 |
@app.route('/')
|
76 |
def index():
|
|
|
130 |
# Clean up ngrok
|
131 |
if ngrok_process:
|
132 |
ngrok_process.terminate()
|
133 |
+
ngrok_process = None
|
134 |
|
135 |
@app.route('/shutdown', methods=['POST'])
|
136 |
def shutdown():
|
137 |
+
global httpd, ngrok_process
|
138 |
+
try:
|
139 |
+
if httpd:
|
140 |
+
httpd.shutdown()
|
141 |
+
httpd.server_close()
|
142 |
+
print("HTTP server shut down")
|
143 |
+
if ngrok_process:
|
144 |
+
ngrok_process.terminate()
|
145 |
+
ngrok_process = None
|
146 |
+
print("Ngrok shut down")
|
147 |
+
return jsonify({"status": "success", "message": "Server shutdown"})
|
148 |
+
except Exception as e:
|
149 |
+
return jsonify({"status": "error", "message": str(e)})
|
150 |
|
151 |
if __name__ == '__main__':
|
152 |
try:
|
153 |
+
start_http_server()
|
154 |
+
app.run(debug=True, use_reloader=False) # Disable reloader to prevent multiple server instances
|
155 |
+
except Exception as e:
|
156 |
+
print(f"Application failed to start: {e}")
|
157 |
finally:
|
158 |
+
if httpd:
|
159 |
+
httpd.shutdown()
|
160 |
+
httpd.server_close()
|
161 |
if ngrok_process:
|
162 |
ngrok_process.terminate()
|