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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stderr",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"f:\\miniconda3\\envs\\btl\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
13 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
14 |
+
]
|
15 |
+
}
|
16 |
+
],
|
17 |
+
"source": [
|
18 |
+
"import matplotlib.pyplot as plt\n",
|
19 |
+
"import gradio as gd\n",
|
20 |
+
"import numpy as np"
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": 3,
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [
|
28 |
+
{
|
29 |
+
"name": "stdout",
|
30 |
+
"output_type": "stream",
|
31 |
+
"text": [
|
32 |
+
"Running on local URL: http://127.0.0.1:7860\n",
|
33 |
+
"\n",
|
34 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"data": {
|
39 |
+
"text/html": [
|
40 |
+
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
41 |
+
],
|
42 |
+
"text/plain": [
|
43 |
+
"<IPython.core.display.HTML object>"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
"metadata": {},
|
47 |
+
"output_type": "display_data"
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"data": {
|
51 |
+
"text/plain": []
|
52 |
+
},
|
53 |
+
"execution_count": 3,
|
54 |
+
"metadata": {},
|
55 |
+
"output_type": "execute_result"
|
56 |
+
}
|
57 |
+
],
|
58 |
+
"source": [
|
59 |
+
"import gradio as gr\n",
|
60 |
+
"\n",
|
61 |
+
"def greet(name, intensity):\n",
|
62 |
+
" return \"Hello, \" + name + \"!\" * int(intensity)\n",
|
63 |
+
"\n",
|
64 |
+
"demo = gr.Interface(\n",
|
65 |
+
" fn=greet,\n",
|
66 |
+
" inputs=[\"text\", \"slider\"],\n",
|
67 |
+
" outputs=[\"text\"],\n",
|
68 |
+
")\n",
|
69 |
+
"\n",
|
70 |
+
"demo.launch()\n"
|
71 |
+
]
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"cell_type": "code",
|
75 |
+
"execution_count": 51,
|
76 |
+
"metadata": {},
|
77 |
+
"outputs": [
|
78 |
+
{
|
79 |
+
"name": "stdout",
|
80 |
+
"output_type": "stream",
|
81 |
+
"text": [
|
82 |
+
"Running on local URL: http://127.0.0.1:7904\n",
|
83 |
+
"\n",
|
84 |
+
"Could not create share link. Missing file: f:\\miniconda3\\envs\\btl\\Lib\\site-packages\\gradio\\frpc_windows_amd64_v0.2. \n",
|
85 |
+
"\n",
|
86 |
+
"Please check your internet connection. This can happen if your antivirus software blocks the download of this file. You can install manually by following these steps: \n",
|
87 |
+
"\n",
|
88 |
+
"1. Download this file: https://cdn-media.huggingface.co/frpc-gradio-0.2/frpc_windows_amd64.exe\n",
|
89 |
+
"2. Rename the downloaded file to: frpc_windows_amd64_v0.2\n",
|
90 |
+
"3. Move the file to this location: f:\\miniconda3\\envs\\btl\\Lib\\site-packages\\gradio\n"
|
91 |
+
]
|
92 |
+
},
|
93 |
+
{
|
94 |
+
"name": "stderr",
|
95 |
+
"output_type": "stream",
|
96 |
+
"text": [
|
97 |
+
"\n"
|
98 |
+
]
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"data": {
|
102 |
+
"text/html": [
|
103 |
+
"<div><iframe src=\"http://127.0.0.1:7904/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
104 |
+
],
|
105 |
+
"text/plain": [
|
106 |
+
"<IPython.core.display.HTML object>"
|
107 |
+
]
|
108 |
+
},
|
109 |
+
"metadata": {},
|
110 |
+
"output_type": "display_data"
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"data": {
|
114 |
+
"text/plain": []
|
115 |
+
},
|
116 |
+
"execution_count": 51,
|
117 |
+
"metadata": {},
|
118 |
+
"output_type": "execute_result"
|
119 |
+
}
|
120 |
+
],
|
121 |
+
"source": [
|
122 |
+
"# #setup\n",
|
123 |
+
"# model_path = \"KAITANY/finetuned-roberta-base-sentiment\"\n",
|
124 |
+
"\n",
|
125 |
+
"# tokenizer = AutoTokenizer.from_pretrained(model_path)\n",
|
126 |
+
"# #config = AutoConfig.from_pretrained(model_path)\n",
|
127 |
+
"# model = AutoModelForSequenceClassification.from_pretrained(model_path)\n",
|
128 |
+
"\n",
|
129 |
+
"# def preprocess(text):\n",
|
130 |
+
"# # Preprocess text (username and link placeholders)\n",
|
131 |
+
"# new_text = []\n",
|
132 |
+
"# for t in text.split(\" \"):\n",
|
133 |
+
"# t = '@user' if t.startswith('@') and len(t) > 1 else t\n",
|
134 |
+
"# t = 'http' if t.startswith('http') else t\n",
|
135 |
+
"# new_text.append(t)\n",
|
136 |
+
"# return \" \".join(new_text)\n",
|
137 |
+
"\n",
|
138 |
+
"# def sentiment_analysis(text):\n",
|
139 |
+
" # text = preprocess(text)\n",
|
140 |
+
"\n",
|
141 |
+
" # # Tokenize the text\n",
|
142 |
+
" # inputs = tokenizer(text, return_tensors=\"pt\", padding=True)\n",
|
143 |
+
"\n",
|
144 |
+
" # # Make a prediction\n",
|
145 |
+
" # with torch.no_grad():\n",
|
146 |
+
" # outputs = model(**inputs)\n",
|
147 |
+
"\n",
|
148 |
+
" # # Get the predicted class probabilities\n",
|
149 |
+
" # scores = torch.softmax(outputs.logits, dim=1).tolist()[0]\n",
|
150 |
+
" # # Map the scores to labels\n",
|
151 |
+
" # labels = ['Negative', 'Neutral', 'Positive']\n",
|
152 |
+
" # scores_dict = {label: score for label, score in zip(labels, scores)}\n",
|
153 |
+
"\n",
|
154 |
+
" # return scores_dict\n",
|
155 |
+
"#demo\n",
|
156 |
+
"aspects = ['General', 'Battery', 'Performance', 'Camera', 'Ser&Acc', 'Others', 'Design', 'Screen', 'Features', 'Price']\n",
|
157 |
+
"aspects_ratio = (np.random.dirichlet(np.ones(10), size=1) * 100).flatten()\n",
|
158 |
+
"\n",
|
159 |
+
"sentiments_ratio = (np.random.dirichlet(np.ones(3), size=1) * 100).flatten()\n",
|
160 |
+
"sentiments = ['Positive', 'Negative', 'Neutral']\n",
|
161 |
+
"\n",
|
162 |
+
"aspects_polarity = []\n",
|
163 |
+
"aspects_polarity_ratio = (np.random.dirichlet(np.ones(30), size=1) * 100).flatten()\n",
|
164 |
+
"for aspect in aspects:\n",
|
165 |
+
" for sentiment in sentiments:\n",
|
166 |
+
" aspects_polarity.append(aspect + '#' + sentiment) \n",
|
167 |
+
"\n",
|
168 |
+
"def sentiment_analysis(text, aspect):\n",
|
169 |
+
"\n",
|
170 |
+
" # Tạo biểu đồ cảm xúc theo aspect\n",
|
171 |
+
" pie_sentiments_of_an_aspect = draw_pie_sentiments_of_an_aspect(aspect)\n",
|
172 |
+
"\n",
|
173 |
+
" #Biểu đồ aspect\n",
|
174 |
+
" pie_of_all_aspect = draw_pie_of_aspect()\n",
|
175 |
+
"\n",
|
176 |
+
" #Biểu đồ aspect#polirity\n",
|
177 |
+
" pie_aspect_polarity = draw_pie_aspect_polarity()\n",
|
178 |
+
" # return [pie_aspect, pie_all_aspect, pie_aspect_polarity]\n",
|
179 |
+
" return pie_sentiments_of_an_aspect, pie_of_all_aspect, pie_aspect_polarity\n",
|
180 |
+
"\n",
|
181 |
+
"def draw_pie_sentiments_of_an_aspect(aspect):\n",
|
182 |
+
" sentiments_ratio = (np.random.dirichlet(np.ones(3), size=1) * 100).flatten()\n",
|
183 |
+
" pie_sentiments_of_an_aspect = plt.figure(figsize=(5,5))\n",
|
184 |
+
" plt.pie(sentiments_ratio, labels=sentiments, autopct='%1.1f%%', startangle=140)\n",
|
185 |
+
" return pie_sentiments_of_an_aspect\n",
|
186 |
+
"\n",
|
187 |
+
"def draw_pie_of_aspect():\n",
|
188 |
+
" pie_aspect = plt.figure(figsize=(5,5))\n",
|
189 |
+
" plt.pie(aspects_ratio, labels=aspects, autopct='%1.1f%%', startangle=140)\n",
|
190 |
+
" return pie_aspect\n",
|
191 |
+
"\n",
|
192 |
+
"def draw_pie_aspect_polarity():\n",
|
193 |
+
" pie_aspect_polarity = plt.figure(figsize=(20, 10))\n",
|
194 |
+
" plt.pie(aspects_polarity_ratio, labels=aspects_polarity, autopct='%1.1f%%', startangle=140)\n",
|
195 |
+
" plt.legend(aspects_polarity, loc='upper right', bbox_to_anchor=(1.5, 1.))\n",
|
196 |
+
" return pie_aspect_polarity\n",
|
197 |
+
"\n",
|
198 |
+
"def submit(comment, aspect):\n",
|
199 |
+
" return sentiment_analysis(comment, aspect)\n",
|
200 |
+
" \n",
|
201 |
+
"title = \"Sentiment Analysis Application\\n\\n\\nThis application assesses if a twitter post relating to vaccination is positive,neutral or negative\"\n",
|
202 |
+
"with gr.Blocks() as demo:\n",
|
203 |
+
" with gr.Row():\n",
|
204 |
+
" text_box = gr.Textbox(placeholder=\"Write your comment here...\", visible=True, label=\"Comment\")\n",
|
205 |
+
" submit_btn = gr.Button(\"Submit\")\n",
|
206 |
+
" with gr.Row():\n",
|
207 |
+
" with gr.Column():\n",
|
208 |
+
" choose_aspect_dropdown = gr.Dropdown(\n",
|
209 |
+
" choices=['General', 'Battery', 'Performance', 'Camera', 'Ser&Acc', 'Others', 'Design', 'Screen', 'Features', 'Price'], \n",
|
210 |
+
" label=\"Choose Aspect\",\n",
|
211 |
+
" value='General'\n",
|
212 |
+
" )\n",
|
213 |
+
" pie_sentiment = gr.Plot()\n",
|
214 |
+
" pie_all_aspect = gr.Plot()\n",
|
215 |
+
" pie_aspect_polarity = gr.Plot(min_width=2000)\n",
|
216 |
+
" # demo.fn(draw_pie_aspect(choose_aspect_dropdown))\n",
|
217 |
+
"\n",
|
218 |
+
" choose_aspect_dropdown.select(\n",
|
219 |
+
" fn = draw_pie_sentiments_of_an_aspect,\n",
|
220 |
+
" inputs = [choose_aspect_dropdown],\n",
|
221 |
+
" outputs = [pie_sentiment],\n",
|
222 |
+
" )\n",
|
223 |
+
"\n",
|
224 |
+
" submit_btn.click(\n",
|
225 |
+
" fn = submit,\n",
|
226 |
+
" inputs = [text_box, choose_aspect_dropdown],\n",
|
227 |
+
" outputs = [pie_sentiment, pie_all_aspect, pie_aspect_polarity],\n",
|
228 |
+
")\n",
|
229 |
+
"\n",
|
230 |
+
"\n",
|
231 |
+
"# demo = gr.Interface(\n",
|
232 |
+
"# fn=sentiment_analysis,\n",
|
233 |
+
"# inputs=gr.Textbox(placeholder=\"Write your tweet here...\"),\n",
|
234 |
+
"# outputs=gr.Plot(),\n",
|
235 |
+
"# examples=[[\"The Vaccine is harmful!\"],[\"I cant believe people don't vaccinate their kids\"],[\"FDA think just not worth the AE unfortunately\"],[\"For a vaccine given to healthy\"]],\n",
|
236 |
+
"# title=title\n",
|
237 |
+
"# )\n",
|
238 |
+
"\n",
|
239 |
+
"demo.launch(share=True)"
|
240 |
+
]
|
241 |
+
},
|
242 |
+
{
|
243 |
+
"cell_type": "code",
|
244 |
+
"execution_count": 32,
|
245 |
+
"metadata": {},
|
246 |
+
"outputs": [
|
247 |
+
{
|
248 |
+
"name": "stdout",
|
249 |
+
"output_type": "stream",
|
250 |
+
"text": [
|
251 |
+
"Running on local URL: http://127.0.0.1:7881\n",
|
252 |
+
"\n",
|
253 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
254 |
+
]
|
255 |
+
},
|
256 |
+
{
|
257 |
+
"data": {
|
258 |
+
"text/html": [
|
259 |
+
"<div><iframe src=\"http://127.0.0.1:7881/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
260 |
+
],
|
261 |
+
"text/plain": [
|
262 |
+
"<IPython.core.display.HTML object>"
|
263 |
+
]
|
264 |
+
},
|
265 |
+
"metadata": {},
|
266 |
+
"output_type": "display_data"
|
267 |
+
},
|
268 |
+
{
|
269 |
+
"data": {
|
270 |
+
"text/plain": []
|
271 |
+
},
|
272 |
+
"execution_count": 32,
|
273 |
+
"metadata": {},
|
274 |
+
"output_type": "execute_result"
|
275 |
+
}
|
276 |
+
],
|
277 |
+
"source": [
|
278 |
+
"import gradio as gr\n",
|
279 |
+
"\n",
|
280 |
+
"with gr.Blocks() as demo:\n",
|
281 |
+
" error_box = gr.Textbox(label=\"Error\", visible=False)\n",
|
282 |
+
"\n",
|
283 |
+
" name_box = gr.Textbox(label=\"Name\")\n",
|
284 |
+
" age_box = gr.Number(label=\"Age\", minimum=0, maximum=100)\n",
|
285 |
+
" symptoms_box = gr.CheckboxGroup([\"Cough\", \"Fever\", \"Runny Nose\"])\n",
|
286 |
+
" submit_btn = gr.Button(\"Submit\")\n",
|
287 |
+
"\n",
|
288 |
+
" with gr.Column(visible=False) as output_col:\n",
|
289 |
+
" diagnosis_box = gr.Textbox(label=\"Diagnosis\")\n",
|
290 |
+
" patient_summary_box = gr.Textbox(label=\"Patient Summary\")\n",
|
291 |
+
"\n",
|
292 |
+
" def submit(name, age, symptoms):\n",
|
293 |
+
" if len(name) == 0:\n",
|
294 |
+
" return {error_box: gr.Textbox(value=\"Enter name\", visible=True)}\n",
|
295 |
+
" return {\n",
|
296 |
+
" output_col: gr.Column(visible=True),\n",
|
297 |
+
" diagnosis_box: \"covid\" if \"Cough\" in symptoms else \"flu\",\n",
|
298 |
+
" patient_summary_box: f\"{name}, {age} y/o\",\n",
|
299 |
+
" }\n",
|
300 |
+
"\n",
|
301 |
+
" submit_btn.click(\n",
|
302 |
+
" submit,\n",
|
303 |
+
" [name_box, age_box, symptoms_box],\n",
|
304 |
+
" [error_box, diagnosis_box, patient_summary_box, output_col],\n",
|
305 |
+
" )\n",
|
306 |
+
"\n",
|
307 |
+
"demo.launch()\n"
|
308 |
+
]
|
309 |
+
},
|
310 |
+
{
|
311 |
+
"cell_type": "code",
|
312 |
+
"execution_count": null,
|
313 |
+
"metadata": {},
|
314 |
+
"outputs": [],
|
315 |
+
"source": []
|
316 |
+
}
|
317 |
+
],
|
318 |
+
"metadata": {
|
319 |
+
"kernelspec": {
|
320 |
+
"display_name": "btl",
|
321 |
+
"language": "python",
|
322 |
+
"name": "python3"
|
323 |
+
},
|
324 |
+
"language_info": {
|
325 |
+
"codemirror_mode": {
|
326 |
+
"name": "ipython",
|
327 |
+
"version": 3
|
328 |
+
},
|
329 |
+
"file_extension": ".py",
|
330 |
+
"mimetype": "text/x-python",
|
331 |
+
"name": "python",
|
332 |
+
"nbconvert_exporter": "python",
|
333 |
+
"pygments_lexer": "ipython3",
|
334 |
+
"version": "3.12.2"
|
335 |
+
}
|
336 |
+
},
|
337 |
+
"nbformat": 4,
|
338 |
+
"nbformat_minor": 2
|
339 |
+
}
|