File size: 1,951 Bytes
76c6334
1e1d870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4066f8e
de1b425
1e1d870
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 = '''<!doctype html>
            <html>
            <body>
            <h1>Text Sentiment Analysis</h1>
            <div style=background-color:#d9eee1>
            <h2>Overall Sentiment</h2>
            <p>{}</p>
            </div>
            <div style=background-color:#fff4a3>
            <h2>Adult Content</h2>
            <p>{}</p>
            </div>
            <div style=background-color:#ffc0c7>
            <h2>Hate Speech</h2>
            <p>{}</p>
            </div>
            <div style=background-color:#cfb0b1>
            <h2>Text Summary</h2>
            <p>{}</p>
            </div>
            </body>
            </html>
            '''.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()