hk-kaden-kim commited on
Commit
7920d51
·
1 Parent(s): 7a500c2

Test unicode handling

Browse files
Files changed (2) hide show
  1. app.py +26 -4
  2. tools/__init__.py +30 -0
app.py CHANGED
@@ -1,7 +1,29 @@
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import tools
3
 
4
+ def generate(emoji):
5
+ output = ''
6
+
7
+ # Input handling
8
+ emoji = emoji.replace(' ','').split(',')
9
+ emoji= [s for s in emoji if s]
10
+
11
+ # Find a matched unicode
12
+ callback_u = []
13
+ for e in emoji:
14
+ u_emoji = tools.emoji_to_unicode(e)
15
+ output += f"{e} : {u_emoji}\n"
16
+ callback_u.append(u_emoji)
17
 
18
+ # Return unicodes back into the string
19
+ print('Test: ',callback_u)
20
+ output += "\nCallback Test\n"
21
+ for u in callback_u:
22
+ output += " " + tools.unicode_print(u)
23
+
24
+ return output
25
+
26
+ demo = gr.Interface(fn=generate, inputs="textbox", outputs="textbox")
27
+
28
+ if __name__ == "__main__":
29
+ demo.launch()
tools/__init__.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def unicode_print(unicode_str):
2
+ # Get an individual unicode
3
+ unicode_str_lst = unicode_str.split('_')
4
+
5
+ if len(unicode_str_lst) > 1: # Condition of reculsion
6
+ remains = '_'.join(unicode_str_lst[1:])
7
+ res = unicode_print(unicode_str_lst[0]) + unicode_print(remains)
8
+ return res
9
+ else:
10
+ # ----------Last Call-------------
11
+ try:
12
+ res = chr(int(unicode_str_lst[0], 16)) # Convert the unicode string to an actual character
13
+ return res
14
+ except ValueError:
15
+ print(f"Invalid unicode: {unicode_str_lst}")
16
+
17
+ def emoji_to_unicode(emoji):
18
+
19
+ if len(emoji) > 1: # Condition of reculsion
20
+ res = emoji_to_unicode(emoji[0]) + '_' + emoji_to_unicode(emoji[1:])
21
+ return res
22
+ else:
23
+ # ----------Last Call-------------
24
+ try:
25
+ code_point = ord(emoji) # Get the Unicode code point of the emoji
26
+ unicode_hex = hex(code_point)[2:] # Convert the code point to hexadecimal and remove the "0x" prefix
27
+ unicode_hex_padded = unicode_hex.zfill(4) # Pad with zeros to ensure a 4-character representation
28
+ return unicode_hex_padded.upper()
29
+ except Exception as e:
30
+ return f"Error: {str(e)}"