shrirangphadke commited on
Commit
1e1d870
·
verified ·
1 Parent(s): 07b1448

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -23
app.py CHANGED
@@ -1,26 +1,71 @@
1
  import gradio as gr
2
- from textblob import TextBlob
3
-
4
- def analyze_sentiment(text):
5
- blob = TextBlob(text)
6
- sentiment = blob.sentiment.polarity
7
- if sentiment > 0:
8
- color = "green"
9
- elif sentiment < 0:
10
- color = "red"
11
- else:
12
- color = "yellow"
13
- return color
14
-
15
- def sentiment_analysis(text):
16
- color = analyze_sentiment(text)
17
- # return "<h2 style='color:{}; text-align:center;'>{} Sentiment</h2>".format(color, label)
18
- return "<h2><svg fill="#ff0000" width="213px" height="213px" viewBox="0 0 64 64" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <rect id="Icons" x="-640" y="-64" width="1280" height="800" style="fill:none;"></rect> <g id="Icons1" serif:id="Icons"> <g id="Strike"> </g> <g id="H1"> </g> <g id="H2"> </g> <g id="H3"> </g> <g id="list-ul"> </g> <g id="hamburger-1"> </g> <g id="hamburger-2"> </g> <g id="list-ol"> </g> <g id="list-task"> </g> <g id="trash"> </g> <g id="vertical-menu"> </g> <g id="horizontal-menu"> </g> <g id="sidebar-2"> </g> <g id="Pen"> </g> <g id="Pen1" serif:id="Pen"> </g> <g id="clock"> </g> <g id="external-link"> </g> <g id="hr"> </g> <g id="info"> </g> <g id="warning"> <path d="M32.427,7.987c2.183,0.124 4,1.165 5.096,3.281l17.936,36.208c1.739,3.66 -0.954,8.585 -5.373,8.656l-36.119,0c-4.022,-0.064 -7.322,-4.631 -5.352,-8.696l18.271,-36.207c0.342,-0.65 0.498,-0.838 0.793,-1.179c1.186,-1.375 2.483,-2.111 4.748,-2.063Zm-0.295,3.997c-0.687,0.034 -1.316,0.419 -1.659,1.017c-6.312,11.979 -12.397,24.081 -18.301,36.267c-0.546,1.225 0.391,2.797 1.762,2.863c12.06,0.195 24.125,0.195 36.185,0c1.325,-0.064 2.321,-1.584 1.769,-2.85c-5.793,-12.184 -11.765,-24.286 -17.966,-36.267c-0.366,-0.651 -0.903,-1.042 -1.79,-1.03Z" style="fill-rule:nonzero;"></path> <path d="M33.631,40.581l-3.348,0l-0.368,-16.449l4.1,0l-0.384,16.449Zm-3.828,5.03c0,-0.609 0.197,-1.113 0.592,-1.514c0.396,-0.4 0.935,-0.601 1.618,-0.601c0.684,0 1.223,0.201 1.618,0.601c0.395,0.401 0.593,0.905 0.593,1.514c0,0.587 -0.193,1.078 -0.577,1.473c-0.385,0.395 -0.929,0.593 -1.634,0.593c-0.705,0 -1.249,-0.198 -1.634,-0.593c-0.384,-0.395 -0.576,-0.886 -0.576,-1.473Z" style="fill-rule:nonzero;"></path> </g> <g id="plus-circle"> </g> <g id="minus-circle"> </g> <g id="vue"> </g> <g id="cog"> </g> <g id="logo"> </g> <g id="radio-check"> </g> <g id="eye-slash"> </g> <g id="eye"> </g> <g id="toggle-off"> </g> <g id="shredder"> </g> <g id="spinner--loading--dots-" serif:id="spinner [loading, dots]"> </g> <g id="react"> </g> <g id="check-selected"> </g> <g id="turn-off"> </g> <g id="code-block"> </g> <g id="user"> </g> <g id="coffee-bean"> </g> <g id="coffee-beans"> <g id="coffee-bean1" serif:id="coffee-bean"> </g> </g> <g id="coffee-bean-filled"> </g> <g id="coffee-beans-filled"> <g id="coffee-bean2" serif:id="coffee-bean"> </g> </g> <g id="clipboard"> </g> <g id="clipboard-paste"> </g> <g id="clipboard-copy"> </g> <g id="Layer1"> </g> </g> </g></svg></h2>"
19
-
20
- iface = gr.Interface(
21
- fn=sentiment_analysis,
22
- inputs="text",
23
- outputs="html"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
 
26
- iface.launch()
 
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()