gamino-instructor commited on
Commit
1c43bb7
β€’
1 Parent(s): 561ea5b

Adding an example Gradio app

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