import gradio as gr from transformers import pipeline pipe = pipeline( task="text-classification", model="TostAI/nsfw-text-detection-large", return_all_scores=True ) def predict(input_txt): predictions = pipe(input_txt)[0] # Get first (and only) result label_map = { 'LABEL_0': 'SAFE', 'LABEL_1': 'QUESTIONABLE', 'LABEL_2': 'UNSAFE' } # Convert list of dicts to a single dict result = {label_map[pred['label']]: pred['score'] for pred in predictions} return result gradio_app = gr.Interface( predict, inputs=gr.Textbox(label="Input text"), outputs=gr.Label(label="Result"), title="NSFW Prediction", ) if __name__ == "__main__": gradio_app.launch()