prasanna kumar commited on
Commit
6e43644
β€’
1 Parent(s): b9a925b

text box added and decoding issue fixed

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -25,23 +25,29 @@ def create_vertical_histogram(data, title):
25
  )
26
  return fig
27
 
28
- def process_input(input_type, input_value, model_name):
 
 
 
 
 
 
29
  tokenizer = AutoTokenizer.from_pretrained(model_path + model_name)
 
 
 
 
 
 
30
 
31
  if input_type == "Text":
32
- text = input_value
33
  elif input_type == "Token IDs":
34
- try:
35
- token_ids = ast.literal_eval(input_value)
36
- text = tokenizer.decode(token_ids)
37
- except ValueError:
38
- return "Error", "Invalid input", "", "", "", None, None, None
39
 
40
  character_count = len(text)
41
  word_count = len(text.split())
42
 
43
- token_ids = tokenizer.encode(text, add_special_tokens=True)
44
- tokens = tokenizer.convert_ids_to_tokens(token_ids)
45
 
46
  space_count = sum(1 for token in tokens if token == '▁')
47
  special_char_count = sum(1 for token in tokens if not token.isalnum() and token != '▁')
@@ -65,7 +71,7 @@ def process_input(input_type, input_value, model_name):
65
  analysis += f"Special character tokens: {special_char_count}\n"
66
  analysis += f"Other tokens: {len(tokens) - space_count - special_char_count}"
67
 
68
- return analysis, " ".join(tokens), str(token_ids), words_hist, special_chars_hist, numbers_hist
69
 
70
  def text_example():
71
  return "Hello, world! This is an example text input for tokenization."
@@ -90,6 +96,7 @@ with gr.Blocks() as iface:
90
  submit_button = gr.Button("Process")
91
 
92
  analysis_output = gr.Textbox(label="Analysis", lines=6)
 
93
  tokens_output = gr.Textbox(label="Tokens", lines=3)
94
  token_ids_output = gr.Textbox(label="Token IDs", lines=2)
95
 
@@ -111,7 +118,7 @@ with gr.Blocks() as iface:
111
  submit_button.click(
112
  process_input,
113
  inputs=[input_type, input_text, model_name],
114
- outputs=[analysis_output, tokens_output, token_ids_output, words_plot, special_chars_plot, numbers_plot]
115
  )
116
 
117
  if __name__ == "__main__":
 
25
  )
26
  return fig
27
 
28
+ def process_text(text:str,model_name):
29
+ tokenizer = AutoTokenizer.from_pretrained(model_path + model_name)
30
+ token_ids = tokenizer.encode(text, add_special_tokens=True)
31
+ tokens = tokenizer.convert_ids_to_tokens(token_ids)
32
+ return text,tokens,token_ids,
33
+
34
+ def process_ids(ids:str,model_name):
35
  tokenizer = AutoTokenizer.from_pretrained(model_path + model_name)
36
+ token_ids = ast.literal_eval(ids)
37
+ text = tokenizer.decode(token_ids)
38
+ tokens = tokenizer.convert_ids_to_tokens(token_ids)
39
+ return text,tokens,token_ids
40
+
41
+ def process_input(input_type, input_value, model_name):
42
 
43
  if input_type == "Text":
44
+ text,tokens,token_ids = process_text(text=input_value,model_name=model_name)
45
  elif input_type == "Token IDs":
46
+ text,tokens,token_ids = process_ids(ids=input_value,model_name=model_name)
 
 
 
 
47
 
48
  character_count = len(text)
49
  word_count = len(text.split())
50
 
 
 
51
 
52
  space_count = sum(1 for token in tokens if token == '▁')
53
  special_char_count = sum(1 for token in tokens if not token.isalnum() and token != '▁')
 
71
  analysis += f"Special character tokens: {special_char_count}\n"
72
  analysis += f"Other tokens: {len(tokens) - space_count - special_char_count}"
73
 
74
+ return analysis, text,tokens, str(token_ids), words_hist, special_chars_hist, numbers_hist
75
 
76
  def text_example():
77
  return "Hello, world! This is an example text input for tokenization."
 
96
  submit_button = gr.Button("Process")
97
 
98
  analysis_output = gr.Textbox(label="Analysis", lines=6)
99
+ text_output = gr.Textbox(label="Text",lines=6)
100
  tokens_output = gr.Textbox(label="Tokens", lines=3)
101
  token_ids_output = gr.Textbox(label="Token IDs", lines=2)
102
 
 
118
  submit_button.click(
119
  process_input,
120
  inputs=[input_type, input_text, model_name],
121
+ outputs=[analysis_output,text_output ,tokens_output, token_ids_output, words_plot, special_chars_plot, numbers_plot]
122
  )
123
 
124
  if __name__ == "__main__":