mibrahimzia commited on
Commit
336902e
·
verified ·
1 Parent(s): f359a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py CHANGED
@@ -179,6 +179,24 @@ Words: {len(text.split()):,}
179
  Tokens: {result:,}
180
  """
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  # ---------- UI ----------
183
  with gr.Blocks(title="Advanced LLM Token Counter") as demo:
184
  gr.Markdown("## Advanced LLM Token Counter\nUpload a file or paste text to count tokens for various LLMs.")
@@ -209,5 +227,12 @@ with gr.Blocks(title="Advanced LLM Token Counter") as demo:
209
  inputs=[provider, text_input, file_input],
210
  outputs=output
211
  )
 
 
 
 
 
 
 
212
 
213
  demo.launch()
 
179
  Tokens: {result:,}
180
  """
181
 
182
+ def count_tokens_for_input(provider, text_input_val):
183
+ """Function to count tokens when text is entered directly"""
184
+ if not text_input_val.strip():
185
+ return "No input text."
186
+
187
+ counter = PROVIDERS[provider]
188
+ result = counter(text_input_val)
189
+
190
+ if isinstance(result, str): # Error message
191
+ return result
192
+
193
+ return f"""
194
+ Provider: {provider}
195
+ Characters: {len(text_input_val):,}
196
+ Words: {len(text_input_val.split()):,}
197
+ Tokens: {result:,}
198
+ """
199
+
200
  # ---------- UI ----------
201
  with gr.Blocks(title="Advanced LLM Token Counter") as demo:
202
  gr.Markdown("## Advanced LLM Token Counter\nUpload a file or paste text to count tokens for various LLMs.")
 
227
  inputs=[provider, text_input, file_input],
228
  outputs=output
229
  )
230
+
231
+ # Add event for real-time token counting as user types
232
+ text_input.change(
233
+ fn=count_tokens_for_input,
234
+ inputs=[provider, text_input],
235
+ outputs=output
236
+ )
237
 
238
  demo.launch()