File size: 10,477 Bytes
43ae76e
 
 
 
 
 
 
008f61b
43ae76e
ce9fe2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43ae76e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00ddcdd
43ae76e
 
 
 
 
 
 
f1895c9
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import streamlit as st
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
import numpy as np
import cv2
import streamlit.components.v1 as components

# --- GOOGLE ANALYTICS INJECTION (THE NUCLEAR FIX) ---
GA_ID = "G-JRWLD5D22V"
ga_script = f"""
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={GA_ID}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){{dataLayer.push(arguments);}}
  gtag('js', new Date());
  gtag('config', '{GA_ID}');
</script>
"""

def inject_ga():
    # Locate the streamlit index.html file
    import pathlib
    import shutil
    
    # Find where streamlit is installed
    streamlit_path = pathlib.Path(st.__file__).parent
    index_path = streamlit_path / "static" / "index.html"
    
    # Read the original file
    with open(index_path, 'r') as f:
        html_content = f.read()
        
    # Check if GA is already injected to avoid duplicates
    if GA_ID not in html_content:
        # Inject the script into the <head> tag
        new_html = html_content.replace('<head>', f'<head>{ga_script}')
        
        # Save the modified file
        with open(index_path, 'w') as f:
            f.write(new_html)

# Run the injection function
try:
    inject_ga()
except Exception as e:
    # If file permissions fail (rare on HF), fallback to standard st.html
    print(f"GA Injection Failed: {e}")

# --- 1. CONFIGURATION & STYLING ---
st.set_page_config(
    page_title="Aesthetix AI",
    page_icon="✨",
    layout="centered",
    initial_sidebar_state="collapsed"
)

# Custom CSS for Premium White/Clean Theme
st.markdown("""
    <style>
    /* App Background */
    .stApp {
        background-color: #F8F9FB;
        font-family: 'Helvetica Neue', sans-serif;
    }
    
    /* Hide Streamlit Branding */
    #MainMenu {visibility: hidden;}
    header {visibility: hidden;}
    footer {visibility: hidden;}
    
    /* Main Content Card Style */
    .block-container {
        padding-top: 2rem;
        padding-bottom: 2rem;
    }
    
    /* Custom Headers */
    h1 {
        color: #1A1A1A;
        font-weight: 700;
        letter-spacing: -1px;
        text-align: center;
        padding-bottom: 10px;
    }
    
    p {
        color: #666666;
    }
    
    /* Styled Image Containers */
    div[data-testid="stImage"] {
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 10px 20px rgba(0,0,0,0.05);
        transition: transform 0.3s ease;
    }
    
    /* Score Card */
    .score-card {
        background-color: #FFFFFF;
        padding: 30px;
        border-radius: 20px;
        box-shadow: 0 4px 15px rgba(0,0,0,0.05);
        text-align: center;
        border: 1px solid #EEEEEE;
        margin-top: 20px;
    }
    
    .score-value {
        font-size: 5rem;
        font-weight: 800;
        margin: 0;
        line-height: 1;
    }
    
    .score-label {
        font-size: 1.1rem;
        color: #888;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 2px;
    }
    
    /* Button Styling */
    .stButton > button {
        background: linear-gradient(90deg, #1A1A1A 0%, #333333 100%);
        color: white;
        border: none;
        padding: 12px 28px;
        border-radius: 50px;
        font-weight: 600;
        letter-spacing: 0.5px;
        width: 100%;
        transition: all 0.3s;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }
    
    .stButton > button:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 12px rgba(0,0,0,0.15);
        background: #000000;
    }
    
    /* File Uploader */
    .stFileUploader {
        padding: 20px;
        background-color: #FFFFFF;
        border-radius: 15px;
        border: 1px dashed #DDDDDD;
    }
    </style>
""", unsafe_allow_html=True)

# Header
st.markdown("<h1>✨ Aesthetix AI</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center; margin-top: -15px; margin-bottom: 30px;'>Facial Symmetry & Feature Analysis Engine</p>", unsafe_allow_html=True)

# --- 2. MODEL LOADING ---
@st.cache_resource
def load_models():
    device = torch.device("cpu")
    
    # Rating Model (ResNet18)
    rater = models.resnet18(weights=None)
    num_ftrs = rater.fc.in_features
    rater.fc = nn.Linear(num_ftrs, 1)
    try:
        rater.load_state_dict(torch.load("best_face_rater_colab.pth", map_location=device))
    except FileNotFoundError:
        st.error("⚠️ Model file missing. Upload 'best_face_rater_colab.pth'.")
        return None, None
    rater.eval()

    # Segmentation Model (DeepLabV3)
    seg_model = models.segmentation.deeplabv3_resnet50(weights='DEFAULT')
    seg_model.eval()
    
    return rater, seg_model

rater_model, seg_model = load_models()

