Spaces:
Build error
Build error
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the models | |
| MODEL_PATHS = { | |
| "Toxic Bert-based model": "unitary/toxic-bert", | |
| "Martin-HA-toxic-comment-model": "martin-ha/toxic-comment-model" | |
| } | |
| classifiers = {name: pipeline("text-classification", model=path, tokenizer=path) for name, path in MODEL_PATHS.items()} | |
| def predict_toxicity(text, model_choice): | |
| # Get predictions | |
| classifier = classifiers[model_choice] | |
| predictions = classifier(text, return_all_scores=True)[0] | |
| # Format results | |
| results = {} | |
| for pred in predictions: | |
| results[pred['label']] = f"{pred['score']:.4f}" | |
| return results | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_toxicity, | |
| inputs=[ | |
| gr.Textbox(lines=5, label="Enter text to analyze"), | |
| gr.Radio(choices=list(MODEL_PATHS.keys()), label="Choose a model", value="Toxic Bert-based model") | |
| ], | |
| outputs=gr.Label(num_top_classes=6, label="Toxicity Scores"), | |
| title="Toxicity Prediction", | |
| description="This POC uses trained & pre-trained models to predict toxicity in text. Choose between two models: 'Toxic Bert-based model' for multi-class labeling and 'Martin-HA-toxic-comment-model' for binary clasification.", | |
| examples=[ | |
| ["Great game everyone!", "Toxic Bert-based model"], | |
| ["You're such a noob, uninstall please.", "Martin-HA-toxic-comment-model"], | |
| ["I hope you die in real life, loser.", "Toxic Bert-based model"], | |
| ["Nice move! How did you do that?", "Martin-HA-toxic-comment-model"], | |
| ["Go back to the kitchen where you belong.", "Toxic Bert-based model"], | |
| ] | |
| ) | |
| # Launch the app | |
| iface.launch() |