Amandeep01 commited on
Commit
8682e4d
Β·
verified Β·
1 Parent(s): 9f212d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -27
app.py CHANGED
@@ -1,33 +1,64 @@
 
 
1
  import gradio as gr
 
 
2
  import requests
3
- from bs4 import BeautifulSoup
4
-
5
- def analyze_ads(url):
6
- try:
7
- html = requests.get(url, timeout=5).text
8
- soup = BeautifulSoup(html, "html.parser")
9
 
10
- ads = soup.find_all(lambda tag: tag.name in ["iframe", "div", "img"]
11
- and any(k in (tag.get("id", "") + str(tag.get("class", ""))).lower()
12
- for k in ["ad", "banner", "sponsor", "promo", "google"]))
13
 
14
- total_ads = len(ads)
 
 
 
 
 
15
 
16
- if total_ads == 0:
17
- return "πŸ“­ Koi ad nahi mila. Aap top ya sidebar par ad laga sakte hain."
18
- elif total_ads <= 2:
19
- return f"🧐 {total_ads} ad mila. Thoda aur visible jagah pe ads lagane ki zarurat hai."
 
 
20
  else:
21
- return f"βœ… {total_ads} ads mile. Achha coverage hai! Par top section par focus karein."
22
-
23
- except Exception as e:
24
- return "❌ Galat URL ya website load nahi hui."
25
-
26
- # Gradio interface
27
- gr.Interface(
28
- fn=analyze_ads,
29
- inputs=gr.Textbox(lines=1, placeholder="Apni website ka URL daalein..."),
30
- outputs="text",
31
- title="πŸ“Š AdVision AI",
32
- description="Website ad visibility checker. Apne site ka URL dijiye aur ad analysis lijiye!"
33
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AdVision AI: Simple Ad Detector & Suggestion Tool for Hugging Face
2
+
3
  import gradio as gr
4
+ from PIL import Image, ImageDraw
5
+ import torch
6
  import requests
7
+ from io import BytesIO
8
+ import numpy as np
9
+ from ultralytics import YOLO
 
 
 
10
 
11
+ # Load YOLOv8 model (assumes pretrained for ad/banner detection)
12
+ # You can also use your custom model from Roboflow or YOLO
13
+ model = YOLO("yolov8n.pt") # Change to custom ad-detection model if available
14
 
15
+ # Function to analyze ad positions and give suggestions
16
+ def analyze_ad_positions(detections, image_height):
17
+ insights = []
18
+ for box in detections:
19
+ x1, y1, x2, y2 = box[:4]
20
+ center_y = (y1 + y2) / 2
21
 
22
+ if center_y < image_height * 0.33:
23
+ pos = "Top"
24
+ suggestion = "Good visibility βœ…"
25
+ elif center_y < image_height * 0.66:
26
+ pos = "Middle"
27
+ suggestion = "Moderate visibility ⚠️ Consider moving up"
28
  else:
29
+ pos = "Bottom"
30
+ suggestion = "Low visibility ❌ Move above the fold"
31
+
32
+ insights.append(f"Ad at {pos} β†’ {suggestion}")
33
+ return insights
34
+
35
+ # Main prediction function
36
+ def detect_ads(image):
37
+ img = image.convert("RGB")
38
+ image_array = np.array(img)
39
+
40
+ results = model(image_array)[0] # First result
41
+ detections = []
42
+ draw = ImageDraw.Draw(img)
43
+
44
+ for box in results.boxes.xyxy:
45
+ x1, y1, x2, y2 = map(int, box.tolist())
46
+ draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
47
+ draw.text((x1, y1 - 10), "Ad", fill="red")
48
+ detections.append((x1, y1, x2, y2))
49
+
50
+ suggestions = analyze_ad_positions(detections, img.height)
51
+ suggestions_text = "\n".join(suggestions) if suggestions else "No ads detected."
52
+
53
+ return img, suggestions_text
54
+
55
+ # Gradio Interface
56
+ interface = gr.Interface(
57
+ fn=detect_ads,
58
+ inputs=gr.Image(type="pil"),
59
+ outputs=[gr.Image(type="pil"), gr.Textbox()],
60
+ title="AdVision AI",
61
+ description="Upload a webpage screenshot. Detects ad placements and gives visibility suggestions."
62
+ )
63
+
64
+ interface.launch()