# !pip install argparse import gradio as gr import requests import argparse args_dict = dict( EX_LIST = [["This is wonderful!"], ["Nice car"], ["La France est la meilleure équipe du monde"], ["Visca Barca"], ["Hala Madrid"], ["Buongiorno"], # ["Auf einigen deutschen Straßen gibt es kein Radar"], ["Tempo soleggiato in Italia"], ["Bonjour"], ["صباح الخير"], ["اكل زوجتي جميل"], ], description = 'Real-time Emoji Prediction', article = '', ) config = argparse.Namespace(**args_dict) list_interface = [] list_title = [] # Preprocess text (username and link placeholders) def preprocess(text): text = text.lower() new_text = [] for t in text.split(" "): t = '@user' if t.startswith('@') and len(t) > 1 else t t = '' if t.startswith('http') else t new_text.append(t) # print(" ".join(new_text)) return " ".join(new_text) # the MMiniLM model API_URL_MMiniLM = "https://api-inference.huggingface.co/models/Karim-Gamal/MMiniLM-L12-finetuned-emojis-IID-Fed" headers_MMiniLM = {"Authorization": "Bearer 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"} def query_MMiniLM(payload): response = requests.post(API_URL_MMiniLM, headers=headers_MMiniLM, json=payload) return response.json() query_MMiniLM({ "inputs": 'test',}) def _method(text): text = preprocess(text) output_temp = query_MMiniLM({ "inputs": text, }) # output_dict = {d['label']: d['score'] for d in output_temp[0]} if output_temp: try: output_dict = {d['label']: d['score'] for d in output_temp[0]} except: pass else: # handle the case where output_temp is empty output_dict = {} input_list = list(output_dict.items())[:3] output_dict = {key: value for key, value in input_list} return output_dict # greet("sun") interface = gr.Interface( fn = _method, inputs=gr.Textbox(placeholder="Enter sentence here..."), outputs="label", examples=config.EX_LIST, live = True, title = 'MiniLM Multilingual', description=config.description, article = '', ) list_interface.append(interface) list_title.append('MiniLM Multilingual') # the XLM model API_URL_XLM = "https://api-inference.huggingface.co/models/Karim-Gamal/XLM-Roberta-finetuned-emojis-IID-Fed" headers_XLM = {"Authorization": "Bearer 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"} def query_XLM(payload): response = requests.post(API_URL_XLM, headers=headers_XLM, json=payload) return response.json() query_XLM({ "inputs": 'test',}) def _method(text): text = preprocess(text) output_temp = query_XLM({ "inputs": text, }) # output_dict = {d['label']: d['score'] for d in output_temp[0]} if output_temp: output_dict = {d['label']: d['score'] for d in output_temp[0]} else: output_dict = {} try: input_list = list(output_dict.items())[:3] except: pass output_dict = {key: value for key, value in input_list} return output_dict # greet("sun") interface = gr.Interface( fn = _method, inputs=gr.Textbox(placeholder="Enter sentence here..."), outputs="label", examples=config.EX_LIST, live = True, title = 'XLM Roberta Multilingual', description=config.description, article = '', ) list_interface.append(interface) list_title.append('XLM Roberta Multilingual') # the bert model API_URL_BERT = "https://api-inference.huggingface.co/models/Karim-Gamal/BERT-base-finetuned-emojis-IID-Fed" headers_BERT = {"Authorization": "Bearer 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"} def query_BERT(payload): response = requests.post(API_URL_BERT, headers=headers_BERT, json=payload) return response.json() query_BERT({ "inputs": 'test',}) def _method(text): text = preprocess(text) output_temp = query_BERT({ "inputs": text, }) # output_dict = {d['label']: d['score'] for d in output_temp[0]} if output_temp: try: output_dict = {d['label']: d['score'] for d in output_temp[0]} except: pass else: # handle the case where output_temp is empty output_dict = {} try: input_list = list(output_dict.items())[:3] except: pass output_dict = {key: value for key, value in input_list} return output_dict # greet("sun") interface = gr.Interface( fn = _method, inputs=gr.Textbox(placeholder="Enter sentence here..."), outputs="label", examples=config.EX_LIST, live = True, title = 'BERT Multilingual', description=config.description, article = '', ) list_interface.append(interface) list_title.append('BERT Multilingual') # the Switch API_URL_Switch = "https://api-inference.huggingface.co/models/Karim-Gamal/switch-base-8-finetuned-SemEval-2018-emojis-IID-Fed" headers_Switch = {"Authorization": "Bearer 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"} def query_Switch(payload): response = requests.post(API_URL_Switch, headers=headers_Switch, json=payload) return response.json() query_Switch({ "inputs": 'test',}) def _method(text): text = preprocess(text) output_temp = query_Switch({ "inputs": text, }) text_to_emoji = {'red' : '❤', 'face': '😍', 'joy':'😂', 'love':'💕', 'fire':'🔥', 'smile':'😊', 'sunglasses':'😎', 'sparkle':'✨', 'blue':'💙', 'kiss':'😘', 'camera':'📷', 'USA':'🇺🇸', 'sun':'☀' , 'purple':'💜', 'blink':'😉', 'hundred':'💯', 'beam':'😁', 'tree':'🎄', 'flash':'📸', 'tongue':'😜'} # Extract the dictionary from the list try: d = output_temp[0] except: pass # Extract the text from the 'generated_text' key text = d['generated_text'] # my_dict = {} # my_dict[str(text_to_emoji[text.split(' ')[0]])] = 0.99 return text_to_emoji[text.split(' ')[0]] # greet("sun") interface = gr.Interface( fn = _method, inputs=gr.Textbox(placeholder="Enter sentence here..."), outputs="text", examples=config.EX_LIST, live = True, title = 'Switch-Base-8', description=config.description, article = '', ) list_interface.append(interface) list_title.append('Switch-Base-8') import time # delay of 40 seconds time.sleep(40) def _method(input_rating): # tokenizer = AutoTokenizer.from_pretrained(config.CHECKPOINT_BERT) # model_loaded = torch.load('/content/NEW_MODELS_Imbalance/Bert/g_ex3_bert_multi_fed_data_epoch_2.pt', map_location=torch.device('cpu')) if input_rating <=2: return {'🔥': 0.6, '✨': 0.3, '💯': 0.1} elif input_rating <= 4 and input_rating >2: return {'✨': 0.6, '😉': 0.3, '💯': 0.1} elif input_rating >4: return {'😍': 0.6, '💯': 0.3, '💕': 0.1} # return test_with_sentance(text , config.model_loaded_bert_multi_NONIID , config.tokenizer_bert) # greet("sun") interface = gr.Interface( fn = _method, inputs=gr.Slider(1, 5, value=4), outputs="label", # examples=config.EX_LIST, live = True, title = 'About us', description='We don\'t have sad emoji so our rating will always be great. 😂', # CSS Source : https://codepen.io/bibiangel199/pen/warevP article = config.article + '
', ) list_interface.append(interface) list_title.append('About us') demo = gr.TabbedInterface( list_interface, list_title, title='Federated-Learning-Based-Multilingual-Emoji-Prediction', css='.gradio-container {color : orange}',) # css='.gradio-container {background-color: white; color : orange}',) demo.launch()