Spaces:
Sleeping
Sleeping
Suvajit Majumder commited on
Commit ·
37a02e7
0
Parent(s):
Initial commit
Browse files- app.py +111 -0
- requirements.txt +128 -0
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the toxicity classification pipeline
|
| 5 |
+
print("Loading toxicity classifier pipeline...")
|
| 6 |
+
toxicity_pipeline = pipeline(
|
| 7 |
+
"text-classification",
|
| 8 |
+
model="s-nlp/roberta_toxicity_classifier",
|
| 9 |
+
tokenizer="s-nlp/roberta_toxicity_classifier",
|
| 10 |
+
# return_all_scores=True # This ensures we get scores for both classes
|
| 11 |
+
)
|
| 12 |
+
print("Pipeline loaded successfully!")
|
| 13 |
+
|
| 14 |
+
def toxicity_classification(text: str) -> dict:
|
| 15 |
+
"""
|
| 16 |
+
Classify the toxicity of the given text.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
text (str): The text to analyze
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
dict: A dictionary containing toxicity scores and classification
|
| 23 |
+
"""
|
| 24 |
+
if not text.strip():
|
| 25 |
+
return {
|
| 26 |
+
"error": "Please enter some text to analyze"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Get predictions using the pipeline
|
| 31 |
+
result = toxicity_pipeline(text)[0]
|
| 32 |
+
|
| 33 |
+
# The pipeline returns a list like: [{'label': 'LABEL_0', 'score': 0.8}]
|
| 34 |
+
# LABEL_0 is non-toxic, LABEL_1 is toxic (based on the model description)
|
| 35 |
+
|
| 36 |
+
# Convert to a more readable format
|
| 37 |
+
|
| 38 |
+
label = result['label']
|
| 39 |
+
confidence = result['score']
|
| 40 |
+
|
| 41 |
+
classification = "non-toxic" if label == "LABEL_0" else "toxic"
|
| 42 |
+
|
| 43 |
+
return {
|
| 44 |
+
"classification": classification,
|
| 45 |
+
"confidence": round(confidence, 4)
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return {
|
| 50 |
+
"error": f"Error processing text: {str(e)}"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Create the Gradio interface
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=toxicity_classification,
|
| 56 |
+
inputs=gr.Textbox(
|
| 57 |
+
placeholder="Enter text to analyze for toxicity...",
|
| 58 |
+
lines=3,
|
| 59 |
+
label="Input Text"
|
| 60 |
+
),
|
| 61 |
+
outputs=gr.JSON(label="Toxicity Analysis Results"),
|
| 62 |
+
title="Text Toxicity Classification",
|
| 63 |
+
description="Analyze text toxicity using RoBERTa transformer model (s-nlp/roberta_toxicity_classifier)",
|
| 64 |
+
examples=[
|
| 65 |
+
["You are amazing!"],
|
| 66 |
+
["This is a wonderful day."],
|
| 67 |
+
["I disagree with your opinion."],
|
| 68 |
+
["The weather is nice today."]
|
| 69 |
+
]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch the interface and MCP server
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
# Add debugging section for local testing
|
| 75 |
+
import sys
|
| 76 |
+
|
| 77 |
+
# Check if running in debug mode (pass 'debug' as command line argument)
|
| 78 |
+
if len(sys.argv) > 1 and sys.argv[1] == "debug":
|
| 79 |
+
print("=" * 50)
|
| 80 |
+
print("DEBUG MODE - Testing toxicity classification")
|
| 81 |
+
print("=" * 50)
|
| 82 |
+
|
| 83 |
+
# Test cases for debugging
|
| 84 |
+
test_cases = [
|
| 85 |
+
"You are amazing!",
|
| 86 |
+
"This is a wonderful day.",
|
| 87 |
+
"I hate you so much!",
|
| 88 |
+
"The weather is nice today.",
|
| 89 |
+
"You're such an idiot!",
|
| 90 |
+
"I disagree with your opinion.",
|
| 91 |
+
"" # Empty string test
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
for i, test_text in enumerate(test_cases):
|
| 95 |
+
print(f"\n--- Test Case {i} ---")
|
| 96 |
+
print(f"Input: '{test_text}'")
|
| 97 |
+
|
| 98 |
+
# Set breakpoint here for debugging
|
| 99 |
+
import pdb; pdb.set_trace()
|
| 100 |
+
|
| 101 |
+
# Call the function
|
| 102 |
+
result = toxicity_classification(test_text)
|
| 103 |
+
|
| 104 |
+
print(f"Output: {result}")
|
| 105 |
+
print("-" * 30)
|
| 106 |
+
|
| 107 |
+
print("\nDebug testing completed!")
|
| 108 |
+
|
| 109 |
+
else:
|
| 110 |
+
# Normal Gradio mode
|
| 111 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altgraph @ file:///AppleInternal/Library/BuildRoots/2c89a47b-9dd5-11ef-938f-6e654a286000/Library/Caches/com.apple.xbs/Sources/python3/altgraph-0.17.2-py2.py3-none-any.whl
|
| 2 |
+
anyio==4.9.0
|
| 3 |
+
appnope==0.1.4
|
| 4 |
+
argon2-cffi==23.1.0
|
| 5 |
+
argon2-cffi-bindings==21.2.0
|
| 6 |
+
arrow==1.3.0
|
| 7 |
+
asttokens==3.0.0
|
| 8 |
+
async-lru==2.0.5
|
| 9 |
+
attrs==25.3.0
|
| 10 |
+
babel==2.17.0
|
| 11 |
+
beautifulsoup4==4.13.3
|
| 12 |
+
bleach==6.2.0
|
| 13 |
+
bs4==0.0.2
|
| 14 |
+
certifi==2025.1.31
|
| 15 |
+
cffi==1.17.1
|
| 16 |
+
charset-normalizer==3.4.1
|
| 17 |
+
cloudpickle==3.1.1
|
| 18 |
+
comm==0.2.2
|
| 19 |
+
contourpy==1.3.0
|
| 20 |
+
cycler==0.12.1
|
| 21 |
+
debugpy==1.8.13
|
| 22 |
+
decorator==5.2.1
|
| 23 |
+
defusedxml==0.7.1
|
| 24 |
+
exceptiongroup==1.2.2
|
| 25 |
+
executing==2.2.0
|
| 26 |
+
fastjsonschema==2.21.1
|
| 27 |
+
filelock==3.17.0
|
| 28 |
+
fonttools==4.56.0
|
| 29 |
+
fqdn==1.5.1
|
| 30 |
+
fsspec==2025.2.0
|
| 31 |
+
future @ file:///AppleInternal/Library/BuildRoots/2c89a47b-9dd5-11ef-938f-6e654a286000/Library/Caches/com.apple.xbs/Sources/python3/future-0.18.2-py3-none-any.whl
|
| 32 |
+
gym==0.26.2
|
| 33 |
+
gym-notices==0.0.8
|
| 34 |
+
h11==0.16.0
|
| 35 |
+
httpcore==1.0.9
|
| 36 |
+
httpx==0.28.1
|
| 37 |
+
idna==3.10
|
| 38 |
+
importlib_metadata==8.6.1
|
| 39 |
+
importlib_resources==6.5.2
|
| 40 |
+
ipykernel==6.29.5
|
| 41 |
+
ipython==8.18.1
|
| 42 |
+
isoduration==20.11.0
|
| 43 |
+
jax==0.4.30
|
| 44 |
+
jaxlib==0.4.30
|
| 45 |
+
jedi==0.19.2
|
| 46 |
+
Jinja2==3.1.5
|
| 47 |
+
json5==0.12.0
|
| 48 |
+
jsonpointer==3.0.0
|
| 49 |
+
jsonschema==4.23.0
|
| 50 |
+
jsonschema-specifications==2025.4.1
|
| 51 |
+
jupyter-events==0.12.0
|
| 52 |
+
jupyter-lsp==2.2.5
|
| 53 |
+
jupyter_client==8.6.3
|
| 54 |
+
jupyter_core==5.7.2
|
| 55 |
+
jupyter_server==2.15.0
|
| 56 |
+
jupyter_server_terminals==0.5.3
|
| 57 |
+
jupyterlab==4.4.2
|
| 58 |
+
jupyterlab_pygments==0.3.0
|
| 59 |
+
jupyterlab_server==2.27.3
|
| 60 |
+
kiwisolver==1.4.7
|
| 61 |
+
macholib @ file:///AppleInternal/Library/BuildRoots/2c89a47b-9dd5-11ef-938f-6e654a286000/Library/Caches/com.apple.xbs/Sources/python3/macholib-1.15.2-py2.py3-none-any.whl
|
| 62 |
+
MarkupSafe==3.0.2
|
| 63 |
+
matplotlib==3.9.4
|
| 64 |
+
matplotlib-inline==0.1.7
|
| 65 |
+
mistune==3.1.3
|
| 66 |
+
ml_dtypes==0.5.1
|
| 67 |
+
mpmath==1.3.0
|
| 68 |
+
nbclient==0.10.2
|
| 69 |
+
nbconvert==7.16.6
|
| 70 |
+
nbformat==5.10.4
|
| 71 |
+
nest-asyncio==1.6.0
|
| 72 |
+
networkx==3.2.1
|
| 73 |
+
notebook==7.4.2
|
| 74 |
+
notebook_shim==0.2.4
|
| 75 |
+
numpy==2.0.2
|
| 76 |
+
opt_einsum==3.4.0
|
| 77 |
+
overrides==7.7.0
|
| 78 |
+
packaging==24.2
|
| 79 |
+
pandas==2.2.3
|
| 80 |
+
pandocfilters==1.5.1
|
| 81 |
+
parso==0.8.4
|
| 82 |
+
pexpect==4.9.0
|
| 83 |
+
pillow==11.1.0
|
| 84 |
+
platformdirs==4.3.6
|
| 85 |
+
prometheus_client==0.21.1
|
| 86 |
+
prompt_toolkit==3.0.50
|
| 87 |
+
psutil==7.0.0
|
| 88 |
+
ptyprocess==0.7.0
|
| 89 |
+
pure_eval==0.2.3
|
| 90 |
+
pycparser==2.22
|
| 91 |
+
pygame==2.6.1
|
| 92 |
+
Pygments==2.19.1
|
| 93 |
+
pyhumps==3.8.0
|
| 94 |
+
pyparsing==3.2.1
|
| 95 |
+
PyPFF @ git+https://github.com/pro-football-focus/pypff.git@ca2c4a53a63c20267a900065e9bd0d194db1a077
|
| 96 |
+
python-dateutil==2.9.0.post0
|
| 97 |
+
python-json-logger==3.3.0
|
| 98 |
+
pytz==2025.1
|
| 99 |
+
PyYAML==6.0.2
|
| 100 |
+
pyzmq==26.2.1
|
| 101 |
+
referencing==0.36.2
|
| 102 |
+
requests==2.32.3
|
| 103 |
+
rfc3339-validator==0.1.4
|
| 104 |
+
rfc3986-validator==0.1.1
|
| 105 |
+
rpds-py==0.24.0
|
| 106 |
+
scipy==1.13.1
|
| 107 |
+
Send2Trash==1.8.3
|
| 108 |
+
six @ file:///AppleInternal/Library/BuildRoots/2c89a47b-9dd5-11ef-938f-6e654a286000/Library/Caches/com.apple.xbs/Sources/python3/six-1.15.0-py2.py3-none-any.whl
|
| 109 |
+
sniffio==1.3.1
|
| 110 |
+
soupsieve==2.6
|
| 111 |
+
stack-data==0.6.3
|
| 112 |
+
sympy==1.13.1
|
| 113 |
+
terminado==0.18.1
|
| 114 |
+
tinycss2==1.4.0
|
| 115 |
+
tomli==2.2.1
|
| 116 |
+
torch==2.6.0
|
| 117 |
+
tornado==6.4.2
|
| 118 |
+
traitlets==5.14.3
|
| 119 |
+
types-python-dateutil==2.9.0.20241206
|
| 120 |
+
typing_extensions==4.12.2
|
| 121 |
+
tzdata==2025.1
|
| 122 |
+
uri-template==1.3.0
|
| 123 |
+
urllib3==2.3.0
|
| 124 |
+
wcwidth==0.2.13
|
| 125 |
+
webcolors==24.11.1
|
| 126 |
+
webencodings==0.5.1
|
| 127 |
+
websocket-client==1.8.0
|
| 128 |
+
zipp==3.21.0
|