Spaces:
Runtime error
Runtime error
shrirangphadke
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,71 @@
|
|
1 |
import gradio as gr
|
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 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
import spacy
|
5 |
+
from spacy import displacy
|
6 |
+
|
7 |
+
nlp = spacy.load("en_core_web_sm")
|
8 |
+
|
9 |
+
# Load model directly
|
10 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
11 |
+
|
12 |
+
|
13 |
+
def get_hatespeech_score(text):
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained("unhcr/hatespeech-detection")
|
15 |
+
model = AutoModelForSequenceClassification.from_pretrained("unhcr/hatespeech-detection")
|
16 |
+
|
17 |
+
# Tokenize input text
|
18 |
+
inputs = tokenizer(text, return_tensors='pt')
|
19 |
+
|
20 |
+
# Perform inference
|
21 |
+
outputs = model(**inputs)
|
22 |
+
|
23 |
+
# Get predicted label
|
24 |
+
predicted_label_idx = torch.argmax(outputs.logits).item()
|
25 |
+
predicted_label = model.config.id2label[predicted_label_idx]
|
26 |
+
return predicted_label
|
27 |
+
|
28 |
+
def text_analysis(text):
|
29 |
+
label_1 = get_hatespeech_score(text)
|
30 |
+
html = '''<!doctype html>
|
31 |
+
<html>
|
32 |
+
<body>
|
33 |
+
<h1>Text Sentiment Analysis</h1>
|
34 |
+
<div style=background-color:#d9eee1>
|
35 |
+
<h2>Overall Sentiment</h2>
|
36 |
+
<p>{}</p>
|
37 |
+
</div>
|
38 |
+
<div style=background-color:#fff4a3>
|
39 |
+
<h2>Adult Content</h2>
|
40 |
+
<p>{}</p>
|
41 |
+
</div>
|
42 |
+
<div style=background-color:#ffc0c7>
|
43 |
+
<h2>Hate Speech</h2>
|
44 |
+
<p>{}</p>
|
45 |
+
</div>
|
46 |
+
<div style=background-color:#cfb0b1>
|
47 |
+
<h2>Text Summary</h2>
|
48 |
+
<p>{}</p>
|
49 |
+
</div>
|
50 |
+
</body>
|
51 |
+
</html>
|
52 |
+
'''.format("Alpha", label_1, "Gamma", "Theta")
|
53 |
+
|
54 |
+
doc = nlp(text)
|
55 |
+
pos_tokens = []
|
56 |
+
for token in doc:
|
57 |
+
pos_tokens.extend([(token.text, token.pos_), (" ", None)])
|
58 |
+
|
59 |
+
return pos_tokens, html
|
60 |
+
|
61 |
+
demo = gr.Interface(
|
62 |
+
text_analysis,
|
63 |
+
gr.Textbox(placeholder="Enter sentence here..."),
|
64 |
+
["highlight", "html"],
|
65 |
+
examples=[
|
66 |
+
["What a beautiful morning for a walk!"],
|
67 |
+
["It was the best of times, it was the worst of times."],
|
68 |
+
],
|
69 |
)
|
70 |
|
71 |
+
demo.launch()
|