Spaces:
Running
Running
| 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__) | |
| def home(): | |
| return "YOUV.AI Flask Skin Analysis API is running!", 200 | |
| # --------------------------------------------------------- | |
| # 1️⃣ STEP ONE: ANALYSIS ONLY (returns JSON, no PDF) | |
| # --------------------------------------------------------- | |
| 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) | |
| # --------------------------------------------------------- | |
| 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) |