Ozgur Unlu
commited on
Commit
·
53feca5
1
Parent(s):
38825ee
Basic initial setup
Browse files- app.py +130 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import easyocr
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline, DistilBertTokenizer, DistilBertForSequenceClassification
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import json
|
8 |
+
|
9 |
+
# Initialize OCR reader
|
10 |
+
reader = easyocr.Reader(['en'])
|
11 |
+
|
12 |
+
# Define compliance rules
|
13 |
+
COMPLIANCE_RULES = {
|
14 |
+
"US_SEC": {
|
15 |
+
"required_disclaimers": [
|
16 |
+
"past performance",
|
17 |
+
"investment risks",
|
18 |
+
"regulatory statement"
|
19 |
+
],
|
20 |
+
"prohibited_terms": [
|
21 |
+
"guaranteed returns",
|
22 |
+
"risk-free",
|
23 |
+
"sure thing"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
"UK_FCA": {
|
27 |
+
"required_disclaimers": [
|
28 |
+
"capital at risk",
|
29 |
+
"regulated by FCA"
|
30 |
+
],
|
31 |
+
"prohibited_terms": [
|
32 |
+
"guaranteed profit",
|
33 |
+
"no risk"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
"EU": {
|
37 |
+
"required_disclaimers": [
|
38 |
+
"risk warning",
|
39 |
+
"regulatory information"
|
40 |
+
],
|
41 |
+
"prohibited_terms": [
|
42 |
+
"assured returns",
|
43 |
+
"no losses"
|
44 |
+
]
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
def extract_text_from_image(image):
|
49 |
+
"""Extract text from image using EasyOCR"""
|
50 |
+
result = reader.readtext(np.array(image))
|
51 |
+
return " ".join([text[1] for text in result])
|
52 |
+
|
53 |
+
def check_compliance(text):
|
54 |
+
"""Check text for compliance across all regions"""
|
55 |
+
report = {
|
56 |
+
"compliant": True,
|
57 |
+
"violations": [],
|
58 |
+
"warnings": [],
|
59 |
+
"channel_risks": {
|
60 |
+
"email": 0,
|
61 |
+
"social": 0,
|
62 |
+
"print": 0
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
# Check each region's rules
|
67 |
+
for region, rules in COMPLIANCE_RULES.items():
|
68 |
+
# Check prohibited terms
|
69 |
+
for term in rules["prohibited_terms"]:
|
70 |
+
if term.lower() in text.lower():
|
71 |
+
report["compliant"] = False
|
72 |
+
report["violations"].append(f"{region}: Prohibited term '{term}' found")
|
73 |
+
report["channel_risks"]["email"] += 2
|
74 |
+
report["channel_risks"]["social"] += 2
|
75 |
+
report["channel_risks"]["print"] += 1
|
76 |
+
|
77 |
+
# Check required disclaimers
|
78 |
+
for disclaimer in rules["required_disclaimers"]:
|
79 |
+
if disclaimer.lower() not in text.lower():
|
80 |
+
report["warnings"].append(f"{region}: Missing disclaimer about {disclaimer}")
|
81 |
+
report["channel_risks"]["email"] += 1
|
82 |
+
report["channel_risks"]["social"] += 1
|
83 |
+
report["channel_risks"]["print"] += 1
|
84 |
+
|
85 |
+
return report
|
86 |
+
|
87 |
+
def analyze_ad_copy(image):
|
88 |
+
"""Main function to analyze ad copy"""
|
89 |
+
# Extract text from image
|
90 |
+
text = extract_text_from_image(image)
|
91 |
+
|
92 |
+
# Check compliance
|
93 |
+
compliance_report = check_compliance(text)
|
94 |
+
|
95 |
+
# Generate readable report
|
96 |
+
report_text = "Compliance Analysis Report\n\n"
|
97 |
+
report_text += f"Overall Status: {'✅ Compliant' if compliance_report['compliant'] else '❌ Non-Compliant'}\n\n"
|
98 |
+
|
99 |
+
if compliance_report["violations"]:
|
100 |
+
report_text += "Violations Found:\n"
|
101 |
+
for violation in compliance_report["violations"]:
|
102 |
+
report_text += f"• {violation}\n"
|
103 |
+
report_text += "\n"
|
104 |
+
|
105 |
+
if compliance_report["warnings"]:
|
106 |
+
report_text += "Warnings:\n"
|
107 |
+
for warning in compliance_report["warnings"]:
|
108 |
+
report_text += f"• {warning}\n"
|
109 |
+
report_text += "\n"
|
110 |
+
|
111 |
+
report_text += "Channel Risk Assessment:\n"
|
112 |
+
for channel, risk in compliance_report["channel_risks"].items():
|
113 |
+
risk_level = "Low" if risk < 2 else "Medium" if risk < 4 else "High"
|
114 |
+
report_text += f"• {channel.capitalize()}: {risk_level} Risk\n"
|
115 |
+
|
116 |
+
return report_text
|
117 |
+
|
118 |
+
# Create Gradio interface
|
119 |
+
iface = gr.Interface(
|
120 |
+
fn=analyze_ad_copy,
|
121 |
+
inputs=gr.Image(type="pil"),
|
122 |
+
outputs=gr.Textbox(label="Compliance Report", lines=10),
|
123 |
+
title="Marketing Campaign Compliance Checker",
|
124 |
+
description="Upload marketing material to check compliance with US (SEC), UK (FCA), and EU financial regulations.",
|
125 |
+
examples=[],
|
126 |
+
theme=gr.themes.Base()
|
127 |
+
)
|
128 |
+
|
129 |
+
# Launch the app
|
130 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
easyocr
|
5 |
+
pandas
|
6 |
+
numpy
|
7 |
+
pillow
|