Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,56 @@
|
|
| 1 |
-
import
|
| 2 |
from analysis import get_comprehensive_analysis, generate_html_report
|
| 3 |
from pdf_generator import generate_pdf
|
| 4 |
import os
|
|
|
|
| 5 |
|
|
|
|
| 6 |
|
| 7 |
-
def process(image_path):
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return None, None
|
| 13 |
|
| 14 |
-
# 2️⃣ Generate HTML report using SAME analysis JSON
|
| 15 |
-
html_file = generate_html_report(analysis, "new_report.html")
|
| 16 |
-
html_file = os.path.abspath(html_file)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
demo = gr.Interface(
|
| 36 |
-
fn=process,
|
| 37 |
-
inputs=gr.Image(type="filepath", label="Upload Your Image"),
|
| 38 |
-
outputs=[
|
| 39 |
-
gr.JSON(label="Raw JSON Used in Report"),
|
| 40 |
-
gr.File(label="Download Your PDF Report"),
|
| 41 |
-
],
|
| 42 |
-
title="YOUV.AI Skin Analysis Report Generator"
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
from analysis import get_comprehensive_analysis, generate_html_report
|
| 3 |
from pdf_generator import generate_pdf
|
| 4 |
import os
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
+
@app.route("/")
|
| 11 |
+
def home():
|
| 12 |
+
return "YOUV.AI Flask Skin Analysis API is running!", 200
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
@app.route("/analyze", methods=["POST"])
|
| 16 |
+
def analyze():
|
| 17 |
+
# Check if image is uploaded
|
| 18 |
+
if "image" not in request.files:
|
| 19 |
+
return jsonify({"success": False, "error": "No image uploaded"}), 400
|
| 20 |
|
| 21 |
+
image_file = request.files["image"]
|
| 22 |
+
|
| 23 |
+
# Temp file
|
| 24 |
+
temp_name = f"upload_{uuid.uuid4().hex}.jpg"
|
| 25 |
+
image_file.save(temp_name)
|
| 26 |
|
| 27 |
+
# Run AI skin analysis → returns JSON!
|
| 28 |
+
analysis = get_comprehensive_analysis(temp_name)
|
| 29 |
+
if not analysis:
|
| 30 |
+
os.remove(temp_name)
|
| 31 |
+
return jsonify({"success": False, "error": "Analysis failed"}), 500
|
| 32 |
+
|
| 33 |
+
# Create report HTML + PDF
|
| 34 |
+
html_file = generate_html_report(analysis, "new_report.html")
|
| 35 |
+
pdf_file = generate_pdf(html_file, "report.pdf")
|
| 36 |
|
| 37 |
+
# Delete temp upload
|
| 38 |
+
os.remove(temp_name)
|
| 39 |
|
| 40 |
+
# If user wants PDF download directly
|
| 41 |
+
return_pdf = request.form.get("return_pdf", "false").lower() == "true"
|
| 42 |
+
|
| 43 |
+
if return_pdf:
|
| 44 |
+
return send_file(pdf_file, mimetype="application/pdf", as_attachment=True)
|
| 45 |
|
| 46 |
+
# Otherwise return JSON + confirmation PDF exists
|
| 47 |
+
return jsonify({
|
| 48 |
+
"success": True,
|
| 49 |
+
"analysis": analysis,
|
| 50 |
+
"pdf_available": True
|
| 51 |
+
}), 200
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# REQUIRED for HuggingFace Spaces Docker
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
app.run(host="0.0.0.0", port=7860)
|