Spaces:
Runtime error
Runtime error
GirishKiran
commited on
Commit
·
242bf4e
1
Parent(s):
42e6b3a
Upload 2 files
Browse files- Bar chart colour change
- Comparison report
app.py
CHANGED
@@ -11,7 +11,7 @@ import cryptography
|
|
11 |
import cryptography.fernet
|
12 |
|
13 |
class Utility(object):
|
14 |
-
|
15 |
def __init__(self, name="Utility") -> None:
|
16 |
self.name = name
|
17 |
self.author = "Duc Haba, Girish"
|
@@ -20,7 +20,7 @@ class Utility(object):
|
|
20 |
|
21 |
#Define encrypted keys
|
22 |
self._huggingface_key="gAAAAABkgtmOIjpnjwXFWmgh1j2et2kMjHUze-ym6h3BieAp34Sqkqv3EVYvRinETvpw-kXu7RSRl5_9FqrYe-7unfakMvMkU8nHrfB3hBSC76ZTXwkVSzlN0RfBNs9NL8BGjaSJ8mz8"
|
23 |
-
|
24 |
#Key for crypto
|
25 |
self._fkey="=cvsOPRcWD6JONmdr4Sh6-PqF6nT1InYh965mI8f_sef"
|
26 |
return
|
@@ -44,14 +44,14 @@ class Utility(object):
|
|
44 |
print(x)
|
45 |
else:
|
46 |
y = x
|
47 |
-
return y
|
48 |
|
49 |
# Hugging face : Login to Hugging face
|
50 |
def _login_hface(self):
|
51 |
-
huggingface_hub.login(self._decrypt_it(self._huggingface_key),
|
52 |
add_to_git_credential=True) # non-blocking login
|
53 |
self._ph()
|
54 |
-
return
|
55 |
|
56 |
# Hugging face : Push files to Hugging face
|
57 |
def push_hface_files(self,
|
@@ -63,8 +63,8 @@ class Utility(object):
|
|
63 |
for f in hf_names:
|
64 |
lo = local_dir + f
|
65 |
huggingface_hub.upload_file(
|
66 |
-
path_or_fileobj=lo,
|
67 |
-
path_in_repo=f,
|
68 |
repo_id=hf_space,
|
69 |
repo_type=huggingface_hub.REPO_TYPE_SPACE)
|
70 |
except Exception as e:
|
@@ -82,10 +82,10 @@ class Utility(object):
|
|
82 |
|
83 |
# Hugging face : Login to Hugging face
|
84 |
def _login_hface(self):
|
85 |
-
huggingface_hub.login(self._decrypt_it(self._huggingface_key),
|
86 |
add_to_git_credential=True) # non-blocking login
|
87 |
self._ph()
|
88 |
-
return
|
89 |
|
90 |
# System Info : Fetch available CPU and RAM of the system
|
91 |
def fetch_system_info(self):
|
@@ -104,9 +104,9 @@ class Utility(object):
|
|
104 |
s += f"Available memory: {mem_available_gb:.2f} GB\n"
|
105 |
# print(f"Used memory: {mem_used_gb:.2f} GB")
|
106 |
s += f"Memory usage: {mem_used_gb/mem_total_gb:.2f}%\n"
|
107 |
-
return
|
108 |
|
109 |
-
# System Info : Fetch GPU information of the system
|
110 |
def fetch_gpu_info(self):
|
111 |
s=''
|
112 |
try:
|
@@ -126,8 +126,8 @@ class Utility(object):
|
|
126 |
s += f"Hostname: {hostname}\n"
|
127 |
s += f"IP Address: {ip_address}\n"
|
128 |
return s
|
129 |
-
|
130 |
-
# Create and writes data to the file
|
131 |
def write_file(self,fname, txt):
|
132 |
f = open(fname, "w")
|
133 |
f.writelines("\n".join(txt))
|
@@ -140,14 +140,14 @@ class Utility(object):
|
|
140 |
if (is_generate):
|
141 |
s=open(self._xkeyfile, "rb").read()
|
142 |
return s
|
143 |
-
|
144 |
# Crypto : Decrypt value
|
145 |
def _decrypt_it(self, x):
|
146 |
y = self._fetch_crypt()
|
147 |
f = cryptography.fernet.Fernet(y)
|
148 |
m = f.decrypt(x)
|
149 |
return m.decode()
|
150 |
-
|
151 |
# Crypto : Encrypt value
|
152 |
def _encrypt_it(self, x):
|
153 |
key = self._fetch_crypt()
|
@@ -156,27 +156,27 @@ class Utility(object):
|
|
156 |
y = f.encrypt(p)
|
157 |
return y
|
158 |
|
159 |
-
# Add method to class
|
160 |
def add_method(cls):
|
161 |
def decorator(func):
|
162 |
-
@functools.wraps(func)
|
163 |
-
def wrapper(*args, **kwargs):
|
164 |
return func(*args, **kwargs)
|
165 |
setattr(cls, func.__name__, wrapper)
|
166 |
return func # returning func means func can still be used normally
|
167 |
-
return decorator
|
168 |
-
|
169 |
|
170 |
""" This file contains multiple Python classes and responssible to provide Emotions based on the given user input
|
171 |
Currently it supports emotions like Anger, Joy, Optimism and Sadness"""
|
172 |
|
173 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
174 |
import scipy
|
175 |
import scipy.special
|
176 |
import pandas
|
177 |
|
178 |
class SentimentAnalyser(object):
|
179 |
-
|
180 |
global utility
|
181 |
|
182 |
# initialize the object
|
@@ -206,7 +206,7 @@ class SentimentAnalyser(object):
|
|
206 |
modelLink = "bhadresh-savani/distilbert-base-uncased-emotion"
|
207 |
self.tokenizer = AutoTokenizer.from_pretrained(modelLink)
|
208 |
self.model = AutoModelForSequenceClassification.from_pretrained(modelLink)
|
209 |
-
return
|
210 |
|
211 |
sentiment = SentimentAnalyser(name="EmotionAnalyser")
|
212 |
|
@@ -226,7 +226,8 @@ def _predict_sentiment(p):
|
|
226 |
|
227 |
@add_method(SentimentAnalyser)
|
228 |
def draw_bar_plot(df_data, title='Sentiment Analysis', xlabel='p string', ylabel='Emotion Score'):
|
229 |
-
|
|
|
230 |
title=title,
|
231 |
ylabel=ylabel,
|
232 |
xlabel=xlabel,
|
@@ -236,28 +237,29 @@ def draw_bar_plot(df_data, title='Sentiment Analysis', xlabel='p string', ylabel
|
|
236 |
@add_method(SentimentAnalyser)
|
237 |
def predict_sentiment(p):
|
238 |
df_out = _predict_sentiment(p)
|
239 |
-
print("sort : ", df_out.sort_values(['Love', 'Joy', 'Surprise', 'Fear', 'Sadness', 'Anger']))
|
240 |
max_column = df_out.loc[0].idxmax()
|
241 |
max_value = df_out.loc[0].max()
|
242 |
title = f'Sentiment Analysis: {max_column}: {round(max_value*100,1)}%'
|
243 |
xlabel= f'Input: {p}'
|
244 |
pic = draw_bar_plot(df_out, title=title, xlabel=xlabel)
|
245 |
-
return pic.get_figure(), df_out.to_json()
|
246 |
|
247 |
import gradio
|
248 |
in_box = [gradio.Textbox(lines=1, label="Input", placeholder="type text here")]
|
249 |
out_box = [gradio.Plot(label="Sentiment Score:"),
|
250 |
gradio.Textbox(lines=4, label="Raw JSON Response:")]
|
251 |
title = "Sentiment Analysis: Understanding the Emotional Tone of Text"
|
252 |
-
desc = "Sentiment analysis is a powerful tool that can be used to gain insights into how people feel about the world around them."
|
253 |
exp = [
|
254 |
['I am feeling very bad today.'],
|
255 |
['I hate to swim early morning.']
|
256 |
-
]
|
|
|
257 |
|
258 |
gradio.Interface(fn=predict_sentiment,
|
259 |
inputs=in_box,
|
260 |
outputs=out_box,
|
261 |
title=title,
|
262 |
description=desc,
|
263 |
-
examples=exp
|
|
|
|
11 |
import cryptography.fernet
|
12 |
|
13 |
class Utility(object):
|
14 |
+
|
15 |
def __init__(self, name="Utility") -> None:
|
16 |
self.name = name
|
17 |
self.author = "Duc Haba, Girish"
|
|
|
20 |
|
21 |
#Define encrypted keys
|
22 |
self._huggingface_key="gAAAAABkgtmOIjpnjwXFWmgh1j2et2kMjHUze-ym6h3BieAp34Sqkqv3EVYvRinETvpw-kXu7RSRl5_9FqrYe-7unfakMvMkU8nHrfB3hBSC76ZTXwkVSzlN0RfBNs9NL8BGjaSJ8mz8"
|
23 |
+
|
24 |
#Key for crypto
|
25 |
self._fkey="=cvsOPRcWD6JONmdr4Sh6-PqF6nT1InYh965mI8f_sef"
|
26 |
return
|
|
|
44 |
print(x)
|
45 |
else:
|
46 |
y = x
|
47 |
+
return y
|
48 |
|
49 |
# Hugging face : Login to Hugging face
|
50 |
def _login_hface(self):
|
51 |
+
huggingface_hub.login(self._decrypt_it(self._huggingface_key),
|
52 |
add_to_git_credential=True) # non-blocking login
|
53 |
self._ph()
|
54 |
+
return
|
55 |
|
56 |
# Hugging face : Push files to Hugging face
|
57 |
def push_hface_files(self,
|
|
|
63 |
for f in hf_names:
|
64 |
lo = local_dir + f
|
65 |
huggingface_hub.upload_file(
|
66 |
+
path_or_fileobj=lo,
|
67 |
+
path_in_repo=f,
|
68 |
repo_id=hf_space,
|
69 |
repo_type=huggingface_hub.REPO_TYPE_SPACE)
|
70 |
except Exception as e:
|
|
|
82 |
|
83 |
# Hugging face : Login to Hugging face
|
84 |
def _login_hface(self):
|
85 |
+
huggingface_hub.login(self._decrypt_it(self._huggingface_key),
|
86 |
add_to_git_credential=True) # non-blocking login
|
87 |
self._ph()
|
88 |
+
return
|
89 |
|
90 |
# System Info : Fetch available CPU and RAM of the system
|
91 |
def fetch_system_info(self):
|
|
|
104 |
s += f"Available memory: {mem_available_gb:.2f} GB\n"
|
105 |
# print(f"Used memory: {mem_used_gb:.2f} GB")
|
106 |
s += f"Memory usage: {mem_used_gb/mem_total_gb:.2f}%\n"
|
107 |
+
return
|
108 |
|
109 |
+
# System Info : Fetch GPU information of the system
|
110 |
def fetch_gpu_info(self):
|
111 |
s=''
|
112 |
try:
|
|
|
126 |
s += f"Hostname: {hostname}\n"
|
127 |
s += f"IP Address: {ip_address}\n"
|
128 |
return s
|
129 |
+
|
130 |
+
# Create and writes data to the file
|
131 |
def write_file(self,fname, txt):
|
132 |
f = open(fname, "w")
|
133 |
f.writelines("\n".join(txt))
|
|
|
140 |
if (is_generate):
|
141 |
s=open(self._xkeyfile, "rb").read()
|
142 |
return s
|
143 |
+
|
144 |
# Crypto : Decrypt value
|
145 |
def _decrypt_it(self, x):
|
146 |
y = self._fetch_crypt()
|
147 |
f = cryptography.fernet.Fernet(y)
|
148 |
m = f.decrypt(x)
|
149 |
return m.decode()
|
150 |
+
|
151 |
# Crypto : Encrypt value
|
152 |
def _encrypt_it(self, x):
|
153 |
key = self._fetch_crypt()
|
|
|
156 |
y = f.encrypt(p)
|
157 |
return y
|
158 |
|
159 |
+
# Add method to class
|
160 |
def add_method(cls):
|
161 |
def decorator(func):
|
162 |
+
@functools.wraps(func)
|
163 |
+
def wrapper(*args, **kwargs):
|
164 |
return func(*args, **kwargs)
|
165 |
setattr(cls, func.__name__, wrapper)
|
166 |
return func # returning func means func can still be used normally
|
167 |
+
return decorator
|
|
|
168 |
|
169 |
""" This file contains multiple Python classes and responssible to provide Emotions based on the given user input
|
170 |
Currently it supports emotions like Anger, Joy, Optimism and Sadness"""
|
171 |
|
172 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
173 |
+
from matplotlib.colors import LinearSegmentedColormap
|
174 |
import scipy
|
175 |
import scipy.special
|
176 |
import pandas
|
177 |
|
178 |
class SentimentAnalyser(object):
|
179 |
+
|
180 |
global utility
|
181 |
|
182 |
# initialize the object
|
|
|
206 |
modelLink = "bhadresh-savani/distilbert-base-uncased-emotion"
|
207 |
self.tokenizer = AutoTokenizer.from_pretrained(modelLink)
|
208 |
self.model = AutoModelForSequenceClassification.from_pretrained(modelLink)
|
209 |
+
return
|
210 |
|
211 |
sentiment = SentimentAnalyser(name="EmotionAnalyser")
|
212 |
|
|
|
226 |
|
227 |
@add_method(SentimentAnalyser)
|
228 |
def draw_bar_plot(df_data, title='Sentiment Analysis', xlabel='p string', ylabel='Emotion Score'):
|
229 |
+
graphCmap=LinearSegmentedColormap.from_list('gr',["g", "w", "r"])
|
230 |
+
pic = df_data.plot.bar(cmap=graphCmap,
|
231 |
title=title,
|
232 |
ylabel=ylabel,
|
233 |
xlabel=xlabel,
|
|
|
237 |
@add_method(SentimentAnalyser)
|
238 |
def predict_sentiment(p):
|
239 |
df_out = _predict_sentiment(p)
|
|
|
240 |
max_column = df_out.loc[0].idxmax()
|
241 |
max_value = df_out.loc[0].max()
|
242 |
title = f'Sentiment Analysis: {max_column}: {round(max_value*100,1)}%'
|
243 |
xlabel= f'Input: {p}'
|
244 |
pic = draw_bar_plot(df_out, title=title, xlabel=xlabel)
|
245 |
+
return pic.get_figure(), df_out.to_json()
|
246 |
|
247 |
import gradio
|
248 |
in_box = [gradio.Textbox(lines=1, label="Input", placeholder="type text here")]
|
249 |
out_box = [gradio.Plot(label="Sentiment Score:"),
|
250 |
gradio.Textbox(lines=4, label="Raw JSON Response:")]
|
251 |
title = "Sentiment Analysis: Understanding the Emotional Tone of Text"
|
252 |
+
desc = "Sentiment analysis is a powerful tool that can be used to gain insights into how people feel about the world around them."
|
253 |
exp = [
|
254 |
['I am feeling very bad today.'],
|
255 |
['I hate to swim early morning.']
|
256 |
+
]
|
257 |
+
arti= "We did a comparision of OpenAI vs DestilBert model (which we are currently using in this space) by running 31 sentences in a loop and found DestilBert is 27 times faster than OpenAI."
|
258 |
|
259 |
gradio.Interface(fn=predict_sentiment,
|
260 |
inputs=in_box,
|
261 |
outputs=out_box,
|
262 |
title=title,
|
263 |
description=desc,
|
264 |
+
examples=exp,
|
265 |
+
article=arti).launch(debug=True)
|