Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,305 +1,77 @@
|
|
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 |
-
"[{'label': 'NEUTRAL', 'score': 0.5040543079376221}]"
|
47 |
-
]
|
48 |
-
},
|
49 |
-
"execution_count": 28,
|
50 |
-
"metadata": {},
|
51 |
-
"output_type": "execute_result"
|
52 |
}
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
"
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
"prediction_data.pop(\"Liked\")\n",
|
80 |
-
"\n",
|
81 |
-
"data = list(prediction_data[\"Review\"])\n",
|
82 |
-
"results = pipe(data)\n"
|
83 |
-
]
|
84 |
-
},
|
85 |
-
{
|
86 |
-
"cell_type": "code",
|
87 |
-
"execution_count": 30,
|
88 |
-
"metadata": {},
|
89 |
-
"outputs": [
|
90 |
-
{
|
91 |
-
"data": {
|
92 |
-
"text/plain": [
|
93 |
-
"['Wow... Loved this place.',\n",
|
94 |
-
" 'Crust is not good.',\n",
|
95 |
-
" 'Not tasty and the texture was just nasty.',\n",
|
96 |
-
" 'Stopped by during the late May bank holiday off Rick Steve recommendation and loved it.',\n",
|
97 |
-
" 'The selection on the menu was great and so were the prices.']"
|
98 |
-
]
|
99 |
-
},
|
100 |
-
"execution_count": 30,
|
101 |
-
"metadata": {},
|
102 |
-
"output_type": "execute_result"
|
103 |
-
}
|
104 |
-
],
|
105 |
-
"source": [
|
106 |
-
"data[:5]"
|
107 |
-
]
|
108 |
-
},
|
109 |
-
{
|
110 |
-
"cell_type": "code",
|
111 |
-
"execution_count": 31,
|
112 |
-
"metadata": {},
|
113 |
-
"outputs": [
|
114 |
-
{
|
115 |
-
"data": {
|
116 |
-
"text/html": [
|
117 |
-
"<div>\n",
|
118 |
-
"<style scoped>\n",
|
119 |
-
" .dataframe tbody tr th:only-of-type {\n",
|
120 |
-
" vertical-align: middle;\n",
|
121 |
-
" }\n",
|
122 |
-
"\n",
|
123 |
-
" .dataframe tbody tr th {\n",
|
124 |
-
" vertical-align: top;\n",
|
125 |
-
" }\n",
|
126 |
-
"\n",
|
127 |
-
" .dataframe thead th {\n",
|
128 |
-
" text-align: right;\n",
|
129 |
-
" }\n",
|
130 |
-
"</style>\n",
|
131 |
-
"<table border=\"1\" class=\"dataframe\">\n",
|
132 |
-
" <thead>\n",
|
133 |
-
" <tr style=\"text-align: right;\">\n",
|
134 |
-
" <th></th>\n",
|
135 |
-
" <th>count</th>\n",
|
136 |
-
" <th>sentiment</th>\n",
|
137 |
-
" </tr>\n",
|
138 |
-
" </thead>\n",
|
139 |
-
" <tbody>\n",
|
140 |
-
" <tr>\n",
|
141 |
-
" <th>0</th>\n",
|
142 |
-
" <td>283</td>\n",
|
143 |
-
" <td>Positive</td>\n",
|
144 |
-
" </tr>\n",
|
145 |
-
" <tr>\n",
|
146 |
-
" <th>1</th>\n",
|
147 |
-
" <td>419</td>\n",
|
148 |
-
" <td>Negative</td>\n",
|
149 |
-
" </tr>\n",
|
150 |
-
" <tr>\n",
|
151 |
-
" <th>2</th>\n",
|
152 |
-
" <td>298</td>\n",
|
153 |
-
" <td>Neutral</td>\n",
|
154 |
-
" </tr>\n",
|
155 |
-
" </tbody>\n",
|
156 |
-
"</table>\n",
|
157 |
-
"</div>"
|
158 |
-
],
|
159 |
-
"text/plain": [
|
160 |
-
" count sentiment\n",
|
161 |
-
"0 283 Positive\n",
|
162 |
-
"1 419 Negative\n",
|
163 |
-
"2 298 Neutral"
|
164 |
-
]
|
165 |
-
},
|
166 |
-
"execution_count": 31,
|
167 |
-
"metadata": {},
|
168 |
-
"output_type": "execute_result"
|
169 |
-
}
|
170 |
-
],
|
171 |
-
"source": [
|
172 |
-
"positive_counter = 0 \n",
|
173 |
-
"negative_counter = 0\n",
|
174 |
-
"neutral_counter = 0\n",
|
175 |
-
"for x in results:\n",
|
176 |
-
" if x['label'] == 'POSITIVE':\n",
|
177 |
-
" positive_counter = positive_counter + 1\n",
|
178 |
-
" elif x['label'] == 'NEGATIVE':\n",
|
179 |
-
" negative_counter = negative_counter + 1\n",
|
180 |
-
" else:\n",
|
181 |
-
" neutral_counter = neutral_counter + 1\n",
|
182 |
-
"\n",
|
183 |
-
"result_data = pd.DataFrame({\n",
|
184 |
-
" 'count': [positive_counter, negative_counter, neutral_counter],\n",
|
185 |
-
" 'sentiment': ['Positive', 'Negative', 'Neutral']\n",
|
186 |
-
"})\n",
|
187 |
-
"\n",
|
188 |
-
"result_data"
|
189 |
-
]
|
190 |
-
},
|
191 |
-
{
|
192 |
-
"cell_type": "code",
|
193 |
-
"execution_count": 32,
|
194 |
-
"metadata": {},
|
195 |
-
"outputs": [
|
196 |
-
{
|
197 |
-
"name": "stdout",
|
198 |
-
"output_type": "stream",
|
199 |
-
"text": [
|
200 |
-
"Running on local URL: http://127.0.0.1:7866\n",
|
201 |
-
"Running on public URL: https://af39480688d32c192a.gradio.live\n",
|
202 |
-
"\n",
|
203 |
-
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
|
204 |
-
]
|
205 |
-
},
|
206 |
-
{
|
207 |
-
"data": {
|
208 |
-
"text/html": [
|
209 |
-
"<div><iframe src=\"https://af39480688d32c192a.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
210 |
-
],
|
211 |
-
"text/plain": [
|
212 |
-
"<IPython.core.display.HTML object>"
|
213 |
-
]
|
214 |
-
},
|
215 |
-
"metadata": {},
|
216 |
-
"output_type": "display_data"
|
217 |
-
},
|
218 |
-
{
|
219 |
-
"data": {
|
220 |
-
"text/plain": []
|
221 |
-
},
|
222 |
-
"execution_count": 32,
|
223 |
-
"metadata": {},
|
224 |
-
"output_type": "execute_result"
|
225 |
-
}
|
226 |
-
],
|
227 |
-
"source": [
|
228 |
-
"single_input_demo = gr.Interface.from_pipeline(pipe, title=\"Sentiment Analysis\")\n",
|
229 |
-
"single_input_demo.launch(share=True)"
|
230 |
-
]
|
231 |
-
},
|
232 |
-
{
|
233 |
-
"cell_type": "code",
|
234 |
-
"execution_count": 33,
|
235 |
-
"metadata": {},
|
236 |
-
"outputs": [
|
237 |
-
{
|
238 |
-
"name": "stdout",
|
239 |
-
"output_type": "stream",
|
240 |
-
"text": [
|
241 |
-
"Running on local URL: http://127.0.0.1:7867\n",
|
242 |
-
"Running on public URL: https://4ce90a546387218f07.gradio.live\n",
|
243 |
-
"\n",
|
244 |
-
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
|
245 |
-
]
|
246 |
-
},
|
247 |
-
{
|
248 |
-
"data": {
|
249 |
-
"text/html": [
|
250 |
-
"<div><iframe src=\"https://4ce90a546387218f07.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
251 |
-
],
|
252 |
-
"text/plain": [
|
253 |
-
"<IPython.core.display.HTML object>"
|
254 |
-
]
|
255 |
-
},
|
256 |
-
"metadata": {},
|
257 |
-
"output_type": "display_data"
|
258 |
-
},
|
259 |
-
{
|
260 |
-
"data": {
|
261 |
-
"text/plain": []
|
262 |
-
},
|
263 |
-
"execution_count": 33,
|
264 |
-
"metadata": {},
|
265 |
-
"output_type": "execute_result"
|
266 |
-
}
|
267 |
-
],
|
268 |
-
"source": [
|
269 |
-
"import plotly.express as plt\n",
|
270 |
-
"\n",
|
271 |
-
"def plotly_plot(): \n",
|
272 |
-
" p = plt.bar(result_data, x='sentiment', y='count', title='Restaurent Review Analysis', color=\"count\")\n",
|
273 |
-
" return p\n",
|
274 |
-
"\n",
|
275 |
-
"# show the results\n",
|
276 |
-
"outputs = gr.Plot()\n",
|
277 |
-
"\n",
|
278 |
-
"demo = gr.Interface(fn=plotly_plot, inputs=None, outputs=outputs, title=\"Restaurant Customer Review Sentiment Analysis\")\n",
|
279 |
-
"\n",
|
280 |
-
"demo.launch(share=True)"
|
281 |
-
]
|
282 |
-
}
|
283 |
-
],
|
284 |
-
"metadata": {
|
285 |
-
"kernelspec": {
|
286 |
-
"display_name": "Python 3",
|
287 |
-
"language": "python",
|
288 |
-
"name": "python3"
|
289 |
-
},
|
290 |
-
"language_info": {
|
291 |
-
"codemirror_mode": {
|
292 |
-
"name": "ipython",
|
293 |
-
"version": 3
|
294 |
-
},
|
295 |
-
"file_extension": ".py",
|
296 |
-
"mimetype": "text/x-python",
|
297 |
-
"name": "python",
|
298 |
-
"nbconvert_exporter": "python",
|
299 |
-
"pygments_lexer": "ipython3",
|
300 |
-
"version": "3.10.6"
|
301 |
-
}
|
302 |
-
},
|
303 |
-
"nbformat": 4,
|
304 |
-
"nbformat_minor": 2
|
305 |
-
}
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
3 |
+
import opendatasets as od
|
4 |
+
import gradio as gr
|
5 |
+
import pandas as pd
|
6 |
+
import plotly.express as plt
|
7 |
+
|
8 |
+
tf.get_logger().setLevel("ERROR")
|
9 |
+
|
10 |
+
# create tokenizer from pre-trained model
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
12 |
+
"blanchefort/rubert-base-cased-sentiment-rurewiews"
|
13 |
+
)
|
14 |
+
# Load the model
|
15 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
16 |
+
"blanchefort/rubert-base-cased-sentiment-rurewiews"
|
17 |
+
)
|
18 |
+
# Create a pipeline for the model
|
19 |
+
pipe = pipeline(
|
20 |
+
"text-classification", model="blanchefort/rubert-base-cased-sentiment-rurewiews"
|
21 |
+
)
|
22 |
+
|
23 |
+
# load review from open dataset
|
24 |
+
od.download_kaggle_dataset("vigneshwarsofficial/reviews", data_dir="restaurent_review")
|
25 |
+
prediction_data = pd.read_csv(
|
26 |
+
"restaurent_review/reviews/Restaurant_Reviews.tsv", delimiter="\t"
|
27 |
+
)
|
28 |
+
# popping irrelevant coloumn
|
29 |
+
prediction_data.pop("Liked")
|
30 |
+
# making a list
|
31 |
+
data = list(prediction_data["Review"])
|
32 |
+
# making prediction using pipe
|
33 |
+
results = pipe(data)
|
34 |
+
|
35 |
+
# Categorizing result
|
36 |
+
positive_counter = 0
|
37 |
+
negative_counter = 0
|
38 |
+
neutral_counter = 0
|
39 |
+
for x in results:
|
40 |
+
if x["label"] == "POSITIVE":
|
41 |
+
positive_counter = positive_counter + 1
|
42 |
+
elif x["label"] == "NEGATIVE":
|
43 |
+
negative_counter = negative_counter + 1
|
44 |
+
else:
|
45 |
+
neutral_counter = neutral_counter + 1
|
46 |
+
|
47 |
+
result_data = pd.DataFrame(
|
48 |
{
|
49 |
+
"count": [positive_counter, negative_counter, neutral_counter],
|
50 |
+
"sentiment": ["Positive", "Negative", "Neutral"],
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
}
|
52 |
+
)
|
53 |
+
|
54 |
+
|
55 |
+
# create bar chart interface on gradio
|
56 |
+
def plotly_plot():
|
57 |
+
p = plt.bar(
|
58 |
+
result_data,
|
59 |
+
x="sentiment",
|
60 |
+
y="count",
|
61 |
+
title="Restaurent Review Analysis",
|
62 |
+
color="count",
|
63 |
+
)
|
64 |
+
return p
|
65 |
+
|
66 |
+
|
67 |
+
# show the results
|
68 |
+
outputs = gr.Plot()
|
69 |
+
|
70 |
+
demo = gr.Interface(
|
71 |
+
fn=plotly_plot,
|
72 |
+
inputs=None,
|
73 |
+
outputs=outputs,
|
74 |
+
title="Restaurant Customer Review Sentiment Analysis",
|
75 |
+
)
|
76 |
+
|
77 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|