Spaces:
Sleeping
Sleeping
File size: 2,958 Bytes
5159d0d bcda899 5159d0d 0828455 bcda899 5159d0d bcda899 950fff3 2c98356 bcda899 0828455 5159d0d bcda899 0828455 c4446c8 0828455 bcda899 2ea8bc2 0828455 5159d0d c4446c8 a117710 0828455 5159d0d d7aa692 0828455 d7aa692 0828455 bb0b72f 0828455 bb0b72f bcda899 0828455 e053591 2ea8bc2 e141765 bb0b72f 0828455 2ea8bc2 0828455 bcda899 0828455 5159d0d 2ea8bc2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | from flask import Flask, request, jsonify, send_file
from analysis import get_comprehensive_analysis, generate_html_report
from pdf_generator import generate_pdf
import os
import uuid
import json
app = Flask(__name__)
@app.route("/")
def home():
return "YOUV.AI Flask Skin Analysis API is running!", 200
# ---------------------------------------------------------
# 1️⃣ STEP ONE: ANALYSIS ONLY (returns JSON, no PDF)
# ---------------------------------------------------------
@app.route("/analyze", methods=["POST"])
def analyze():
if "image" not in request.files:
return jsonify({"success": False, "error": "No image uploaded"}), 400
image = request.files["image"]
temp_name = f"upload_{uuid.uuid4().hex}.jpg"
image.save(temp_name)
# Optional: Enable multi-pass via query parameter
# Example: /analyze?multipass=true
enable_multipass = request.args.get("multipass", "false").lower() == "true"
analysis = get_comprehensive_analysis(temp_name, enable_multipass=enable_multipass)
os.remove(temp_name)
if not analysis:
return jsonify({"success": False, "error": "Analysis failed"}), 500
return jsonify({
"success": True,
"analysis": analysis
}), 200
# ---------------------------------------------------------
# 2️⃣ STEP TWO: PDF GENERATION (frontend sends JSON here)
# ---------------------------------------------------------
@app.route("/generate_pdf", methods=["POST"])
def generate_pdf_endpoint():
try:
# Case 1: JSON in body
data = request.get_json(silent=True)
# Case 2: JSON string inside form-data
if not data and "json_data" in request.form:
try:
data = json.loads(request.form["json_data"])
except:
return jsonify({
"success": False,
"error": "Invalid JSON inside 'json_data'"
}), 400
if not data or "analysis" not in data:
return jsonify({
"success": False,
"error": "Missing 'analysis' JSON"
}), 400
analysis = data["analysis"]
user_info = {
"name": data.get("name", ""),
"age": data.get("age", ""),
"gender": data.get("gender", ""),
"email": data.get("email", ""),
"phone": data.get("phone", "")
}
# Generate HTML + PDF
html_path = os.path.abspath(f"report_{uuid.uuid4().hex}.html")
pdf_path = os.path.abspath(f"report_{uuid.uuid4().hex}.pdf")
generate_html_report(analysis, user_info, html_path)
generate_pdf(html_path, pdf_path)
return send_file(pdf_path, mimetype="application/pdf", as_attachment=True)
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
# HuggingFace entrypoint
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860) |