Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
DIR'A toxicity model for a Hugging Face Space (Gradio SDK, ZeroGPU free tier).
|
| 3 |
+
|
| 4 |
+
Only runs the model. It does NOT touch the database (Spaces block the MySQL
|
| 5 |
+
port). The PHP side on Railway calls this Space and saves the result.
|
| 6 |
+
|
| 7 |
+
API (Gradio): api_name = "predict"
|
| 8 |
+
POST /gradio_api/call/predict {"data": ["comment text"]} -> {"event_id": "..."}
|
| 9 |
+
GET /gradio_api/call/predict/<event_id> -> "event: complete\ndata: [ {...result...} ]"
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import spaces # must be imported before torch on ZeroGPU Spaces
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
import re
|
| 16 |
+
|
| 17 |
+
import gradio as gr
|
| 18 |
+
import torch
|
| 19 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 20 |
+
|
| 21 |
+
MODEL_PATH = os.getenv("MODEL_PATH", "rathath/dira-toxicity-model")
|
| 22 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # only needed if the model repo is private
|
| 23 |
+
API_KEY = os.getenv("API_KEY") # optional shared secret with the Railway site
|
| 24 |
+
THRESHOLD = 0.5
|
| 25 |
+
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, token=HF_TOKEN)
|
| 27 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH, token=HF_TOKEN)
|
| 28 |
+
model.eval()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def clean_comment(text):
|
| 32 |
+
if not text:
|
| 33 |
+
return ""
|
| 34 |
+
text = re.sub(r"https?://\S+|www\.\S+", "", str(text), flags=re.IGNORECASE)
|
| 35 |
+
return re.sub(r"\s+", " ", text).strip()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def is_valid_comment(text):
|
| 39 |
+
if not text or len(text) < 20 or len(text.split()) < 3:
|
| 40 |
+
return False
|
| 41 |
+
return bool(re.search(r"[A-Za-z]", text))
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def analyze_single_comment(text):
|
| 45 |
+
cleaned = clean_comment(text)
|
| 46 |
+
if not is_valid_comment(cleaned):
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
inputs = tokenizer([cleaned], return_tensors="pt", truncation=True,
|
| 50 |
+
padding=True, max_length=256)
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
|
| 54 |
+
preds = (torch.sigmoid(outputs.logits).numpy() >= THRESHOLD).astype(int)[0]
|
| 55 |
+
|
| 56 |
+
# LABEL_0 = is_toxic (not used), 1..5 = categories
|
| 57 |
+
return {
|
| 58 |
+
"threat": int(preds[1]),
|
| 59 |
+
"bullying": int(preds[2]),
|
| 60 |
+
"sexual_harassment": int(preds[3]),
|
| 61 |
+
"hate_speech": int(preds[4]),
|
| 62 |
+
"other_toxicity": int(preds[5]),
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def predict(comment, api_key="", request: gr.Request = None):
|
| 67 |
+
if API_KEY and api_key != API_KEY:
|
| 68 |
+
return {"success": False, "message": "Unauthorized."}
|
| 69 |
+
|
| 70 |
+
prediction = analyze_single_comment(comment) # runs on CPU, uses no GPU quota
|
| 71 |
+
if prediction is None:
|
| 72 |
+
return {"success": False, "message": "Invalid comment."}
|
| 73 |
+
|
| 74 |
+
return {"success": True, "prediction": prediction}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# ZeroGPU refuses to start unless at least one @spaces.GPU function exists.
|
| 78 |
+
# The model is small enough for CPU, so this is never called.
|
| 79 |
+
@spaces.GPU
|
| 80 |
+
def _gpu_placeholder():
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
demo = gr.Interface(
|
| 85 |
+
fn=predict,
|
| 86 |
+
inputs=[gr.Textbox(label="Comment"), gr.Textbox(label="API key (optional)", type="password")],
|
| 87 |
+
outputs=gr.JSON(label="Result"),
|
| 88 |
+
title="DIR'A Toxicity API",
|
| 89 |
+
api_name="predict",
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
spaces
|
| 2 |
+
torch
|
| 3 |
+
transformers==5.6.2
|
| 4 |
+
numpy
|