Spaces:
Running
Running
File size: 3,111 Bytes
1c43bb7 1cfe230 1c43bb7 1cfe230 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import re
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
system_instructions = """<s> [INST] These are the instructions: You will be provided with text, and your task is to translate it into emojis. DO NOT USE ANY REGULAR TEXT. Do your best with emojis only. Translate this text: """
def generate_translation(prompt):
generate_kwargs = dict(
temperature=0.5,
max_new_tokens=1024,
top_p=0.95,
repetition_penalty=1.0,
do_sample=True,
seed=42,
)
formatted_prompt = system_instructions + prompt + "[/INST]"
stream = client.text_generation(
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
emoji_pattern = r"[^\u0021-\u007E\u00A0-\uD7FF\uE000-\uFDCF\uFF00-\uFFEF\u10000-\u10FFFF\u0300-\u036F\u1F00-\u1F1F\u1F20-\u1F7F\u2600-\u26FF\u2700-\u27BF]+?"
filtered_output = re.findall(emoji_pattern, output)
return ''.join(filtered_output).replace(" ", "").replace("\n", "")
system_instructions_emoji_input = """<s> [INST] You will be provided with emojis, and your task is to create a short story from it. DO NOT USE ANY EMOJIS. Do your best with text only. Translate this emojis: """
def generate_emoji_translation(prompt):
generate_kwargs = dict(
temperature=0.5,
max_new_tokens=1024,
top_p=0.95,
repetition_penalty=1.0,
do_sample=True,
seed=42,
)
formatted_prompt = system_instructions_emoji_input + prompt + "[/INST]"
stream = client.text_generation(
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
yield output.rsplit("<", 1)[0]
return output.rsplit("<", 1)[0]
with gr.Blocks() as demo:
gr.HTML("""
<center><h1>Emoji Translator π€π»</h1>
<h3>Translate any text into emojis, and vice versa!</h3>
Original space: https://huggingface.co/spaces/gospacedev/emoji-translator
</center>
""")
gr.Markdown("""
# Text to Emoji πβ‘οΈπ»
""")
with gr.Row():
text_uesr_input = gr.Textbox(label="Enter text π")
output = gr.Textbox(label="Translation")
with gr.Row():
translate_btn = gr.Button("Translate π")
translate_btn.click(fn=generate_translation, inputs=text_uesr_input,
outputs=output, api_name="translate_text")
gr.Markdown("""
# Emoji to Text π»β‘οΈπ
""")
with gr.Row():
emoji_user_input = gr.Textbox(label="Enter emojis π€")
output = gr.Textbox(label="Translation")
with gr.Row():
translate_btn = gr.Button("Translate π")
translate_btn.click(fn=generate_emoji_translation, inputs=emoji_user_input,
outputs=output, api_name="translate_emojis")
if __name__ == "__main__":
demo.launch(show_api=True, share=True, debug=True)
|