HaanIlango commited on
Commit
e297258
·
verified ·
1 Parent(s): c0a278b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -108,11 +108,11 @@ def compute_overall_grade(focus_score, spread_score, read):
108
  total = round(0.55 * focus_score + 0.20 * spread_score + 0.25 * readability_score)
109
  total = max(0, min(100, total))
110
  if total >= 75:
111
- grade = "Good"
112
  elif total >= 50:
113
- grade = "Fair"
114
  else:
115
- grade = "Needs Work"
116
  return total, grade
117
 
118
 
@@ -132,9 +132,15 @@ def generate_recommendations(saliency_map, scores, read, img_h, img_w):
132
  tips.append("Very little of the design pulls attention beyond the main hotspot. Secondary elements may go unnoticed by viewers.")
133
 
134
  if read['contrast_issues'] > 0:
135
- tips.append(f"Fix contrast on {read['contrast_issues']} text element(s) - darken text or lighten its background to reach 4.5:1 minimum.")
 
 
 
136
  if read['small_text_issues'] > 0:
137
- tips.append(f"Increase size on {read['small_text_issues']} text element(s) - text under ~1.2% of image height risks being unreadable.")
 
 
 
138
 
139
  if not tips:
140
  tips.append("No major issues detected - this design is in solid shape.")
@@ -178,6 +184,17 @@ def contrast_ratio(rgb1, rgb2):
178
  return (lighter + 0.05) / (darker + 0.05)
179
 
180
 
 
 
 
 
 
 
 
 
 
 
 
181
  def sample_text_and_bg_color(img_arr, x, y, w, h):
182
  pad = max(2, int(h * 0.3))
183
  y0, y1 = max(0, y - pad), min(img_arr.shape[0], y + h + pad)
@@ -226,7 +243,8 @@ def analyze_readability(pil_img):
226
  if ratio < 4.5:
227
  contrast_issues += 1
228
  if len(issue_lines) < 5:
229
- issue_lines.append(f"- Low contrast on \"{word}\" ({ratio:.1f}:1, needs 4.5:1 minimum)")
 
230
  issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})
231
  if size_pct < 1.2:
232
  small_text_issues += 1
@@ -304,19 +322,20 @@ def analyze(pil_image, overlay_strength):
304
  if read['words_checked'] == 0:
305
  summary += "no text detected\n"
306
  elif read['contrast_issues'] == 0 and read['small_text_issues'] == 0:
307
- summary += "clean (0 issues)\n"
308
  else:
309
- summary += f"{read['contrast_issues'] + read['small_text_issues']} issue(s) found\n"
 
310
 
311
  if read['issue_lines']:
312
- summary += "\n**Readability details:**\n" + "\n".join(read['issue_lines']) + "\n"
313
  summary += (
314
- "\n*Note: contrast is estimated automatically and can misread text sitting on "
315
- "busy or gradient backgrounds. If a flagged item looks clearly readable to your "
316
- "eye, treat it as a false positive.*\n"
317
  )
318
 
319
- summary += "\n**How to Improve:**\n"
320
  for tip in tips:
321
  summary += f"- {tip}\n"
322
 
 
108
  total = round(0.55 * focus_score + 0.20 * spread_score + 0.25 * readability_score)
109
  total = max(0, min(100, total))
110
  if total >= 75:
111
+ grade = "Strong"
112
  elif total >= 50:
113
+ grade = "Good Start"
114
  else:
115
+ grade = "Room to Grow"
116
  return total, grade
117
 
118
 
 
132
  tips.append("Very little of the design pulls attention beyond the main hotspot. Secondary elements may go unnoticed by viewers.")
133
 
134
  if read['contrast_issues'] > 0:
135
+ if read['contrast_issues'] > 5:
136
+ tips.append(f"A quick win: {read['contrast_issues']} text elements could read more clearly. The good news - the pattern (see white boxes marked \"Low contrast\" on the image) suggests one color choice affects most of them, so a single tweak will likely fix most at once.")
137
+ else:
138
+ tips.append(f"{read['contrast_issues']} text element(s) could be easier to read - see the white boxes marked \"Low contrast\" on the image above, then darken the text or lighten its background.")
139
  if read['small_text_issues'] > 0:
140
+ if read['small_text_issues'] > 5:
141
+ tips.append(f"Another quick win: {read['small_text_issues']} text elements are a bit small. Likely one font-size setting affects most of them, so this is usually a fast fix.")
142
+ else:
143
+ tips.append(f"{read['small_text_issues']} text element(s) are a bit small - see the white boxes marked \"Small text\" on the image above.")
144
 
145
  if not tips:
146
  tips.append("No major issues detected - this design is in solid shape.")
 
184
  return (lighter + 0.05) / (darker + 0.05)
185
 
186
 
187
+ def contrast_severity_label(ratio):
188
+ if ratio < 1.5:
189
+ return "could be much clearer"
190
+ elif ratio < 2.5:
191
+ return "could be clearer"
192
+ elif ratio < 3.5:
193
+ return "slightly less clear than ideal"
194
+ else:
195
+ return "close to the recommended level"
196
+
197
+
198
  def sample_text_and_bg_color(img_arr, x, y, w, h):
199
  pad = max(2, int(h * 0.3))
200
  y0, y1 = max(0, y - pad), min(img_arr.shape[0], y + h + pad)
 
243
  if ratio < 4.5:
244
  contrast_issues += 1
245
  if len(issue_lines) < 5:
246
+ severity = contrast_severity_label(ratio)
247
+ issue_lines.append(f"- \"{word}\" is {severity} ({ratio:.1f}:1, needs 4.5:1 minimum)")
248
  issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})
249
  if size_pct < 1.2:
250
  small_text_issues += 1
 
322
  if read['words_checked'] == 0:
323
  summary += "no text detected\n"
324
  elif read['contrast_issues'] == 0 and read['small_text_issues'] == 0:
325
+ summary += "clean, no changes needed\n"
326
  else:
327
+ total_issues = read['contrast_issues'] + read['small_text_issues']
328
+ summary += f"{total_issues} easy improvement(s) spotted - see below\n"
329
 
330
  if read['issue_lines']:
331
+ summary += "\n**A few examples:**\n" + "\n".join(read['issue_lines']) + "\n"
332
  summary += (
333
+ "\n*Note: this is automated and can occasionally flag text that's actually fine, "
334
+ "especially on busy or gradient backgrounds - if something flagged looks perfectly "
335
+ "readable to your eye, trust your eye.*\n"
336
  )
337
 
338
+ summary += "\n**How to Improve:** *(common and usually quick to fix)*\n"
339
  for tip in tips:
340
  summary += f"- {tip}\n"
341