Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,64 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import requests
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
try:
|
7 |
-
html = requests.get(url, timeout=5).text
|
8 |
-
soup = BeautifulSoup(html, "html.parser")
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
if
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
else:
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|