Init
Browse files- app.py +85 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def predict(text: str) -> Dict[str, Any]:
|
| 9 |
+
"""Classify text for PII detection."""
|
| 10 |
+
if not text or text.strip() == "":
|
| 11 |
+
return {"No input provided": 0.0}
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
# Tokenize input
|
| 15 |
+
inputs = tokenizer(
|
| 16 |
+
text,
|
| 17 |
+
return_tensors="pt",
|
| 18 |
+
padding="max_length",
|
| 19 |
+
max_length=512,
|
| 20 |
+
truncation=True
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Run inference
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
logits = outputs.logits
|
| 27 |
+
probabilities = torch.sigmoid(logits)
|
| 28 |
+
probs = probabilities.squeeze().tolist()
|
| 29 |
+
|
| 30 |
+
# Create results dictionary
|
| 31 |
+
results = {
|
| 32 |
+
"Asking for PII": float(probs[0]),
|
| 33 |
+
"Giving PII": float(probs[1])
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
return results
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return {"Error": str(e)}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Example test cases
|
| 43 |
+
examples = [
|
| 44 |
+
["Do you have the blue app?"],
|
| 45 |
+
["I live at 901 Roosevelt St, Redwood City"],
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
# Model configuration
|
| 51 |
+
model_id = "Roblox/PII-OSS-Private-Not-Public"
|
| 52 |
+
|
| 53 |
+
# Load model and tokenizer
|
| 54 |
+
# When deployed as a Hugging Face Space in the same organization,
|
| 55 |
+
# authentication is handled automatically
|
| 56 |
+
print(f"Loading model: {model_id}")
|
| 57 |
+
try:
|
| 58 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 59 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 60 |
+
model.eval()
|
| 61 |
+
print("Model loaded successfully!")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Failed to load model: {e}")
|
| 64 |
+
print("If running locally, you may need to login with: huggingface-cli login")
|
| 65 |
+
exit(1)
|
| 66 |
+
|
| 67 |
+
# Create Gradio interface
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=gr.Textbox(
|
| 71 |
+
lines=3,
|
| 72 |
+
placeholder="Enter text to analyze for PII content...",
|
| 73 |
+
label="Input Text"
|
| 74 |
+
),
|
| 75 |
+
outputs=gr.Label(
|
| 76 |
+
num_top_classes=2,
|
| 77 |
+
label="Classification Results"
|
| 78 |
+
),
|
| 79 |
+
title="PII Detection Demo",
|
| 80 |
+
description="This model detects whether text is asking for or giving personal information (PII).",
|
| 81 |
+
examples=examples,
|
| 82 |
+
flagging_mode="never",
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.35.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
huggingface-hub>=0.19.0
|