import gradio as gr import tiktoken encodings = tiktoken.list_encoding_names() encodings.reverse() def function(input, encoding): tokens = tiktoken.get_encoding(encoding).encode(input) return len(tokens) value1 = gr.Textbox(lines=6, label="Input", placeholder="Enter text here...") value2 = gr.Dropdown( label="Encoding", choices=encodings, value="cl100k_base", info="The encoding to use. (GPT-3.5 and GPT-4 use cl100k_base as their encoding.)" ) value3 = gr.Number(label="Output") # Don't include lines and placeholder examples = [ ["The only way to do great work is to love what you do. - Steve Jobs", "cl100k_base"], ["In the end, we will remember not the words of our enemies, but the silence of our friends. - Martin Luther King Jr.", "cl100k_base"], ["Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill", "cl100k_base"], ["The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela", "cl100k_base"], ["The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Helen Keller", "cl100k_base"] ] demo = gr.Interface( fn=function, inputs=[value1, value2], outputs=value3, title="ChatGPT Token Calculator", examples=examples, description="Calculate the number of tokens in a text string." ) demo.launch(debug=True)