Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,24 +38,30 @@ def is_port_available(port):
|
|
| 38 |
def start_http_server():
|
| 39 |
global httpd
|
| 40 |
Handler = http.server.SimpleHTTPRequestHandler
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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(
|
| 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"]
|
|
@@ -65,12 +71,12 @@ def start_ngrok():
|
|
| 65 |
print(f"Failed to start ngrok: {e}")
|
| 66 |
raise
|
| 67 |
|
| 68 |
-
# Hugging Face captioning pipeline
|
| 69 |
-
|
| 70 |
-
"image-to-text",
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
@app.route('/')
|
| 76 |
def index():
|
|
@@ -78,6 +84,7 @@ def index():
|
|
| 78 |
|
| 79 |
@app.route('/upload', methods=['POST'])
|
| 80 |
def upload_image():
|
|
|
|
| 81 |
try:
|
| 82 |
# Get base64 image from request
|
| 83 |
image_data = request.json['image'].split(',')[1]
|
|
@@ -89,12 +96,18 @@ def upload_image():
|
|
| 89 |
f.write(image_bytes)
|
| 90 |
|
| 91 |
# Generate caption using Hugging Face
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Get public URL from ngrok
|
| 97 |
-
|
|
|
|
| 98 |
image_url = f"{public_url}/uploads/captured_image.jpg"
|
| 99 |
|
| 100 |
# Create Instagram media container
|
|
@@ -149,14 +162,4 @@ def shutdown():
|
|
| 149 |
return jsonify({"status": "error", "message": str(e)})
|
| 150 |
|
| 151 |
if __name__ == '__main__':
|
| 152 |
-
|
| 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()
|
|
|
|
| 38 |
def start_http_server():
|
| 39 |
global httpd
|
| 40 |
Handler = http.server.SimpleHTTPRequestHandler
|
| 41 |
+
port = PORT
|
| 42 |
+
max_attempts = 5
|
| 43 |
+
for i in range(max_attempts):
|
| 44 |
+
if is_port_available(port):
|
| 45 |
+
try:
|
| 46 |
+
httpd = socketserver.TCPServer(("", port), Handler)
|
| 47 |
+
server_thread = threading.Thread(target=httpd.serve_forever)
|
| 48 |
+
server_thread.daemon = True
|
| 49 |
+
server_thread.start()
|
| 50 |
+
print(f"HTTP server started on port {port}")
|
| 51 |
+
return port
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Failed to start HTTP server on port {port}: {e}")
|
| 54 |
+
raise
|
| 55 |
else:
|
| 56 |
+
print(f"Port {port} is in use, trying port {port + 1}")
|
| 57 |
+
port += 1
|
| 58 |
+
raise OSError("No available ports found after multiple attempts")
|
|
|
|
| 59 |
|
| 60 |
# Start ngrok to create a public URL
|
| 61 |
+
def start_ngrok(port):
|
| 62 |
global ngrok_process
|
| 63 |
try:
|
| 64 |
+
ngrok_process = subprocess.Popen(["ngrok", "http", str(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 65 |
time.sleep(2) # Wait for ngrok to start
|
| 66 |
resp = requests.get("http://localhost:4040/api/tunnels")
|
| 67 |
public_url = resp.json()["tunnels"][0]["public_url"]
|
|
|
|
| 71 |
print(f"Failed to start ngrok: {e}")
|
| 72 |
raise
|
| 73 |
|
| 74 |
+
# Hugging Face captioning pipeline
|
| 75 |
+
try:
|
| 76 |
+
caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Failed to initialize Hugging Face pipeline: {e}")
|
| 79 |
+
caption_pipeline = None
|
| 80 |
|
| 81 |
@app.route('/')
|
| 82 |
def index():
|
|
|
|
| 84 |
|
| 85 |
@app.route('/upload', methods=['POST'])
|
| 86 |
def upload_image():
|
| 87 |
+
global ngrok_process
|
| 88 |
try:
|
| 89 |
# Get base64 image from request
|
| 90 |
image_data = request.json['image'].split(',')[1]
|
|
|
|
| 96 |
f.write(image_bytes)
|
| 97 |
|
| 98 |
# Generate caption using Hugging Face
|
| 99 |
+
caption = "Default caption: Uploaded image"
|
| 100 |
+
if caption_pipeline:
|
| 101 |
+
try:
|
| 102 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 103 |
+
caption_result = caption_pipeline(image)
|
| 104 |
+
caption = caption_result[0]['generated_text']
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Failed to generate caption: {e}")
|
| 107 |
|
| 108 |
# Get public URL from ngrok
|
| 109 |
+
port = start_http_server()
|
| 110 |
+
public_url = start_ngrok(port)
|
| 111 |
image_url = f"{public_url}/uploads/captured_image.jpg"
|
| 112 |
|
| 113 |
# Create Instagram media container
|
|
|
|
| 162 |
return jsonify({"status": "error", "message": str(e)})
|
| 163 |
|
| 164 |
if __name__ == '__main__':
|
| 165 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|