|
|
from backend.modules import visual_checks, text_checks, content_checks |
|
|
import logging |
|
|
import random |
|
|
import time |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def classify_real(image_path): |
|
|
"""Perform complete classification with detailed results using AI models.""" |
|
|
|
|
|
components = [ |
|
|
visual_checks.image_quality, |
|
|
visual_checks.ribbon, |
|
|
text_checks.tagline, |
|
|
text_checks.tooMuchText, |
|
|
content_checks.theme, |
|
|
content_checks.body, |
|
|
text_checks.cta, |
|
|
text_checks.tnc, |
|
|
visual_checks.gnc |
|
|
] |
|
|
|
|
|
|
|
|
all_results = {} |
|
|
for component in components: |
|
|
try: |
|
|
results = component(image_path) |
|
|
all_results.update(results) |
|
|
except Exception as e: |
|
|
logger.error(f"Error in component {component.__name__}: {e}") |
|
|
pass |
|
|
|
|
|
|
|
|
final_classification = 0 |
|
|
for result in all_results.values(): |
|
|
if isinstance(result, int): |
|
|
if result == 1: |
|
|
final_classification = 1 |
|
|
break |
|
|
elif isinstance(result, str): |
|
|
if result.startswith('1'): |
|
|
final_classification = 1 |
|
|
break |
|
|
|
|
|
|
|
|
classification_result = "Fail" if final_classification == 1 else "Pass" |
|
|
|
|
|
|
|
|
table_data = [] |
|
|
labels = [ |
|
|
"Bad Image Quality", "No Ribbon", "Empty/Illegible/Black Tagline", "Multiple Taglines", |
|
|
"Incomplete Tagline", "Hyperlink", "Price Tag", "Excessive Emojis", "Too Much Text", |
|
|
"Inappropriate Content", "Religious Content", "High Risk Content", |
|
|
"Illegal Content", "Competitor References", "Bad CTA", "Terms & Conditions", |
|
|
"Visual Gesture or Icon" |
|
|
] |
|
|
|
|
|
|
|
|
failure_labels = [] |
|
|
for label in labels: |
|
|
result = all_results.get(label, 0) |
|
|
|
|
|
is_fail = False |
|
|
if isinstance(result, int) and result == 1: |
|
|
is_fail = True |
|
|
elif isinstance(result, str) and result.startswith('1'): |
|
|
is_fail = True |
|
|
|
|
|
if is_fail: |
|
|
failure_labels.append(label) |
|
|
|
|
|
table_data.append([label, result]) |
|
|
|
|
|
|
|
|
return classification_result, table_data, failure_labels |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def classify_dummy(image_path): |
|
|
""" |
|
|
A dummy classification function that returns random results. |
|
|
Useful for testing the frontend without running expensive models. |
|
|
""" |
|
|
|
|
|
time.sleep(1) |
|
|
|
|
|
all_results = { |
|
|
"Bad Image Quality": 1, |
|
|
"No Ribbon": random.choice([0, 1]), |
|
|
"Empty/Illegible/Black Tagline": 1, |
|
|
"Multiple Taglines": 1, |
|
|
"Incomplete Tagline": 1, |
|
|
"Hyperlink": 1, |
|
|
"Price Tag": 1, |
|
|
"Excessive Emojis": 1, |
|
|
"Too Much Text": 1, |
|
|
"Inappropriate Content": 1, |
|
|
"Religious Content": 1, |
|
|
"High Risk Content": 1, |
|
|
"Illegal Content": 1, |
|
|
"Competitor References": 0, |
|
|
"Bad CTA": 0, |
|
|
"Terms & Conditions": 0, |
|
|
"Visual Gesture or Icon": 1 |
|
|
} |
|
|
|
|
|
|
|
|
final_classification = 0 |
|
|
for result in all_results.values(): |
|
|
if isinstance(result, int) and result == 1: |
|
|
final_classification = 1 |
|
|
break |
|
|
elif isinstance(result, str) and result.startswith('1'): |
|
|
final_classification = 1 |
|
|
break |
|
|
|
|
|
classification_result = "Fail" if final_classification == 1 else "Pass" |
|
|
|
|
|
|
|
|
labels = list(all_results.keys()) |
|
|
failure_labels = [] |
|
|
table_data = [] |
|
|
|
|
|
for label in labels: |
|
|
result = all_results[label] |
|
|
is_fail = False |
|
|
if isinstance(result, int) and result == 1: |
|
|
is_fail = True |
|
|
elif isinstance(result, str) and result.startswith('1'): |
|
|
is_fail = True |
|
|
|
|
|
if is_fail: |
|
|
failure_labels.append(label) |
|
|
|
|
|
table_data.append([label, result]) |
|
|
|
|
|
return classification_result, table_data, failure_labels |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
classify = classify_real |