#### pip install advertools #### pip install emoji #### pip install emoji-chengyu #### pip install gradio-client #### prefer to run in chorme, others may have problem in change hock function import gradio as gr import pandas as pd import emoji from advertools.emoji import emoji_df from copy import deepcopy import numpy as np from emoji_chengyu.data import DefaultChengyuManager from emoji_chengyu.puzzle import make_one_puzzle, gen_puzzle from Lex import * ''' lex = Lexica(query="man woman fire snow").images() ''' from PIL import Image import requests from zipfile import ZipFile from time import sleep sleep_time = 0.5 from gradio_client import Client llm_client = Client("https://svjack-wizardlm-13b-ggml.hf.space/--replicas/bnqpc/") ''' llm_result = llm_client.predict( "Use following emojis to generate a short description of a scene , the emojis are π¨π©π₯βοΈ", # str in 'Question/Instruction' Textbox component 0.8, # int | float (numeric value between 0.1 and 1.0) in 'Temperature' Slider component 0.95, # int | float (numeric value between 0.0 and 1.0) in 'Top-p (nucleus sampling)' Slider component 40, # int | float (numeric value between 5 and 80) in 'Top-k' Slider component 256, # int | float (numeric value between 0 and 1024) in 'Maximum new tokens' Slider component 52, # int | float in 'Seed' Number component fn_index=1 ) ''' def run_llm_client(llm_client, prompt): llm_result = llm_client.predict( prompt, # str in 'Question/Instruction' Textbox component 0.8, # int | float (numeric value between 0.1 and 1.0) in 'Temperature' Slider component 0.95, # int | float (numeric value between 0.0 and 1.0) in 'Top-p (nucleus sampling)' Slider component 40, # int | float (numeric value between 5 and 80) in 'Top-k' Slider component 256, # int | float (numeric value between 0 and 1024) in 'Maximum new tokens' Slider component 52, # int | float in 'Seed' Number component fn_index=1 ) return llm_result def min_dim_to_size(img, size = 512): h, w = img.size ratio = size / max(h, w) h, w = map(lambda x: int(x * ratio), [h, w]) return ( ratio ,img.resize((h, w)) ) def lexica(prompt, limit_size = 128, ratio_size = 256 + 128): if not prompt or not prompt.strip(): return [] prompt = prompt.strip() lex = Lexica(query=prompt).images() lex = lex[:limit_size] lex = list(map(lambda x: x.replace("full_jpg", "sm2"), lex)) lex_ = [] for ele in lex: try: im = Image.open( requests.get(ele, stream = True).raw ) lex_.append(im) except: print("err") sleep(sleep_time) assert lex_ lex = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex_)) return lex def search(emoji_outputs, emoji2text_or_not, llm_prompt_input, llm_client = llm_client): assert emoji2text_or_not in ["Emoji to Text", "Only Emoji"] if emoji2text_or_not == "Only Emoji": l = lexica(emoji_outputs) return (l, "") else: assert "{}" in llm_prompt_input llm_prompt = llm_prompt_input.format(emoji_outputs) llm_output = run_llm_client(llm_client, llm_prompt) tail_list = ["someone do something"] for tail in tail_list: if tail in llm_output and len(llm_output.split(tail)[-1]) > (5 * 3): llm_output = llm_output.split(tail)[-1] l = lexica(llm_output) return (l, llm_output) def enterpix(prompt, limit_size = 100, ratio_size = 256 + 128, use_key = "bigThumbnailUrl"): resp = requests.post( url = "https://www.enterpix.app/enterpix/v1/image/prompt-search", data= { "length": limit_size, "platform": "stable-diffusion,midjourney", "prompt": prompt, "start": 0 } ) resp = resp.json() resp = list(map(lambda x: x[use_key], resp["images"])) lex_ = [] for ele in resp: try: im = Image.open( requests.get(ele, stream = True).raw ) lex_.append(im) except: print("err") sleep(sleep_time) assert lex_ resp = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex_)) return resp def zip_ims(g): from uuid import uuid1 if g is None: return None l = list(map(lambda x: x["name"], g)) if not l: return None zip_file_name ="tmp.zip" with ZipFile(zip_file_name ,"w") as zipObj: for ele in l: zipObj.write(ele, "{}.png".format(uuid1())) #zipObj.write(file2.name, "file2") return zip_file_name emoji_order_list = [ ["π", ["π₯’","πΌ", "π±", "π", "π¦"]], ["π", ["π₯°", "π", "πΊ", "π", "π©"]], ["π΅", ["π΅", "π¦", "π", "π³"]], ["π", ["π" ,"π", "πͺ", "π", "π₯"]], ["π", ["β½", "π", "π―", "π", "ποΈ"]], #["π", ["π", "π "οΈ, "β²", "π"οΈ]], ["π", ["π", "πͺ", "π", "π",]], ["π", ["π", "β²", "π ",]], ] sub_cate_num = 5 sub_cate_size = 36 sub_col_num = 6 def list_to_square(l, col_num = 10): assert type(l) == type([]) row_num = len(l) // col_num res = len(l) % col_num if res > 0: res_for_add = col_num - res else: res_for_add = 0 ll = np.asarray(l).reshape([-1, col_num]).tolist() return ll l_ = deepcopy(l) + [""] * res_for_add return list_to_square(l_, col_num) def append_emojis(selected_index: gr.SelectData, dataframe_origin, emoji_prompt): val = dataframe_origin.iloc[selected_index.index[0], selected_index.index[1]] if val.strip(): emoji_prompt = emoji_prompt + val return emoji_prompt def append_chengyu_emojis(selected_index: gr.SelectData, dataframe_origin, emoji_prompt, append_or_replace): val = dataframe_origin.iloc[selected_index.index[0], selected_index.index[1]] if type(val) != type("") or not val: return emoji_prompt assert append_or_replace in ["replace", "append"] if append_or_replace == "append": emoji_prompt = emoji_prompt + val.split(":")[-1] else: emoji_prompt = val.split(":")[-1] return emoji_prompt def extract_emojis(s): #return ''.join(c for c in s if c in emoji.UNICODE_EMOJI['en']) dl = emoji.emoji_list(s + "s") return "".join(map(lambda x: x["emoji"], dl)) def gen_emojis_by_chengyu(words): assert type(words) == type("") out = DefaultChengyuManager.get_by_word(words) if out is None: return "" out = "".join(make_one_puzzle(out).puzzle) out = extract_emojis(out) return out def gen_emojis_by_sample(search_count=5000): pg = gen_puzzle(manager=DefaultChengyuManager, search_count=search_count) df = pd.DataFrame(list(map(lambda x: { "words": "".join(x.chengyu_item.word_list), "emoji": x.puzzle_str, "score": sum(x.mask) } ,pg))) df = df[df["score"] == 4] df = df[df["words"].map(lambda x: len(x) == 4)] req = [] col0 = set([]) col1 = set([]) col2 = set([]) col3 = set([]) for i, r in df.iterrows(): words = r["words"] emoji = r["emoji"] if emoji[0] in col0: continue col0.add(emoji[0]) if emoji[1] in col1: continue col1.add(emoji[1]) if emoji[2] in col2: continue col2.add(emoji[2]) if emoji[3] in col3: continue col3.add(emoji[3]) req.append( r.to_dict() ) df = pd.DataFrame(req) if len(df) < 21: return gen_emojis_by_sample(search_count=search_count) df = pd.DataFrame( np.asarray(df.apply(lambda x: x.to_dict(), axis = 1).head(21).map(lambda x: "{}:{}".format(x["words"],x["emoji"]) ).tolist()).reshape( (7, 3) ) ) return df def append_pure_to_input(emoji_outputs ,only_emoji_outputs): return emoji_outputs + only_emoji_outputs css = """ #frame span{ font-size: 1.5em; display: flex; align-items: center; } """ ###with gr.Blocks(css="custom.css") as demo: with gr.Blocks(css = css) as demo: title = gr.HTML( """