Karim-Gamal commited on
Commit
eca2f5f
·
1 Parent(s): e82206e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +331 -0
app.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !pip install argparse
2
+
3
+ import gradio as gr
4
+ import requests
5
+ import argparse
6
+
7
+ args_dict = dict(
8
+ EX_LIST = [["This is wonderful!"],
9
+ ["Nice car"],
10
+ ["La France est la meilleure équipe du monde"],
11
+ ["Visca Barca"],
12
+ ["Hala Madrid"],
13
+ ["Buongiorno"],
14
+ # ["Auf einigen deutschen Straßen gibt es kein Radar"],
15
+ ["Tempo soleggiato in Italia"],
16
+ ["Bonjour"],
17
+ ["صباح الخير"],
18
+ ["اكل زوجتي جميل"],
19
+ ],
20
+
21
+ description = 'Real-time Emoji Prediction',
22
+ 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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"> <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">\
23
+ .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>',
24
+
25
+
26
+ )
27
+
28
+ config = argparse.Namespace(**args_dict)
29
+
30
+
31
+
32
+
33
+ list_interface = []
34
+ list_title = []
35
+
36
+ # Preprocess text (username and link placeholders)
37
+ def preprocess(text):
38
+ text = text.lower()
39
+ new_text = []
40
+ for t in text.split(" "):
41
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
42
+ t = '' if t.startswith('http') else t
43
+ new_text.append(t)
44
+ # print(" ".join(new_text))
45
+ return " ".join(new_text)
46
+
47
+
48
+ # the MMiniLM model
49
+ API_URL_MMiniLM = "https://api-inference.huggingface.co/models/Karim-Gamal/MMiniLM-L12-finetuned-emojis-IID-Fed"
50
+ headers_MMiniLM = {"Authorization": "Bearer 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"}
51
+
52
+
53
+ def query_MMiniLM(payload):
54
+ response = requests.post(API_URL_MMiniLM, headers=headers_MMiniLM, json=payload)
55
+ return response.json()
56
+
57
+ query_MMiniLM({ "inputs": 'test',})
58
+
59
+
60
+ def _method(text):
61
+ text = preprocess(text)
62
+ output_temp = query_MMiniLM({
63
+ "inputs": text,
64
+ })
65
+
66
+ # output_dict = {d['label']: d['score'] for d in output_temp[0]}
67
+
68
+ if output_temp:
69
+ output_dict = {d['label']: d['score'] for d in output_temp[0]}
70
+ else:
71
+ # handle the case where output_temp is empty
72
+ output_dict = {}
73
+
74
+ input_list = list(output_dict.items())[:3]
75
+
76
+ output_dict = {key: value for key, value in input_list}
77
+
78
+ return output_dict
79
+
80
+
81
+ # greet("sun")
82
+
83
+ interface = gr.Interface(
84
+
85
+ fn = _method,
86
+
87
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
88
+ outputs="label",
89
+ examples=config.EX_LIST,
90
+ live = True,
91
+
92
+
93
+ title = 'MiniLM Multilingual',
94
+
95
+ description=config.description,
96
+ article = '',
97
+
98
+ )
99
+ list_interface.append(interface)
100
+ list_title.append('MiniLM Multilingual')
101
+
102
+
103
+
104
+ # the XLM model
105
+ API_URL_XLM = "https://api-inference.huggingface.co/models/Karim-Gamal/XLM-Roberta-finetuned-emojis-IID-Fed"
106
+ headers_XLM = {"Authorization": "Bearer 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"}
107
+
108
+
109
+ def query_XLM(payload):
110
+ response = requests.post(API_URL_XLM, headers=headers_XLM, json=payload)
111
+ return response.json()
112
+
113
+ query_XLM({ "inputs": 'test',})
114
+
115
+
116
+
117
+ def _method(text):
118
+ text = preprocess(text)
119
+ output_temp = query_XLM({
120
+ "inputs": text,
121
+ })
122
+
123
+ # output_dict = {d['label']: d['score'] for d in output_temp[0]}
124
+
125
+ if output_temp:
126
+
127
+ output_dict = {d['label']: d['score'] for d in output_temp[0]}
128
+ else:
129
+ # handle the case where output_temp is empty
130
+ output_dict = {}
131
+
132
+ input_list = list(output_dict.items())[:3]
133
+
134
+ output_dict = {key: value for key, value in input_list}
135
+
136
+ return output_dict
137
+
138
+
139
+ # greet("sun")
140
+
141
+ interface = gr.Interface(
142
+
143
+ fn = _method,
144
+
145
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
146
+ outputs="label",
147
+ examples=config.EX_LIST,
148
+ live = True,
149
+
150
+
151
+ title = 'XLM Roberta Multilingual',
152
+
153
+ description=config.description,
154
+ article = '',
155
+
156
+ )
157
+ list_interface.append(interface)
158
+ list_title.append('XLM Roberta Multilingual')
159
+
160
+
161
+
162
+
163
+ # the bert model
164
+ API_URL_BERT = "https://api-inference.huggingface.co/models/Karim-Gamal/BERT-base-finetuned-emojis-IID-Fed"
165
+ headers_BERT = {"Authorization": "Bearer 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"}
166
+
167
+
168
+ def query_BERT(payload):
169
+ response = requests.post(API_URL_BERT, headers=headers_BERT, json=payload)
170
+ return response.json()
171
+
172
+ query_BERT({ "inputs": 'test',})
173
+
174
+
175
+
176
+
177
+ def _method(text):
178
+ text = preprocess(text)
179
+ output_temp = query_BERT({
180
+ "inputs": text,
181
+ })
182
+
183
+ # output_dict = {d['label']: d['score'] for d in output_temp[0]}
184
+
185
+ if output_temp:
186
+ output_dict = {d['label']: d['score'] for d in output_temp[0]}
187
+ else:
188
+ # handle the case where output_temp is empty
189
+ output_dict = {}
190
+
191
+ input_list = list(output_dict.items())[:3]
192
+
193
+ output_dict = {key: value for key, value in input_list}
194
+
195
+ return output_dict
196
+
197
+
198
+ # greet("sun")
199
+
200
+ interface = gr.Interface(
201
+
202
+ fn = _method,
203
+
204
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
205
+ outputs="label",
206
+ examples=config.EX_LIST,
207
+ live = True,
208
+
209
+
210
+ title = 'BERT Multilingual',
211
+
212
+ description=config.description,
213
+ article = '',
214
+
215
+ )
216
+ list_interface.append(interface)
217
+ list_title.append('BERT Multilingual')
218
+
219
+
220
+
221
+
222
+ # the Switch
223
+ API_URL_Switch = "https://api-inference.huggingface.co/models/Karim-Gamal/switch-base-8-finetuned-SemEval-2018-emojis-IID-Fed"
224
+ headers_Switch = {"Authorization": "Bearer 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"}
225
+
226
+
227
+ def query_Switch(payload):
228
+ response = requests.post(API_URL_Switch, headers=headers_Switch, json=payload)
229
+ return response.json()
230
+
231
+ query_Switch({ "inputs": 'test',})
232
+
233
+
234
+
235
+
236
+ def _method(text):
237
+ text = preprocess(text)
238
+ output_temp = query_Switch({
239
+ "inputs": text,
240
+ })
241
+
242
+ text_to_emoji = {'red' : '❤', 'face': '😍', 'joy':'😂', 'love':'💕', 'fire':'🔥', 'smile':'😊', 'sunglasses':'😎', 'sparkle':'✨', 'blue':'💙', 'kiss':'😘', 'camera':'📷', 'USA':'🇺🇸', 'sun':'☀' , 'purple':'💜', 'blink':'😉', 'hundred':'💯', 'beam':'😁', 'tree':'🎄', 'flash':'📸', 'tongue':'😜'}
243
+
244
+ # Extract the dictionary from the list
245
+ d = output_temp[0]
246
+
247
+ # Extract the text from the 'generated_text' key
248
+ text = d['generated_text']
249
+
250
+ # my_dict = {}
251
+ # my_dict[str(text_to_emoji[text.split(' ')[0]])] = 0.99
252
+ return text_to_emoji[text.split(' ')[0]]
253
+
254
+
255
+ # greet("sun")
256
+
257
+ interface = gr.Interface(
258
+
259
+ fn = _method,
260
+
261
+ inputs=gr.Textbox(placeholder="Enter sentence here..."),
262
+ outputs="text",
263
+ examples=config.EX_LIST,
264
+ live = True,
265
+
266
+
267
+ title = 'Switch-Base-8',
268
+
269
+ description=config.description,
270
+ article = '',
271
+
272
+ )
273
+ list_interface.append(interface)
274
+ list_title.append('Switch-Base-8')
275
+
276
+
277
+
278
+ import time
279
+ # delay of 40 seconds
280
+ time.sleep(40)
281
+
282
+
283
+ def _method(input_rating):
284
+
285
+ # tokenizer = AutoTokenizer.from_pretrained(config.CHECKPOINT_BERT)
286
+ # model_loaded = torch.load('/content/NEW_MODELS_Imbalance/Bert/g_ex3_bert_multi_fed_data_epoch_2.pt', map_location=torch.device('cpu'))
287
+
288
+ if input_rating <=2:
289
+ return {'🔥': 0.6, '✨': 0.3, '💯': 0.1}
290
+
291
+ elif input_rating <= 4 and input_rating >2:
292
+ return {'✨': 0.6, '😉': 0.3, '💯': 0.1}
293
+
294
+ elif input_rating >4:
295
+ return {'😍': 0.6, '💯': 0.3, '💕': 0.1}
296
+
297
+ # return test_with_sentance(text , config.model_loaded_bert_multi_NONIID , config.tokenizer_bert)
298
+
299
+ # greet("sun")
300
+
301
+ interface = gr.Interface(
302
+
303
+ fn = _method,
304
+
305
+ inputs=gr.Slider(1, 5, value=4),
306
+ outputs="label",
307
+ # examples=config.EX_LIST,
308
+ live = True,
309
+
310
+
311
+ title = 'About us',
312
+
313
+ description='We don\'t have sad emoji so our rating will always be great. 😂',
314
+
315
+ # CSS Source : https://codepen.io/bibiangel199/pen/warevP
316
+
317
+ 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> <td><aside class="profile-card"> <div class="mask-shadow"></div> <header> <!-- here’s the avatar --> <a href="https://www.linkedin.com/in/yuanzhu-chen-3408265/"> <img src="https://drive.google.com/uc?export=view&id=1n5Vld5FJzp1TzTISM_fB69zPHAdfDYQn"> </a> <!-- the username --> <h1 style = " font-size:20px; padding:20px; color:#444; margin-bottom:5px; " >Dr. Yuanzhu Chen</h1> <!-- and role or location --> <h2 style = " font-size:14px; color:#acacac; text- margin:0; ">Professor at Queen\'s University</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/abdelrahman-elhamoly/"> <img src="https://drive.google.com/uc?export=view&id=1a9tR4Xd5XSTmx7Dy_WfRHbVCSpbgAoYb"> </a> <!-- the username --> <h1 style = " font-size:20px; padding:20px; color:#444; margin-bottom:5px; ">Abdelrahman El-Hamoly</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/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>',
318
+ )
319
+ list_interface.append(interface)
320
+ list_title.append('About us')
321
+
322
+
323
+
324
+
325
+ demo = gr.TabbedInterface(
326
+ list_interface,
327
+ list_title,
328
+ title='Federated-Learning-Based-Multilingual-Emoji-Prediction',
329
+ css='.gradio-container {color : orange}',)
330
+ # css='.gradio-container {background-color: white; color : orange}',)
331
+ demo.launch()