Delete gradio.ipynb
Browse files- gradio.ipynb +0 -306
gradio.ipynb
DELETED
@@ -1,306 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "markdown",
|
5 |
-
"metadata": {},
|
6 |
-
"source": [
|
7 |
-
"<center>\n",
|
8 |
-
"\n",
|
9 |
-
"## [S. Mussard](https://sites.google.com/view/cv-stphane-mussard/accueil \"Homepage\")\n",
|
10 |
-
"\n",
|
11 |
-
"# UM6P\n",
|
12 |
-
"\n",
|
13 |
-
"# Natural Language Processing: LOGIT\n",
|
14 |
-
"\n",
|
15 |
-
"\n",
|
16 |
-
"<center> <a href=\"https://www.fgses-um6p.ma/\"><img src=\"UM6P.png\",style=\"float: left; max-width: 500px; width: 20\" />\n",
|
17 |
-
"\n",
|
18 |
-
"\n",
|
19 |
-
"\n",
|
20 |
-
"<div align=\"center\"> \n",
|
21 |
-
"<a href=\"https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html\"><img src=\"http://scikit-learn.org/stable/_static/scikit-learn-logo-small.png\" style=\"max-width: 180px; display: inline\" alt=\"Scikit-Learn\"/></a>\n",
|
22 |
-
"</div>\n",
|
23 |
-
"<div align=\"center\"> <a href=\"https://www.python.org/\"><img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/390px-Python_logo_and_wordmark.svg.png\" style=\"max-width: 150px; display: inline\" alt=\"Python\"/></a> \n",
|
24 |
-
"</div>\n",
|
25 |
-
" \n"
|
26 |
-
]
|
27 |
-
},
|
28 |
-
{
|
29 |
-
"cell_type": "markdown",
|
30 |
-
"metadata": {},
|
31 |
-
"source": [
|
32 |
-
"<div align=\"center\">\n",
|
33 |
-
"\n",
|
34 |
-
"## Sentiment Analysis"
|
35 |
-
]
|
36 |
-
},
|
37 |
-
{
|
38 |
-
"cell_type": "code",
|
39 |
-
"execution_count": 1,
|
40 |
-
"metadata": {},
|
41 |
-
"outputs": [
|
42 |
-
{
|
43 |
-
"name": "stderr",
|
44 |
-
"output_type": "stream",
|
45 |
-
"text": [
|
46 |
-
"C:\\Users\\smussa01\\AppData\\Roaming\\Python\\Python37\\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",
|
47 |
-
" from .autonotebook import tqdm as notebook_tqdm\n"
|
48 |
-
]
|
49 |
-
}
|
50 |
-
],
|
51 |
-
"source": [
|
52 |
-
"# Importation \n",
|
53 |
-
"\n",
|
54 |
-
"%matplotlib inline \n",
|
55 |
-
"import numpy as np\n",
|
56 |
-
"import pandas as pd\n",
|
57 |
-
"import matplotlib.pyplot as plt\n",
|
58 |
-
"from sklearn import metrics\n",
|
59 |
-
"import torch\n",
|
60 |
-
"from torch.utils.data import Dataset, DataLoader\n",
|
61 |
-
"from transformers import AutoModel, AutoTokenizer\n",
|
62 |
-
"from transformers import AutoModelForSequenceClassification, AutoTokenizer\n",
|
63 |
-
"\n",
|
64 |
-
"import gradio as gr\n",
|
65 |
-
"from gradio.components import Label"
|
66 |
-
]
|
67 |
-
},
|
68 |
-
{
|
69 |
-
"cell_type": "code",
|
70 |
-
"execution_count": 2,
|
71 |
-
"metadata": {},
|
72 |
-
"outputs": [
|
73 |
-
{
|
74 |
-
"name": "stderr",
|
75 |
-
"output_type": "stream",
|
76 |
-
"text": [
|
77 |
-
"Some weights of the model checkpoint at S:\\Mes Documents\\Cours\\Cours-NLP\\PFE kenza\\poids were not used when initializing RobertaModel: ['classifier.out_proj.bias', 'classifier.dense.weight', 'classifier.out_proj.weight', 'classifier.dense.bias']\n",
|
78 |
-
"- This IS expected if you are initializing RobertaModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
|
79 |
-
"- This IS NOT expected if you are initializing RobertaModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
|
80 |
-
"Some weights of RobertaModel were not initialized from the model checkpoint at S:\\Mes Documents\\Cours\\Cours-NLP\\PFE kenza\\poids and are newly initialized: ['roberta.pooler.dense.bias', 'roberta.pooler.dense.weight']\n",
|
81 |
-
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
|
82 |
-
]
|
83 |
-
}
|
84 |
-
],
|
85 |
-
"source": [
|
86 |
-
"path = \".\\poids\"\n",
|
87 |
-
"model = AutoModel.from_pretrained(path, trust_remote_code=True)\n",
|
88 |
-
"class CamembertClass(torch.nn.Module):\n",
|
89 |
-
" def __init__(self):\n",
|
90 |
-
" super(CamembertClass, self).__init__()\n",
|
91 |
-
" self.l1 = model\n",
|
92 |
-
" self.dropout = torch.nn.Dropout(0.1)\n",
|
93 |
-
" self.pre_classifier = torch.nn.Linear(1024, 1024)\n",
|
94 |
-
" self.classifier = torch.nn.Linear(1024, 3)\n",
|
95 |
-
"\n",
|
96 |
-
" def forward(self, input_ids, attention_mask, token_type_ids):\n",
|
97 |
-
" output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)\n",
|
98 |
-
" hidden_state = output_1[0]\n",
|
99 |
-
" pooler = hidden_state[:, 0]\n",
|
100 |
-
" pooler = self.pre_classifier(pooler)\n",
|
101 |
-
" pooler = torch.nn.ReLU()(pooler)\n",
|
102 |
-
" pooler = self.dropout(pooler)\n",
|
103 |
-
" output = self.classifier(pooler)\n",
|
104 |
-
" return output"
|
105 |
-
]
|
106 |
-
},
|
107 |
-
{
|
108 |
-
"cell_type": "code",
|
109 |
-
"execution_count": 3,
|
110 |
-
"metadata": {},
|
111 |
-
"outputs": [],
|
112 |
-
"source": [
|
113 |
-
"#model_gradio = CamembertClass()\n",
|
114 |
-
"path = \"S:\\Mes Documents\\Cours\\Cours-NLP\\PFE kenza\\pytorch_model.bin\"\n",
|
115 |
-
"model = torch.load(path, map_location=\"cpu\")\n",
|
116 |
-
"path_tokenizer = \"S:\\Mes Documents\\Cours\\Cours-NLP\\PFE kenza\"\n",
|
117 |
-
"tokenizer = AutoTokenizer.from_pretrained(path_tokenizer)\n"
|
118 |
-
]
|
119 |
-
},
|
120 |
-
{
|
121 |
-
"cell_type": "code",
|
122 |
-
"execution_count": 4,
|
123 |
-
"metadata": {},
|
124 |
-
"outputs": [],
|
125 |
-
"source": [
|
126 |
-
"#pip install pydantic==1.10.7"
|
127 |
-
]
|
128 |
-
},
|
129 |
-
{
|
130 |
-
"cell_type": "code",
|
131 |
-
"execution_count": 6,
|
132 |
-
"metadata": {},
|
133 |
-
"outputs": [
|
134 |
-
{
|
135 |
-
"name": "stdout",
|
136 |
-
"output_type": "stream",
|
137 |
-
"text": [
|
138 |
-
"Running on local URL: http://127.0.0.1:7861\n",
|
139 |
-
"Running on public URL: https://c6de28517ce6caf32f.gradio.live\n",
|
140 |
-
"\n",
|
141 |
-
"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"
|
142 |
-
]
|
143 |
-
},
|
144 |
-
{
|
145 |
-
"data": {
|
146 |
-
"text/html": [
|
147 |
-
"<div><iframe src=\"https://c6de28517ce6caf32f.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
148 |
-
],
|
149 |
-
"text/plain": [
|
150 |
-
"<IPython.core.display.HTML object>"
|
151 |
-
]
|
152 |
-
},
|
153 |
-
"metadata": {},
|
154 |
-
"output_type": "display_data"
|
155 |
-
},
|
156 |
-
{
|
157 |
-
"data": {
|
158 |
-
"text/plain": []
|
159 |
-
},
|
160 |
-
"execution_count": 6,
|
161 |
-
"metadata": {},
|
162 |
-
"output_type": "execute_result"
|
163 |
-
}
|
164 |
-
],
|
165 |
-
"source": [
|
166 |
-
"model.eval() # Mettez votre modèle en mode évaluation\n",
|
167 |
-
"\n",
|
168 |
-
"# Fonction d'inférence pour Gradio\n",
|
169 |
-
"def predict(text):\n",
|
170 |
-
" inputs = tokenizer(text, return_tensors=\"pt\", padding=True, truncation=True, max_length=512)\n",
|
171 |
-
" \n",
|
172 |
-
" # Extract necessary inputs for the model\n",
|
173 |
-
" input_ids = inputs['input_ids']\n",
|
174 |
-
" attention_mask = inputs['attention_mask']\n",
|
175 |
-
" token_type_ids = inputs.get('token_type_ids', None) # Some models do not use segment IDs\n",
|
176 |
-
" \n",
|
177 |
-
" # Make prediction\n",
|
178 |
-
" with torch.no_grad():\n",
|
179 |
-
" # Directly use outputs if your model returns logits directly\n",
|
180 |
-
" logits = model(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)\n",
|
181 |
-
"\n",
|
182 |
-
" \n",
|
183 |
-
" # Convert logits to probabilities\n",
|
184 |
-
" probabilities = torch.softmax(logits, dim=1).detach().cpu().numpy()[0]\n",
|
185 |
-
" # Replace the following with your actual classes\n",
|
186 |
-
" classes = ['Negative Sentiment', 'Positive Sentiment']\n",
|
187 |
-
" return {classes[i]: float(probabilities[i]) for i in range(len(classes))}\n",
|
188 |
-
"\n",
|
189 |
-
"# Création de l'interface Gradio\n",
|
190 |
-
"iface = gr.Interface(fn=predict,\n",
|
191 |
-
" inputs=gr.components.Textbox(placeholder=\"Enter your text here...\"),\n",
|
192 |
-
" outputs=gr.components.Label(num_top_classes=2))\n",
|
193 |
-
"iface.launch(share=True)\n"
|
194 |
-
]
|
195 |
-
},
|
196 |
-
{
|
197 |
-
"cell_type": "markdown",
|
198 |
-
"metadata": {},
|
199 |
-
"source": [
|
200 |
-
"### <span style=\"color:blue\">Dataset importation : absences.csv</span>"
|
201 |
-
]
|
202 |
-
},
|
203 |
-
{
|
204 |
-
"cell_type": "code",
|
205 |
-
"execution_count": 5,
|
206 |
-
"metadata": {},
|
207 |
-
"outputs": [
|
208 |
-
{
|
209 |
-
"data": {
|
210 |
-
"text/plain": [
|
211 |
-
"{'Negative Sentiment': 0.8629835844039917,\n",
|
212 |
-
" 'Positive Sentiment': 0.1370164006948471}"
|
213 |
-
]
|
214 |
-
},
|
215 |
-
"execution_count": 5,
|
216 |
-
"metadata": {},
|
217 |
-
"output_type": "execute_result"
|
218 |
-
}
|
219 |
-
],
|
220 |
-
"source": [
|
221 |
-
"predict(\"Marrakech is a poop\")"
|
222 |
-
]
|
223 |
-
},
|
224 |
-
{
|
225 |
-
"cell_type": "code",
|
226 |
-
"execution_count": 30,
|
227 |
-
"metadata": {},
|
228 |
-
"outputs": [
|
229 |
-
{
|
230 |
-
"name": "stdout",
|
231 |
-
"output_type": "stream",
|
232 |
-
"text": [
|
233 |
-
"Running on local URL: http://127.0.0.1:7868\n",
|
234 |
-
"\n",
|
235 |
-
"To create a public link, set `share=True` in `launch()`.\n"
|
236 |
-
]
|
237 |
-
},
|
238 |
-
{
|
239 |
-
"data": {
|
240 |
-
"text/html": [
|
241 |
-
"<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
242 |
-
],
|
243 |
-
"text/plain": [
|
244 |
-
"<IPython.core.display.HTML object>"
|
245 |
-
]
|
246 |
-
},
|
247 |
-
"metadata": {},
|
248 |
-
"output_type": "display_data"
|
249 |
-
},
|
250 |
-
{
|
251 |
-
"data": {
|
252 |
-
"text/plain": []
|
253 |
-
},
|
254 |
-
"execution_count": 30,
|
255 |
-
"metadata": {},
|
256 |
-
"output_type": "execute_result"
|
257 |
-
}
|
258 |
-
],
|
259 |
-
"source": [
|
260 |
-
"def image_clf(inp):\n",
|
261 |
-
" return {'cat': 0.3 , 'dog': 0.7}\n",
|
262 |
-
"demo = gr.Interface(fn=image_clf, inputs=\"image\", outputs=\"label\")\n",
|
263 |
-
"demo.launch()\n",
|
264 |
-
" "
|
265 |
-
]
|
266 |
-
}
|
267 |
-
],
|
268 |
-
"metadata": {
|
269 |
-
"hide_input": false,
|
270 |
-
"kernelspec": {
|
271 |
-
"display_name": "Python 3",
|
272 |
-
"language": "python",
|
273 |
-
"name": "python3"
|
274 |
-
},
|
275 |
-
"language_info": {
|
276 |
-
"codemirror_mode": {
|
277 |
-
"name": "ipython",
|
278 |
-
"version": 3
|
279 |
-
},
|
280 |
-
"file_extension": ".py",
|
281 |
-
"mimetype": "text/x-python",
|
282 |
-
"name": "python",
|
283 |
-
"nbconvert_exporter": "python",
|
284 |
-
"pygments_lexer": "ipython3",
|
285 |
-
"version": "3.7.8"
|
286 |
-
},
|
287 |
-
"toc": {
|
288 |
-
"base_numbering": 1,
|
289 |
-
"nav_menu": {
|
290 |
-
"height": "244px",
|
291 |
-
"width": "252px"
|
292 |
-
},
|
293 |
-
"number_sections": true,
|
294 |
-
"sideBar": true,
|
295 |
-
"skip_h1_title": false,
|
296 |
-
"title_cell": "Table of Contents",
|
297 |
-
"title_sidebar": "Contents",
|
298 |
-
"toc_cell": false,
|
299 |
-
"toc_position": {},
|
300 |
-
"toc_section_display": "block",
|
301 |
-
"toc_window_display": false
|
302 |
-
}
|
303 |
-
},
|
304 |
-
"nbformat": 4,
|
305 |
-
"nbformat_minor": 1
|
306 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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