Update app.py
Browse files
app.py
CHANGED
|
@@ -115,10 +115,8 @@ class TSZDetector:
|
|
| 115 |
placeholder = config["placeholder"]
|
| 116 |
|
| 117 |
for match in re.finditer(pattern, text, re.IGNORECASE):
|
| 118 |
-
# Avoid duplicate detections for overlapping patterns
|
| 119 |
value = match.group()
|
| 120 |
|
| 121 |
-
# Skip if this looks like a false positive
|
| 122 |
if pii_type == "TC_KIMLIK" and len(value) != 11:
|
| 123 |
continue
|
| 124 |
|
|
@@ -130,15 +128,12 @@ class TSZDetector:
|
|
| 130 |
confidence="HIGH" if len(value) > 8 else "MEDIUM"
|
| 131 |
))
|
| 132 |
|
| 133 |
-
# Sort by position (reverse) for correct replacement
|
| 134 |
detections.sort(key=lambda x: x.start, reverse=True)
|
| 135 |
|
| 136 |
-
# Redact in reverse order to maintain positions
|
| 137 |
for det in detections:
|
| 138 |
placeholder = self.PATTERNS[det.type]["placeholder"]
|
| 139 |
redacted_text = redacted_text[:det.start] + placeholder + redacted_text[det.end:]
|
| 140 |
|
| 141 |
-
# Re-sort for display (by position, ascending)
|
| 142 |
detections.sort(key=lambda x: x.start)
|
| 143 |
|
| 144 |
return detections, redacted_text
|
|
@@ -152,22 +147,17 @@ detector = TSZDetector()
|
|
| 152 |
def analyze_text(text: str) -> Tuple[str, str, str]:
|
| 153 |
"""
|
| 154 |
Main analysis function for Gradio interface.
|
| 155 |
-
|
| 156 |
-
Returns:
|
| 157 |
-
Tuple of (redacted_text, detection_report, stats)
|
| 158 |
"""
|
| 159 |
if not text.strip():
|
| 160 |
-
return "", "
|
| 161 |
|
| 162 |
detections, redacted_text = detector.detect(text)
|
| 163 |
|
| 164 |
if not detections:
|
| 165 |
-
return text, "
|
| 166 |
|
| 167 |
-
|
| 168 |
-
report_lines = ["### 🔍 Detected Entities\n"]
|
| 169 |
|
| 170 |
-
# Group by type
|
| 171 |
by_type = {}
|
| 172 |
for det in detections:
|
| 173 |
if det.type not in by_type:
|
|
@@ -182,110 +172,58 @@ def analyze_text(text: str) -> Tuple[str, str, str]:
|
|
| 182 |
report_lines.append(f"- `{masked_value}` (confidence: {item.confidence})")
|
| 183 |
|
| 184 |
report = "\n".join(report_lines)
|
| 185 |
-
|
| 186 |
-
# Stats
|
| 187 |
stats = f"**{len(detections)}** PII entities found across **{len(by_type)}** categories"
|
| 188 |
|
| 189 |
return redacted_text, report, stats
|
| 190 |
|
| 191 |
-
# Example texts for users to try
|
| 192 |
EXAMPLES = [
|
| 193 |
-
["
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
["
|
| 197 |
-
Name: Jane Doe
|
| 198 |
-
Email: jane.doe@example.org
|
| 199 |
-
Phone: (555) 987-6543
|
| 200 |
-
Credit Card: 4532015112830366
|
| 201 |
-
Shipping Address: 123 Main St, New York, NY 10001"""],
|
| 202 |
-
|
| 203 |
-
["""API Configuration:
|
| 204 |
-
api_key = "sk-1234567890abcdefghijklmnopqrstuvwxyz"
|
| 205 |
-
AWS_ACCESS_KEY: AKIAIOSFODNN7EXAMPLE
|
| 206 |
-
password: "super_secret_123"
|
| 207 |
-
database_url: postgres://user:pass@192.168.1.100:5432/db"""],
|
| 208 |
-
|
| 209 |
-
["""Merhaba, ben Ahmet Yılmaz. TC Kimlik numaram 12345678901.
|
| 210 |
-
Telefon: 0532 123 45 67
|
| 211 |
-
IBAN: TR330006100519786457841326
|
| 212 |
-
Email: ahmet.yilmaz@sirket.com.tr"""],
|
| 213 |
]
|
| 214 |
|
| 215 |
-
|
| 216 |
-
custom_css = """
|
| 217 |
-
.gradio-container {
|
| 218 |
-
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 219 |
-
}
|
| 220 |
-
.detection-box {
|
| 221 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 222 |
-
border-radius: 12px;
|
| 223 |
-
padding: 20px;
|
| 224 |
-
}
|
| 225 |
-
footer {
|
| 226 |
-
visibility: hidden;
|
| 227 |
-
}
|
| 228 |
-
"""
|
| 229 |
-
|
| 230 |
-
# Build the interface
|
| 231 |
-
with gr.Blocks(css=custom_css, title="TSZ - Thyris Safe Zone Demo") as demo:
|
| 232 |
|
| 233 |
-
# Header
|
| 234 |
gr.Markdown("""
|
| 235 |
-
#
|
| 236 |
|
| 237 |
**TSZ** is an open-source PII detection and guardrails engine that prevents sensitive data
|
| 238 |
-
from leaking to LLMs and third-party APIs.
|
| 239 |
|
| 240 |
-
|
| 241 |
-
<a href="https://github.com/thyrisAI/safe-zone" target="_blank">
|
| 242 |
-
<img src="https://img.shields.io/badge/GitHub-Repository-black?logo=github" alt="GitHub">
|
| 243 |
-
</a>
|
| 244 |
-
<a href="https://thyris.ai" target="_blank">
|
| 245 |
-
<img src="https://img.shields.io/badge/Website-thyris.ai-blue" alt="Website">
|
| 246 |
-
</a>
|
| 247 |
-
<a href="https://github.com/thyrisAI/safe-zone/blob/main/LICENSE" target="_blank">
|
| 248 |
-
<img src="https://img.shields.io/badge/License-Apache%202.0-green" alt="License">
|
| 249 |
-
</a>
|
| 250 |
-
</div>
|
| 251 |
""")
|
| 252 |
|
| 253 |
with gr.Row():
|
| 254 |
-
with gr.Column(
|
| 255 |
-
# Input
|
| 256 |
input_text = gr.Textbox(
|
| 257 |
-
label="
|
| 258 |
placeholder="Paste text containing emails, phone numbers, credit cards, API keys, passwords, etc...",
|
| 259 |
-
lines=
|
| 260 |
-
max_lines=20
|
| 261 |
)
|
| 262 |
|
| 263 |
-
analyze_btn = gr.Button("
|
| 264 |
|
| 265 |
-
gr.Markdown("###
|
| 266 |
gr.Examples(
|
| 267 |
examples=EXAMPLES,
|
| 268 |
-
inputs=input_text
|
| 269 |
-
label=""
|
| 270 |
)
|
| 271 |
|
| 272 |
-
with gr.Column(
|
| 273 |
-
# Outputs
|
| 274 |
stats_output = gr.Markdown(label="Statistics")
|
| 275 |
|
| 276 |
redacted_output = gr.Textbox(
|
| 277 |
-
label="
|
| 278 |
-
lines=
|
| 279 |
-
max_lines=20,
|
| 280 |
interactive=False
|
| 281 |
)
|
| 282 |
|
| 283 |
detection_report = gr.Markdown(label="Detection Report")
|
| 284 |
|
| 285 |
-
# Supported types info
|
| 286 |
gr.Markdown("""
|
| 287 |
---
|
| 288 |
-
###
|
| 289 |
|
| 290 |
| Category | Types |
|
| 291 |
|----------|-------|
|
|
@@ -295,33 +233,23 @@ with gr.Blocks(css=custom_css, title="TSZ - Thyris Safe Zone Demo") as demo:
|
|
| 295 |
|
| 296 |
---
|
| 297 |
|
| 298 |
-
###
|
| 299 |
|
| 300 |
This demo uses a simplified detection engine. The full **TSZ** includes:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
-
- ✅ More comprehensive pattern matching with validators
|
| 303 |
-
- ✅ Configurable guardrails and policies
|
| 304 |
-
- ✅ Allowlist/blocklist management
|
| 305 |
-
- ✅ REST API with hot-reloadable rules
|
| 306 |
-
- ✅ High-performance Go implementation with Redis caching
|
| 307 |
-
- ✅ Docker deployment ready
|
| 308 |
-
|
| 309 |
-
**Install TSZ:**
|
| 310 |
```bash
|
| 311 |
git clone https://github.com/thyrisAI/safe-zone.git
|
| 312 |
cd safe-zone
|
| 313 |
docker-compose up -d
|
| 314 |
```
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
<p style="text-align: center; color: #666;">
|
| 319 |
-
Built with ❤️ by <a href="https://thyris.ai" target="_blank">Thyris.AI</a> |
|
| 320 |
-
Licensed under Apache 2.0
|
| 321 |
-
</p>
|
| 322 |
""")
|
| 323 |
|
| 324 |
-
# Event handlers
|
| 325 |
analyze_btn.click(
|
| 326 |
fn=analyze_text,
|
| 327 |
inputs=[input_text],
|
|
@@ -334,6 +262,5 @@ with gr.Blocks(css=custom_css, title="TSZ - Thyris Safe Zone Demo") as demo:
|
|
| 334 |
outputs=[redacted_output, detection_report, stats_output]
|
| 335 |
)
|
| 336 |
|
| 337 |
-
# Launch
|
| 338 |
if __name__ == "__main__":
|
| 339 |
demo.launch()
|
|
|
|
| 115 |
placeholder = config["placeholder"]
|
| 116 |
|
| 117 |
for match in re.finditer(pattern, text, re.IGNORECASE):
|
|
|
|
| 118 |
value = match.group()
|
| 119 |
|
|
|
|
| 120 |
if pii_type == "TC_KIMLIK" and len(value) != 11:
|
| 121 |
continue
|
| 122 |
|
|
|
|
| 128 |
confidence="HIGH" if len(value) > 8 else "MEDIUM"
|
| 129 |
))
|
| 130 |
|
|
|
|
| 131 |
detections.sort(key=lambda x: x.start, reverse=True)
|
| 132 |
|
|
|
|
| 133 |
for det in detections:
|
| 134 |
placeholder = self.PATTERNS[det.type]["placeholder"]
|
| 135 |
redacted_text = redacted_text[:det.start] + placeholder + redacted_text[det.end:]
|
| 136 |
|
|
|
|
| 137 |
detections.sort(key=lambda x: x.start)
|
| 138 |
|
| 139 |
return detections, redacted_text
|
|
|
|
| 147 |
def analyze_text(text: str) -> Tuple[str, str, str]:
|
| 148 |
"""
|
| 149 |
Main analysis function for Gradio interface.
|
|
|
|
|
|
|
|
|
|
| 150 |
"""
|
| 151 |
if not text.strip():
|
| 152 |
+
return "", "Please enter some text to analyze.", ""
|
| 153 |
|
| 154 |
detections, redacted_text = detector.detect(text)
|
| 155 |
|
| 156 |
if not detections:
|
| 157 |
+
return text, "No sensitive information detected.", "**0** PII entities found"
|
| 158 |
|
| 159 |
+
report_lines = ["### Detected Entities\n"]
|
|
|
|
| 160 |
|
|
|
|
| 161 |
by_type = {}
|
| 162 |
for det in detections:
|
| 163 |
if det.type not in by_type:
|
|
|
|
| 172 |
report_lines.append(f"- `{masked_value}` (confidence: {item.confidence})")
|
| 173 |
|
| 174 |
report = "\n".join(report_lines)
|
|
|
|
|
|
|
| 175 |
stats = f"**{len(detections)}** PII entities found across **{len(by_type)}** categories"
|
| 176 |
|
| 177 |
return redacted_text, report, stats
|
| 178 |
|
|
|
|
| 179 |
EXAMPLES = [
|
| 180 |
+
["Hi, my name is John Smith and my email is john.smith@company.com. You can reach me at +1-555-123-4567. My SSN is 123-45-6789."],
|
| 181 |
+
["Customer Order #12345\nName: Jane Doe\nEmail: jane.doe@example.org\nPhone: (555) 987-6543\nCredit Card: 4532015112830366"],
|
| 182 |
+
["API Configuration:\napi_key = \"sk-1234567890abcdefghijklmnopqrstuvwxyz\"\nAWS_ACCESS_KEY: AKIAIOSFODNN7EXAMPLE\npassword: \"super_secret_123\""],
|
| 183 |
+
["Merhaba, ben Ahmet Yilmaz. TC Kimlik numaram 12345678901.\nTelefon: 0532 123 45 67\nIBAN: TR330006100519786457841326"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
]
|
| 185 |
|
| 186 |
+
with gr.Blocks(title="TSZ - Thyris Safe Zone Demo") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
|
|
|
| 188 |
gr.Markdown("""
|
| 189 |
+
# TSZ (Thyris Safe Zone) - Interactive Demo
|
| 190 |
|
| 191 |
**TSZ** is an open-source PII detection and guardrails engine that prevents sensitive data
|
| 192 |
+
from leaking to LLMs and third-party APIs.
|
| 193 |
|
| 194 |
+
[GitHub](https://github.com/thyrisAI/safe-zone) | [Website](https://thyris.ai) | Apache 2.0 License
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
""")
|
| 196 |
|
| 197 |
with gr.Row():
|
| 198 |
+
with gr.Column():
|
|
|
|
| 199 |
input_text = gr.Textbox(
|
| 200 |
+
label="Input Text",
|
| 201 |
placeholder="Paste text containing emails, phone numbers, credit cards, API keys, passwords, etc...",
|
| 202 |
+
lines=8
|
|
|
|
| 203 |
)
|
| 204 |
|
| 205 |
+
analyze_btn = gr.Button("Analyze & Redact", variant="primary")
|
| 206 |
|
| 207 |
+
gr.Markdown("### Try These Examples")
|
| 208 |
gr.Examples(
|
| 209 |
examples=EXAMPLES,
|
| 210 |
+
inputs=input_text
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
+
with gr.Column():
|
|
|
|
| 214 |
stats_output = gr.Markdown(label="Statistics")
|
| 215 |
|
| 216 |
redacted_output = gr.Textbox(
|
| 217 |
+
label="Redacted Output (Safe to send to LLMs)",
|
| 218 |
+
lines=8,
|
|
|
|
| 219 |
interactive=False
|
| 220 |
)
|
| 221 |
|
| 222 |
detection_report = gr.Markdown(label="Detection Report")
|
| 223 |
|
|
|
|
| 224 |
gr.Markdown("""
|
| 225 |
---
|
| 226 |
+
### Supported Detection Types
|
| 227 |
|
| 228 |
| Category | Types |
|
| 229 |
|----------|-------|
|
|
|
|
| 233 |
|
| 234 |
---
|
| 235 |
|
| 236 |
+
### Get the Full Version
|
| 237 |
|
| 238 |
This demo uses a simplified detection engine. The full **TSZ** includes:
|
| 239 |
+
- More comprehensive pattern matching with validators
|
| 240 |
+
- Configurable guardrails and policies
|
| 241 |
+
- REST API with hot-reloadable rules
|
| 242 |
+
- High-performance Go implementation with Redis caching
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
```bash
|
| 245 |
git clone https://github.com/thyrisAI/safe-zone.git
|
| 246 |
cd safe-zone
|
| 247 |
docker-compose up -d
|
| 248 |
```
|
| 249 |
|
| 250 |
+
Built by [Thyris.AI](https://thyris.ai)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
""")
|
| 252 |
|
|
|
|
| 253 |
analyze_btn.click(
|
| 254 |
fn=analyze_text,
|
| 255 |
inputs=[input_text],
|
|
|
|
| 262 |
outputs=[redacted_output, detection_report, stats_output]
|
| 263 |
)
|
| 264 |
|
|
|
|
| 265 |
if __name__ == "__main__":
|
| 266 |
demo.launch()
|