import gradio as gr import os import torch import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification def get_hatespeech_score(text): tokenizer = AutoTokenizer.from_pretrained("unhcr/hatespeech-detection") model = AutoModelForSequenceClassification.from_pretrained("unhcr/hatespeech-detection") # Tokenize input text inputs = tokenizer(text, return_tensors='pt') # Perform inference outputs = model(**inputs) # Get predicted label predicted_label_idx = torch.argmax(outputs.logits).item() predicted_label = model.config.id2label[predicted_label_idx] return predicted_label def text_analysis(text): label_1 = get_hatespeech_score(text) html = '''

Text Sentiment Analysis

Overall Sentiment

{}

Adult Content

{}

Hate Speech

{}

Text Summary

{}

'''.format("Alpha", label_1, "Gamma", "Theta") doc = nlp(text) pos_tokens = [] for token in doc: pos_tokens.extend([(token.text, token.pos_), (" ", None)]) return pos_tokens, html demo = gr.Interface( text_analysis, gr.Textbox(placeholder="Enter sentence here..."), ["highlight", "html"], examples=[ ["What a beautiful morning for a walk!"], ["It was the best of times, it was the worst of times."], ], ) demo.launch()