Karim-Gamal commited on
Commit
0495d77
1 Parent(s): 0d195cd

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +326 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, SwitchTransformersForConditionalGeneration, AutoModelForSequenceClassification
2
+ import torch
3
+ import gradio as gr
4
+ import argparse
5
+ from scipy.special import softmax
6
+ import csv
7
+ import urllib.request
8
+ import numpy as np
9
+ import requests
10
+
11
+
12
+
13
+
14
+ args_dict = dict(
15
+ EX_LIST = [["This is wonderful!"],
16
+ ["Nice car"],
17
+ ["La France est la meilleure équipe du monde"],
18
+ ["Visca Barca"],
19
+ ["Hala Madrid"],
20
+ ["Buongiorno"],
21
+ # ["Auf einigen deutschen Straßen gibt es kein Radar"],
22
+ ["Tempo soleggiato in Italia"],
23
+ ["Bonjour"],
24
+ ["صباح الخير"],
25
+ ["اكل زوجتي جميل"],
26
+ ],
27
+
28
+ #MMiniLM
29
+ # Load the pretrained model and tokenizer
30
+ tokenizer_MMiniLM = AutoTokenizer.from_pretrained("Karim-Gamal/MMiniLM-L12-finetuned-emojis-IID-Fed"),
31
+ model_MMiniLM = AutoModelForSequenceClassification.from_pretrained("Karim-Gamal/MMiniLM-L12-finetuned-emojis-IID-Fed"),
32
+
33
+ #XLM
34
+ # Load the pretrained model and tokenizer
35
+ tokenizer_XLM = AutoTokenizer.from_pretrained("Karim-Gamal/XLM-Roberta-finetuned-emojis-IID-Fed"),
36
+ model_XLM = AutoModelForSequenceClassification.from_pretrained("Karim-Gamal/XLM-Roberta-finetuned-emojis-IID-Fed"),
37
+
38
+ #Bert
39
+ # Load the pretrained model and tokenizer
40
+ tokenizer_Bert = AutoTokenizer.from_pretrained("Karim-Gamal/BERT-base-finetuned-emojis-IID-Fed"),
41
+ model_Bert = AutoModelForSequenceClassification.from_pretrained("Karim-Gamal/BERT-base-finetuned-emojis-IID-Fed"),
42
+
43
+
44
+
45
+ description = 'Real-time Emoji Prediction',
46
+ article = '<head><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,800italic,400,600,700,800)<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css"> <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.21.2/dist/bootstrap-table.min.css">\
47
+ .table-responsive{-sm|-md|-lg|-xl} body{ background-color: #f5f5f5; padding: 120px 0; font-family: \'Open Sans\', sans-serif; } img{ max-width:100%; } .div_table_{ position:relative; width: max-content; margin:0 auto; } .profile-card{ position:relative; width:280px; margin:0 auto; padding:40px 30px 30px; background:#fff; border: 5px solid rgba(255,255,255,.7); text-align:center; border-radius:40px; transition: all 200ms ease; } .profile-card_2{ position:relative; width:60%; // margin:0 auto; padding:40px 30px 30px; background:#fff; border: 5px solid rgba(255,255,255,.7); text-align:center; border-radius:40px; transition: all 200ms ease; } .mask-shadow{ z-index:-1 !important; width:95%; height:12px; background:#000; bottom:0; left:0; right:0; margin:0 auto; position:absolute; border-radius:4px; opacity:0; transition: all 400ms ease-in; } .mask-shadow_2{ z-index:-1 !important; width:95%; height:12px; background:#000; bottom:0; left:0; right:0; margin:0 auto; position:absolute; border-radius:4px; opacity:0; transition: all 400ms ease-in; } .profile-card:hover{ box-shadow: 0px 30px 60px -5px rgba(55,55,71,0.3); transform: translate3d(0,-5px,0); .mask-shadow{ opacity:1; box-shadow: 0px 30px 60px -5px rgba(55,55,71,0.3); position:absolute; } } .profile-card_2:hover{ box-shadow: 0px 30px 60px -5px rgba(55,55,71,0.3); transform: translate3d(0,-5px,0); .mask-shadow{ opacity:1; box-shadow: 0px 30px 60px -5px rgba(55,55,71,0.3); position:absolute; } } .profile-card header{ display:block; margin-bottom:10px; } .profile-card_2 header{ display:block; margin-bottom:10px; } .profile-card header a{ width:150px; height:150px; display:block; border-radius:100%; margin:-120px auto 0; box-shadow: 0 0 0 5px #82b541; } .profile-card_2 header a{ width:85%; height:85%; display:block; border-radius:10%; margin:-120px auto 0; box-shadow: 0 0 0 5px #82b541; } .profile-card header a img{ border-radius: 50%; width:150px; height:150px; } .profile-card_2 header a img{ border-radius: 10%; width:100%; height:100%; } .profile-card:hover header a, .profile-card header a:hover{ animation: bounceOut .4s linear; -webkit-animation: bounceOut .4s linear; } .profile-card_2:hover header a, .profile-card header a:hover{ animation: bounceOut .4s linear; -webkit-animation: bounceOut .4s linear; } .profile-card header h1{ font-size:20px; padding:20px; color:#444; text-transform:uppercase; margin-bottom:5px; } .profile-card_2 header h1{ font-size:20px; padding:20px; color:#444; text-transform:uppercase; margin-bottom:5px; } .profile-card header h2{ font-size:14px; color:#acacac; text-transform:uppercase; margin:0; } .profile-card_2 header h2{ font-size:14px; color:#acacac; text-transform:uppercase; margin:0; } /*content*/ .profile-bio{ font-size:14px; color:#a5a5a5; line-height:1.7; font-style: italic; margin-bottom:30px; } /*link social*/ .profile-social-links{ margin:0; padding:0; list-style:none; } .profile-social-links li{ display: inline-block; margin: 0 10px; } .profile-social-links li a{ width: 55px; height:55px; display:block; background:#f1f1f1; border-radius:50%; -webkit-transition: all 2.75s cubic-bezier(0,.83,.17,1); -moz-transition: all 2.75s cubic-bezier(0,.83,.17,1); -o-transition: all 2.75s cubic-bezier(0,.83,.17,1); transition: all 2.75s cubic-bezier(0,.83,.17,1); transform-style: preserve-3d; } .profile-social-links li a img{ width:35px; height:35px; margin:10px auto 0; } .profile-social-links li a:hover{ background:#ddd; transform: scale(1.2); -webkit-transform: scale(1.2); } /*animation hover effect*/ @-webkit-keyframes bounceOut { 0% { box-shadow: 0 0 0 4px #82b541; opacity: 1; } 25% { box-shadow: 0 0 0 1px #82b541; opacity: 1; } 50% { box-shadow: 0 0 0 7px #82b541; opacity: 1; } 75% { box-shadow: 0 0 0 4px #82b541; opacity: 1; } 100% { box-shadow: 0 0 0 5px #82b541; opacity: 1; } } @keyframes bounceOut { 0% { box-shadow: 0 0 0 6px #82b541; opacity: 1; } 25% { box-shadow: 0 0 0 2px #82b541; opacity: 1; } 50% { box-shadow: 0 0 0 9px #82b541; opacity: 1; } 75% { box-shadow: 0 0 0 3px #82b541; opacity: 1; } 100% { box-shadow: 0 0 0 5px #82b541; opacity: 1; } }</style></head>',
48
+
49
+
50
+ )
51
+
52
+ config = argparse.Namespace(**args_dict)
53
+
54
+
55
+
56
+ # Preprocess text (username and link placeholders)
57
+ def preprocess(text):
58
+ text = text.lower()
59
+ new_text = []
60
+ for t in text.split(" "):
61
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
62
+ t = '' if t.startswith('http') else t
63
+ new_text.append(t)
64
+ # print(" ".join(new_text))
65
+ return " ".join(new_text)
66
+
67
+
68
+
69
+ def test_with_sentance(text ,net ,tokenizer):
70
+
71
+ # text = "good morning"
72
+ text = preprocess(text)
73
+
74
+ # tc = TweetCleaner(remove_stop_words=True, remove_retweets=False)
75
+ # print('before : ' ,text)
76
+ # text = tc.get_cleaned_text(text)
77
+ # print('after : ' ,text)
78
+
79
+ net.eval()
80
+ encoded_input = tokenizer.encode(text, padding=True, truncation=True, return_tensors='pt')
81
+ net.to('cpu')
82
+ # print(type())
83
+ # encoded_input = {k: v.to(DEVICE) for k, v in encoded_input.items()}
84
+ output = net(encoded_input)
85
+ scores = output[0][0].detach().numpy()
86
+ scores = softmax(scores)
87
+
88
+ # download label mapping
89
+ labels=[]
90
+ mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/emoji/mapping.txt"
91
+ with urllib.request.urlopen(mapping_link) as f:
92
+ html = f.read().decode('utf-8').split("\n")
93
+ csvreader = csv.reader(html, delimiter='\t')
94
+ labels = [row[1] for row in csvreader if len(row) > 1]
95
+
96
+ ranking = np.argsort(scores)
97
+ ranking = ranking[::-1]
98
+ output_d = {}
99
+ for i in range(scores.shape[0]):
100
+ l = labels[ranking[i]]
101
+ s = scores[ranking[i]]
102
+ # print(f"{ranking[i]}) {l} {np.round(float(s), 4)}")
103
+ output_d[l] = np.round(float(s), 4)
104
+
105
+ if i == 2 :
106
+ # break
107
+ return output_d
108
+
109
+ # net.to('cuda:0')
110
+
111
+
112
+
113
+ list_interface = []
114
+ list_title = []
115
+
116
+
117
+
118
+
119
+
120
+
121
+ # BERT
122
+
123
+ def _method(text):
124
+
125
+ # tokenizer = AutoTokenizer.from_pretrained(config.CHECKPOINT_BERT)
126
+ # model_loaded = torch.load('/content/NEW_MODELS_Imbalance/Bert/g_ex3_bert_multi_fed_data_epoch_2.pt', map_location=torch.device('cpu'))
127
+
128
+ return test_with_sentance(text , config.model_Bert , config.tokenizer_Bert)
129
+
130
+ # greet("sun")
131
+
132
+ interface = gr.Interface(
133
+
134
+ fn = _method,
135
+
136
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
137
+ outputs="label",
138
+ examples=config.EX_LIST,
139
+ live = True,
140
+
141
+
142
+ title = 'BERT Multilingual',
143
+
144
+ description=config.description,
145
+ article = '',
146
+
147
+ )
148
+ list_interface.append(interface)
149
+ list_title.append('BERT Multilingual')
150
+
151
+
152
+ # XLM
153
+
154
+ def _method(text):
155
+
156
+ # tokenizer = AutoTokenizer.from_pretrained(config.CHECKPOINT_BERT)
157
+ # model_loaded = torch.load('/content/NEW_MODELS_Imbalance/Bert/g_ex3_bert_multi_fed_data_epoch_2.pt', map_location=torch.device('cpu'))
158
+
159
+ return test_with_sentance(text , config.model_XLM , config.tokenizer_XLM)
160
+
161
+ # greet("sun")
162
+
163
+ interface = gr.Interface(
164
+
165
+ fn = _method,
166
+
167
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
168
+ outputs="label",
169
+ examples=config.EX_LIST,
170
+ live = True,
171
+
172
+
173
+ title = 'XLM Roberta Multilingual',
174
+
175
+ description=config.description,
176
+ article = '',
177
+
178
+ )
179
+ list_interface.append(interface)
180
+ list_title.append('XLM Roberta Multilingual')
181
+
182
+
183
+ # MMiniLM
184
+
185
+ def _method(text):
186
+
187
+ # tokenizer = AutoTokenizer.from_pretrained(config.CHECKPOINT_BERT)
188
+ # model_loaded = torch.load('/content/NEW_MODELS_Imbalance/Bert/g_ex3_bert_multi_fed_data_epoch_2.pt', map_location=torch.device('cpu'))
189
+
190
+ return test_with_sentance(text , config.model_MMiniLM , config.tokenizer_MMiniLM)
191
+
192
+ # greet("sun")
193
+
194
+ interface = gr.Interface(
195
+
196
+ fn = _method,
197
+
198
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
199
+ outputs="label",
200
+ examples=config.EX_LIST,
201
+ live = True,
202
+
203
+
204
+ title = 'MiniLM Multilingual',
205
+
206
+ description=config.description,
207
+ article = '',
208
+
209
+ )
210
+ list_interface.append(interface)
211
+ list_title.append('MiniLM Multilingual')
212
+
213
+
214
+
215
+ # Switch
216
+
217
+ API_URL_Switch = "https://api-inference.huggingface.co/models/Karim-Gamal/switch-base-8-finetuned-SemEval-2018-emojis-IID-Fed"
218
+ headers_Switch = {"Authorization": "Bearer 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"}
219
+
220
+
221
+ def query_Switch(payload):
222
+ response = requests.post(API_URL_Switch, headers=headers_Switch, json=payload)
223
+ return response.json()
224
+
225
+ query_Switch({ "inputs": 'test',})
226
+
227
+
228
+
229
+ def _method(text):
230
+ text = preprocess(text)
231
+ output_temp = query_Switch({
232
+ "inputs": text,
233
+ })
234
+
235
+ text_to_emoji = {'red' : '❤', 'face': '😍', 'joy':'😂', 'love':'💕', 'fire':'🔥', 'smile':'😊', 'sunglasses':'😎', 'sparkle':'✨', 'blue':'💙', 'kiss':'😘', 'camera':'📷', 'USA':'🇺🇸', 'sun':'☀' , 'purple':'💜', 'blink':'😉', 'hundred':'💯', 'beam':'😁', 'tree':'🎄', 'flash':'📸', 'tongue':'😜'}
236
+
237
+ # Extract the dictionary from the list
238
+ try:
239
+ # code that may raise an exception
240
+ d = output_temp[0]
241
+ except:
242
+ pass
243
+
244
+ # Extract the text from the 'generated_text' key
245
+ text = d['generated_text']
246
+
247
+ # my_dict = {}
248
+ # my_dict[str(text_to_emoji[text.split(' ')[0]])] = 0.99
249
+ return text_to_emoji[text.split(' ')[0]]
250
+
251
+
252
+ # greet("sun")
253
+
254
+ interface = gr.Interface(
255
+
256
+ fn = _method,
257
+
258
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
259
+ outputs="text",
260
+ examples=config.EX_LIST,
261
+ live = True,
262
+
263
+
264
+ title = 'Switch-Base-8',
265
+
266
+ description=config.description,
267
+ article = '',
268
+
269
+ )
270
+ list_interface.append(interface)
271
+ list_title.append('Switch-Base-8')
272
+
273
+
274
+
275
+ # About us
276
+
277
+ def _method(input_rating):
278
+
279
+ # tokenizer = AutoTokenizer.from_pretrained(config.CHECKPOINT_BERT)
280
+ # model_loaded = torch.load('/content/NEW_MODELS_Imbalance/Bert/g_ex3_bert_multi_fed_data_epoch_2.pt', map_location=torch.device('cpu'))
281
+
282
+ if input_rating <=2:
283
+ return {'🔥': 0.6, '✨': 0.3, '💯': 0.1}
284
+
285
+ elif input_rating <= 4 and input_rating >2:
286
+ return {'✨': 0.6, '😉': 0.3, '💯': 0.1}
287
+
288
+ elif input_rating >4:
289
+ return {'😍': 0.6, '💯': 0.3, '💕': 0.1}
290
+
291
+ # return test_with_sentance(text , config.model_loaded_bert_multi_NONIID , config.tokenizer_bert)
292
+
293
+ # greet("sun")
294
+
295
+ interface = gr.Interface(
296
+
297
+ fn = _method,
298
+
299
+ inputs=gr.Slider(1, 5, value=4),
300
+ outputs="label",
301
+ # examples=config.EX_LIST,
302
+ live = True,
303
+
304
+
305
+ title = 'About us',
306
+
307
+ description='We don\'t have sad emoji so our rating will always be great. 😂',
308
+
309
+ # CSS Source : https://codepen.io/bibiangel199/pen/warevP
310
+
311
+ article = config.article + '<!-- this is the markup. you can change the details (your own name, your own avatar etc.) but don’t change the basic structure! --> <div class="div_table_"> <table class="table"> <tr> <td><aside class="profile-card"> <div class="mask-shadow"></div> <header> <!-- here’s the avatar --> <a href="https://www.linkedin.com/in/hossam-amer-23b9329b/"> <img src="https://drive.google.com/uc?export=view&id=1-C_UIimeqbofJC_lldC7IQzIOX_OYRSn"> </a> <!-- the username --> <h1 style = " font-size:20px; padding:20px; color:#444; margin-bottom:5px; " >Dr. Hossam Amer</h1> <!-- and role or location --> <h2 style = " font-size:14px; color:#acacac; text- margin:0; " >Research Scientist at Microsoft</h2> </header> </aside></td> </tr> </table> </div> <div class="div_table_"> <table class="table"> <tr> <td><aside class="profile-card"> <div class="mask-shadow"></div> <header> <!-- here’s the avatar --> <a href="https://www.linkedin.com/in/ahmed-mohamed-gaber-143b25175/"> <img src="https://drive.google.com/uc?export=view&id=1OiGZwhL23PYhIJzQexYvPDFRrgUIprMj"> </a> <!-- the username --> <h1 style = " font-size:20px; padding:20px; color:#444; margin-bottom:5px; ">Ahmed Gaber</h1> <!-- and role or location --> <h2 style = " font-size:14px; color:#acacac; text- margin:0; " >Master\'s student at Queen\'s University</h2> </header> </aside></td> <td><aside class="profile-card"> <div class="mask-shadow"></div> <header> <!-- here’s the avatar --> <a href="https://www.linkedin.com/in/karim-gamal-mahmoud/"> <img src="https://drive.google.com/uc?export=view&id=1Lg2RzimITL9y__X2hycBTX10rJ4o87Ax"> </a> <!-- the username --> <h1 style=" font-size:20px; padding:20px; color:#444; margin-bottom:5px; ">Karim Gamal</h1> <!-- and role or location --> <h2 style = " font-size:14px; color:#acacac; text- margin:0; " >Master\'s student at Queen\'s University</h2> </header> </aside></td> </tr> </table> </div>',
312
+ )
313
+ list_interface.append(interface)
314
+ list_title.append('About us')
315
+
316
+
317
+
318
+ demo = gr.TabbedInterface(
319
+ list_interface,
320
+ list_title,
321
+ title='Multilingual Emoji Prediction Using Federated Learning',
322
+ css='.gradio-container {color : orange}',)
323
+ # css='.gradio-container {background-color: white; color : orange}',)
324
+ demo.launch()
325
+
326
+
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ transformers[sentencepiece]
2
+ argparse
3
+ torch
4
+ scipy
5
+ python-csv
6
+ urllib3
7
+ numpy