Spaces:
No application file
No application file
Upload 3 files
Browse files- app.py.txt +125 -0
- pdf-template.html.txt +36 -0
- requirements.txt.txt +6 -0
app.py.txt
CHANGED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import random
|
| 3 |
+
from PIL import Image, ImageOps
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import mediapipe as mp
|
| 7 |
+
from jinja2 import Template
|
| 8 |
+
from weasyprint import HTML, CSS
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
|
| 11 |
+
mp_face = mp.solutions.face_mesh
|
| 12 |
+
mp_pose = mp.solutions.pose
|
| 13 |
+
|
| 14 |
+
def image_to_np(img):
|
| 15 |
+
return np.array(img.convert("RGB"))
|
| 16 |
+
|
| 17 |
+
def get_face_landmarks(img_np):
|
| 18 |
+
with mp_face.FaceMesh(static_image_mode=True, max_num_faces=1) as fm:
|
| 19 |
+
results = fm.process(img_np[:, :, ::-1])
|
| 20 |
+
return results.multi_face_landmarks[0] if results.multi_face_landmarks else None
|
| 21 |
+
|
| 22 |
+
def get_pose_landmarks(img_np):
|
| 23 |
+
with mp_pose.Pose(static_image_mode=True) as pose:
|
| 24 |
+
results = pose.process(img_np[:, :, ::-1])
|
| 25 |
+
return results.pose_landmarks if results.pose_landmarks else None
|
| 26 |
+
|
| 27 |
+
def safe_percent(x):
|
| 28 |
+
return max(0, min(100, round(x, 1)))
|
| 29 |
+
|
| 30 |
+
def compute_symmetry_score(face_lms, image_w):
|
| 31 |
+
pairs = [(1,33),(61,291),(199,429),(10,152),(234,454)]
|
| 32 |
+
diffs=[]
|
| 33 |
+
for a,b in pairs:
|
| 34 |
+
try:
|
| 35 |
+
xa=face_lms.landmark[a].x*image_w
|
| 36 |
+
xb=face_lms.landmark[b].x*image_w
|
| 37 |
+
diffs.append(abs(xa-(image_w-xb)))
|
| 38 |
+
except: pass
|
| 39 |
+
if not diffs: return 75.0
|
| 40 |
+
avg=sum(diffs)/len(diffs)
|
| 41 |
+
score=100-(avg/(image_w/10)*15)
|
| 42 |
+
return safe_percent(score)
|
| 43 |
+
|
| 44 |
+
def compute_proportion_score(face_lms, image_h):
|
| 45 |
+
try:
|
| 46 |
+
top=face_lms.landmark[10].y*image_h
|
| 47 |
+
mid=face_lms.landmark[1].y*image_h
|
| 48 |
+
chin=face_lms.landmark[152].y*image_h
|
| 49 |
+
upper=mid-top
|
| 50 |
+
midv=chin-mid
|
| 51 |
+
ratio=upper/midv
|
| 52 |
+
score=100-abs(ratio-1.0)*40
|
| 53 |
+
return safe_percent(score)
|
| 54 |
+
except: return 75.0
|
| 55 |
+
|
| 56 |
+
def compute_color_fit_score(img):
|
| 57 |
+
hsv=Image.fromarray(img).convert("HSV")
|
| 58 |
+
s_avg=np.array(hsv.split()[1]).mean()
|
| 59 |
+
return safe_percent(50+(s_avg/255.0)*50)
|
| 60 |
+
|
| 61 |
+
def compute_posture_score(pose_lms):
|
| 62 |
+
try:
|
| 63 |
+
lsh=pose_lms.landmark[11]; rsh=pose_lms.landmark[12]
|
| 64 |
+
lhip=pose_lms.landmark[23]; rhip=pose_lms.landmark[24]
|
| 65 |
+
score=100-((abs(lsh.y-rsh.y)+abs(lhip.y-rhip.y))*150)
|
| 66 |
+
return safe_percent(score)
|
| 67 |
+
except: return 80.0
|
| 68 |
+
|
| 69 |
+
def make_teaser(name, symmetry, proportion, color_fit, posture):
|
| 70 |
+
teaser=f"""✨ AI Glow-Up Teaser — {name if name else 'Your result'} ✨
|
| 71 |
+
• Face Balance: {symmetry}%
|
| 72 |
+
• Facial Proportion Fit: {proportion}%
|
| 73 |
+
• Color Harmony: {color_fit}%
|
| 74 |
+
• Posture / Presence: {posture}%
|
| 75 |
+
Unlock full 20-page plan after purchase for hairstyles, wardrobe, grooming & 30-day checklist."""
|
| 76 |
+
return teaser
|
| 77 |
+
|
| 78 |
+
def render_pdf(context):
|
| 79 |
+
template_html=open("pdf_template.html","r",encoding="utf-8").read()
|
| 80 |
+
html=Template(template_html).render(**context)
|
| 81 |
+
css=CSS(string="@page{size:A4;margin:1.5cm}body{font-family:Helvetica;color:#111}")
|
| 82 |
+
return HTML(string=html).write_pdf(stylesheets=[css])
|
| 83 |
+
|
| 84 |
+
def build_context(name, symmetry, proportion, color_fit, posture, face_shape, suggestions):
|
| 85 |
+
now=datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
|
| 86 |
+
return {"name":name or "Customer","date":now,"symmetry":symmetry,"proportion":proportion,"color_fit":color_fit,"posture":posture,"face_shape":face_shape,"suggestions":suggestions}
|
| 87 |
+
|
| 88 |
+
def analyze_image_return_teaser(img,name=""):
|
| 89 |
+
pil=ImageOps.exif_transpose(img).convert("RGB")
|
| 90 |
+
img_np=image_to_np(pil)
|
| 91 |
+
h,w=img_np.shape[:2]
|
| 92 |
+
try: face_lms=get_face_landmarks(img_np)
|
| 93 |
+
except: face_lms=None
|
| 94 |
+
try: pose_lms=get_pose_landmarks(img_np)
|
| 95 |
+
except: pose_lms=None
|
| 96 |
+
symmetry=compute_symmetry_score(face_lms,w) if face_lms else random.uniform(70,88)
|
| 97 |
+
proportion=compute_proportion_score(face_lms,h) if face_lms else random.uniform(68,90)
|
| 98 |
+
color_fit=compute_color_fit_score(img_np)
|
| 99 |
+
posture=compute_posture_score(pose_lms) if pose_lms else random.uniform(65,90)
|
| 100 |
+
symmetry,proportion,color_fit,posture=map(safe_percent,(symmetry,proportion,color_fit,posture))
|
| 101 |
+
suggestions=["Try mid-length textured crop.","Warm earth tones suit you.","Slight shoulder-roll posture correction."]
|
| 102 |
+
face_shape="Oval / Slight heart influence" if face_lms else "Not detected"
|
| 103 |
+
teaser=make_teaser(name,symmetry,proportion,color_fit,posture)
|
| 104 |
+
return teaser,{"symmetry":symmetry,"proportion":proportion,"color_fit":color_fit,"posture":posture,"face_shape":face_shape,"suggestions":suggestions}
|
| 105 |
+
|
| 106 |
+
def generate_full_pdf_and_return_bytes(img,name="",extra_notes=""):
|
| 107 |
+
teaser,scores=analyze_image_return_teaser(img,name)
|
| 108 |
+
context=build_context(name,scores["symmetry"],scores["proportion"],scores["color_fit"],scores["posture"],scores["face_shape"],scores["suggestions"])
|
| 109 |
+
context["extra_notes"]=extra_notes
|
| 110 |
+
return render_pdf(context)
|
| 111 |
+
|
| 112 |
+
with gr.Blocks() as demo:
|
| 113 |
+
gr.Markdown("# AI Glow-Up Analyzer — Instant Teaser + Full Report")
|
| 114 |
+
with gr.Row():
|
| 115 |
+
inp=gr.Image(type="pil",label="Upload photo")
|
| 116 |
+
name_in=gr.Textbox(label="Name (optional)")
|
| 117 |
+
teaser_out=gr.Textbox(label="Instant Teaser (free)",interactive=False)
|
| 118 |
+
btn_tease=gr.Button("Get Free Teaser")
|
| 119 |
+
btn_full=gr.Button("Generate Full 20-page PDF (paid)")
|
| 120 |
+
pdf_out=gr.File(label="Download full report (PDF)")
|
| 121 |
+
def on_tease(img,name): return analyze_image_return_teaser(img,name)[0] if img else "Upload image"
|
| 122 |
+
def on_full(img,name): return ("report.pdf",io.BytesIO(generate_full_pdf_and_return_bytes(img,name))) if img else None
|
| 123 |
+
btn_tease.click(on_tease,[inp,name_in],[teaser_out])
|
| 124 |
+
btn_full.click(on_full,[inp,name_in],[pdf_out])
|
| 125 |
+
if _name=="main_": demo.launch()
|
pdf-template.html.txt
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<title>AI Glow-Up Report</title>
|
| 6 |
+
<style>
|
| 7 |
+
body{font-family:Helvetica;color:#111}.page{page-break-after:always;padding:18px}.gold{color:#b98f46}
|
| 8 |
+
</style>
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<div class="page">
|
| 12 |
+
<h1 class="gold">AI Appearance Upgrade Report</h1>
|
| 13 |
+
<p>Report for: {{name}} — Generated: {{date}}</p>
|
| 14 |
+
</div>
|
| 15 |
+
<div class="page">
|
| 16 |
+
<h2>Quick Scores</h2>
|
| 17 |
+
<p>Face Balance: {{symmetry}}%</p>
|
| 18 |
+
<p>Proportion Fit: {{proportion}}%</p>
|
| 19 |
+
<p>Color Harmony: {{color_fit}}%</p>
|
| 20 |
+
<p>Posture / Presence: {{posture}}%</p>
|
| 21 |
+
</div>
|
| 22 |
+
<div class="page">
|
| 23 |
+
<h2>Face Shape</h2><p>{{face_shape}}</p>
|
| 24 |
+
</div>
|
| 25 |
+
<div class="page">
|
| 26 |
+
<h2>Suggestions</h2>
|
| 27 |
+
<ul>
|
| 28 |
+
{% for s in suggestions %}<li>{{s}}</li>{% endfor %}
|
| 29 |
+
</ul>
|
| 30 |
+
</div>
|
| 31 |
+
<div class="page">
|
| 32 |
+
<h2>Full 30-Day Plan</h2>
|
| 33 |
+
<p>Detailed hairstyle, wardrobe, grooming, posture, fitness tips.</p>
|
| 34 |
+
</div>
|
| 35 |
+
</body>
|
| 36 |
+
</html>
|
requirements.txt.txt
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.20
|
| 2 |
+
mediapipe>=0.10.1
|
| 3 |
+
Pillow>=9.0.0
|
| 4 |
+
numpy>=1.23
|
| 5 |
+
jinja2>=3.0
|
| 6 |
+
weasyprint>=57.0
|