# --- 3. PROCESSING LOGIC ---
def isolate_face_pixels(image):
    # Prepare for DeepLabV3
    seg_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    input_tensor = seg_transform(image).unsqueeze(0)
    
    with torch.no_grad():
        output = seg_model(input_tensor)['out'][0]
    
    output_predictions = output.argmax(0)
    # Class 15 is Person
    mask = (output_predictions == 15).byte().numpy()
    
    image_resized = image.resize((224, 224))
    img_np = np.array(image_resized)
    
    # Apply Mask (Black Background)
    mask_3d = np.stack([mask, mask, mask], axis=2)
    foreground = img_np * mask_3d
    
    return Image.fromarray(foreground)

def crop_to_face_strict(image_pil):
    img_np = np.array(image_pil)
    if len(img_np.shape) == 2: img_np = cv2.cvtColor(img_np, cv2.COLOR_GRAY2RGB)
    
    # Haar Cascade
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    
    if len(faces) == 0: return image_pil, False
    
    # Largest Face
    x, y, w, h = max(faces, key=lambda f: f[2] * f[3])
    
    # Margin logic
    margin = int(h * 0.20)
    x = max(0, x - margin)
    y = max(0, y - margin)
    w = min(img_np.shape[1] - x, w + 2*margin)
    h = min(img_np.shape[0] - y, h + 2*margin)
    
    return image_pil.crop((x, y, x+w, y+h)), True

# Grad-CAM Setup
gradients = None
activations = None
def backward_hook(module, grad_input, grad_output):
    global gradients
    gradients = grad_output[0]
def forward_hook(module, input, output):
    global activations
    activations = output

def generate_heatmap(model, input_tensor):
    target_layer = model.layer4[-1]
    handle_f = target_layer.register_forward_hook(forward_hook)
    handle_b = target_layer.register_full_backward_hook(backward_hook)
    
    output = model(input_tensor)
    model.zero_grad()
    output.backward()
    
    pooled_gradients = torch.mean(gradients, dim=[0, 2, 3])
    for i in range(512): activations[:, i, :, :] *= pooled_gradients[i]
    
    heatmap = torch.mean(activations, dim=1).squeeze()
    heatmap = np.maximum(heatmap.detach().numpy(), 0)
    if np.max(heatmap) > 0: heatmap /= np.max(heatmap)
    
    handle_f.remove(); handle_b.remove()
    return heatmap

def overlay_heatmap(heatmap, original_image):
    heatmap = cv2.resize(heatmap, (original_image.width, original_image.height))
    heatmap = np.uint8(255 * heatmap)
    heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
    img_np = np.array(original_image)
    img_np = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
    superimposed_img = heatmap * 0.4 + img_np
    return Image.fromarray(cv2.cvtColor(np.uint8(superimposed_img), cv2.COLOR_BGR2RGB))

# --- 4. MAIN INTERFACE ---

uploaded_file = st.file_uploader("Upload a clear portrait", type=["jpg", "jpeg", "png"])

if uploaded_file is not None and rater_model:
    image = Image.open(uploaded_file).convert('RGB')
    
    # Processing Flow
    with st.spinner("Isolating facial geometry..."):
        cropped_img, found = crop_to_face_strict(image)
        final_input = isolate_face_pixels(cropped_img)

    # UI Columns
    col1, col2 = st.columns(2)
    with col1:
        st.image(image, caption='Original', use_column_width=True)
    with col2:
        st.image(final_input, caption='AI Analysis View', use_column_width=True)

    st.write("")
    
    if st.button('Calculate Score'):
        progress_bar = st.progress(0)
        
        # 1. Transform
        transform = transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])
        input_tensor = transform(final_input).unsqueeze(0)
        input_tensor.requires_grad = True
        
        progress_bar.progress(60)
        
        # 2. Score
        with torch.no_grad():
            output = rater_model(input_tensor)
            score = output.item()
        
        score = max(1.0, min(5.0, score))
        
        # 3. Heatmap (Visual Reasoning)
        heatmap = generate_heatmap(rater_model, input_tensor)
        overlay = overlay_heatmap(heatmap, final_input)
        
        progress_bar.progress(100)
        
        # --- RESULTS DISPLAY ---
        st.markdown("<br>", unsafe_allow_html=True)
        
        # Determine Color Code
        if score >= 4.0: score_color = "#4CAF50" # Green
        elif score >= 3.0: score_color = "#FF9800" # Orange
        else: score_color = "#F44336" # Red
        
        # Metric Card HTML
        st.markdown(f"""
            <div class="score-card">
                <p class="score-label">Aesthetic Rating</p>
                <h1 class="score-value" style="color: {score_color};">{score:.2f}</h1>
                <p style="margin-top: 10px; color: #666;">out of 5.0</p>
            </div>
        """, unsafe_allow_html=True)
        
        st.write("")
        st.image(overlay, caption='Feature Activation Map (Visual Reasoning)', use_column_width=True)
        
        if score >= 4.0:
            st.success("Exceptional features detected. High symmetry and proportion.")
            st.balloons()
        elif score >= 3.0:
            st.info("Strong features detected. Above average structure.")
        else:
            st.warning("Average structure detected. Lighting or angle may affect result.")