HaanIlango commited on
Commit
c8cb6b3
·
verified ·
1 Parent(s): 9cef2f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -3
app.py CHANGED
@@ -20,8 +20,42 @@ from reportlab.lib.units import inch
20
  from reportlab.lib.styles import getSampleStyleSheet
21
  from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as RLImage
22
 
 
 
23
  _msi_model = None
24
  LOGO_PATH = "logo.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def add_watermark(pil_img, logo_path=LOGO_PATH, scale=0.20, opacity=0.95):
@@ -668,7 +702,7 @@ def flatten_transparency(pil_image):
668
 
669
  def analyze(pil_image, overlay_strength):
670
  if pil_image is None:
671
- return None, None, "Upload an image first."
672
  pil_rgb = flatten_transparency(pil_image)
673
  img_bgr = cv2.cvtColor(np.array(pil_rgb), cv2.COLOR_RGB2BGR)
674
  h, w = img_bgr.shape[:2]
@@ -771,10 +805,14 @@ def analyze(pil_image, overlay_strength):
771
  result_image = add_watermark(Image.fromarray(overlay_rgb))
772
  chart_image = add_watermark(chart_image, scale=0.26)
773
 
774
- return result_image, chart_image, summary
 
 
 
775
 
776
 
777
  with gr.Blocks(title="Design Analyzer") as demo:
 
778
  with gr.Row():
779
  with gr.Column():
780
  image_input = gr.Image(type="pil", label="Upload design")
@@ -788,8 +826,10 @@ with gr.Blocks(title="Design Analyzer") as demo:
788
  with gr.Row():
789
  pdf_btn = gr.Button("Download PDF Report")
790
  pdf_output = gr.File(label="PDF Report")
791
- analyze_btn.click(fn=analyze, inputs=[image_input, strength_slider], outputs=[image_output, chart_output, text_output])
 
792
  pdf_btn.click(fn=export_pdf, inputs=[image_output, chart_output, text_output], outputs=pdf_output)
 
793
  gr.Markdown("---\n© 2026 Haan Ilango. All rights reserved. This tool's methodology and design are original work.")
794
 
795
  demo.launch()
 
20
  from reportlab.lib.styles import getSampleStyleSheet
21
  from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as RLImage
22
 
23
+ import requests
24
+
25
  _msi_model = None
26
  LOGO_PATH = "logo.png"
27
+ COUNTAPI_NAMESPACE = "haanilango-design-analyzer"
28
+ COUNTAPI_KEY = "images-analyzed"
29
+
30
+
31
+ def format_count_badge(count):
32
+ return (
33
+ f'<div style="background-color:#1e2761; color:white; text-align:center; '
34
+ f'padding:20px; border-radius:8px; margin-bottom:10px;">'
35
+ f'<div style="font-size:28px; letter-spacing:4px;">&#9733;&#9733;&#9733;&#9733;&#9733;</div>'
36
+ f'<div style="font-size:20px; font-weight:bold; margin-top:8px;">{count:,} images were tested</div>'
37
+ f'</div>'
38
+ )
39
+
40
+
41
+ def get_current_count():
42
+ try:
43
+ resp = requests.get(f"https://api.countapi.xyz/get/{COUNTAPI_NAMESPACE}/{COUNTAPI_KEY}", timeout=5)
44
+ if resp.status_code == 200:
45
+ return resp.json().get("value", 0)
46
+ except Exception:
47
+ pass
48
+ return 0
49
+
50
+
51
+ def increment_count():
52
+ try:
53
+ resp = requests.get(f"https://api.countapi.xyz/hit/{COUNTAPI_NAMESPACE}/{COUNTAPI_KEY}", timeout=5)
54
+ if resp.status_code == 200:
55
+ return resp.json().get("value", None)
56
+ except Exception:
57
+ pass
58
+ return None
59
 
60
 
61
  def add_watermark(pil_img, logo_path=LOGO_PATH, scale=0.20, opacity=0.95):
 
702
 
703
  def analyze(pil_image, overlay_strength):
704
  if pil_image is None:
705
+ return None, None, "Upload an image first.", gr.update()
706
  pil_rgb = flatten_transparency(pil_image)
707
  img_bgr = cv2.cvtColor(np.array(pil_rgb), cv2.COLOR_RGB2BGR)
708
  h, w = img_bgr.shape[:2]
 
805
  result_image = add_watermark(Image.fromarray(overlay_rgb))
806
  chart_image = add_watermark(chart_image, scale=0.26)
807
 
808
+ new_count = increment_count()
809
+ badge_html = format_count_badge(new_count) if new_count is not None else gr.update()
810
+
811
+ return result_image, chart_image, summary, badge_html
812
 
813
 
814
  with gr.Blocks(title="Design Analyzer") as demo:
815
+ count_display = gr.HTML(format_count_badge(0))
816
  with gr.Row():
817
  with gr.Column():
818
  image_input = gr.Image(type="pil", label="Upload design")
 
826
  with gr.Row():
827
  pdf_btn = gr.Button("Download PDF Report")
828
  pdf_output = gr.File(label="PDF Report")
829
+ analyze_btn.click(fn=analyze, inputs=[image_input, strength_slider],
830
+ outputs=[image_output, chart_output, text_output, count_display])
831
  pdf_btn.click(fn=export_pdf, inputs=[image_output, chart_output, text_output], outputs=pdf_output)
832
+ demo.load(fn=lambda: format_count_badge(get_current_count()), outputs=count_display)
833
  gr.Markdown("---\n© 2026 Haan Ilango. All rights reserved. This tool's methodology and design are original work.")
834
 
835
  demo.launch()