Spaces:
Sleeping
Sleeping
unknown commited on
Commit ·
b52459a
1
Parent(s): 508c8f2
fix: response to s3 images
Browse files
server.py
CHANGED
|
@@ -13,6 +13,7 @@ import requests
|
|
| 13 |
import uuid
|
| 14 |
import importlib
|
| 15 |
import io
|
|
|
|
| 16 |
from flask import Flask, request, jsonify, send_file
|
| 17 |
import numpy as np
|
| 18 |
|
|
@@ -228,8 +229,8 @@ def create_app() -> Flask:
|
|
| 228 |
print(f"Error in /generate/{visa_type}: {e}")
|
| 229 |
return jsonify({"error": str(e)}), 500
|
| 230 |
|
| 231 |
-
@app.route("/generate-
|
| 232 |
-
def
|
| 233 |
data = request.get_json()
|
| 234 |
bg_color_name = data.get('bg_color_name', 'white')
|
| 235 |
output_size = data.get('output_size', (600, 800))
|
|
@@ -276,13 +277,43 @@ def create_app() -> Flask:
|
|
| 276 |
bottom_margin=1
|
| 277 |
)
|
| 278 |
# Return result
|
|
|
|
| 279 |
is_success, buffer = cv2.imencode(".jpg", final_passport)
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
except Exception as e:
|
| 283 |
print(f"Passport Error: {e}")
|
| 284 |
return jsonify({"error": str(e)}), 500
|
| 285 |
-
|
| 286 |
@app.route('/', methods=['GET'])
|
| 287 |
def hello_world():
|
| 288 |
return "Flask server is running.", 200
|
|
|
|
| 13 |
import uuid
|
| 14 |
import importlib
|
| 15 |
import io
|
| 16 |
+
import boto3
|
| 17 |
from flask import Flask, request, jsonify, send_file
|
| 18 |
import numpy as np
|
| 19 |
|
|
|
|
| 229 |
print(f"Error in /generate/{visa_type}: {e}")
|
| 230 |
return jsonify({"error": str(e)}), 500
|
| 231 |
|
| 232 |
+
@app.route("/generate-visa-photo", methods=["POST"])
|
| 233 |
+
def generate_passport_visa():
|
| 234 |
data = request.get_json()
|
| 235 |
bg_color_name = data.get('bg_color_name', 'white')
|
| 236 |
output_size = data.get('output_size', (600, 800))
|
|
|
|
| 277 |
bottom_margin=1
|
| 278 |
)
|
| 279 |
# Return result
|
| 280 |
+
# Encode image to memory buffer
|
| 281 |
is_success, buffer = cv2.imencode(".jpg", final_passport)
|
| 282 |
+
if not is_success:
|
| 283 |
+
return jsonify({"error": "Failed to encode image"}), 500
|
| 284 |
+
|
| 285 |
+
# Prepare for S3 Upload
|
| 286 |
+
file_stream = io.BytesIO(buffer)
|
| 287 |
+
s3_client = boto3.client('s3',
|
| 288 |
+
aws_access_key_id=os.getenv("AWS_ACCESS_KEY"),
|
| 289 |
+
aws_secret_access_key=os.getenv("AWS_SECRET_KEY"),
|
| 290 |
+
region_name=os.getenv("AWS_REGION")
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
# You should set this env var or hardcode your bucket
|
| 294 |
+
S3_BUCKET_NAME = os.getenv("S3_PUBLIC_BUCKET", "spun-core-api-temp-development")
|
| 295 |
+
file_key = f"generated-visa-photo/{uuid.uuid4()}.jpg"
|
| 296 |
+
|
| 297 |
+
# Upload
|
| 298 |
+
s3_client.upload_fileobj(
|
| 299 |
+
file_stream,
|
| 300 |
+
S3_BUCKET_NAME,
|
| 301 |
+
file_key,
|
| 302 |
+
ExtraArgs={'ContentType': 'image/jpeg', 'ACL': 'public-read'} # ACL optional depending on bucket settings
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
# Construct URL
|
| 306 |
+
# Note: This assumes standard AWS S3 URL format. Adjust if using CloudFront.
|
| 307 |
+
s3_url = f"https://{S3_BUCKET_NAME}.s3.amazonaws.com/{file_key}"
|
| 308 |
+
|
| 309 |
+
return jsonify({
|
| 310 |
+
"status": "success",
|
| 311 |
+
"url": s3_url
|
| 312 |
+
})
|
| 313 |
|
| 314 |
except Exception as e:
|
| 315 |
print(f"Passport Error: {e}")
|
| 316 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 317 |
@app.route('/', methods=['GET'])
|
| 318 |
def hello_world():
|
| 319 |
return "Flask server is running.", 200
|