Spaces:
Sleeping
Sleeping
File size: 1,394 Bytes
bdd87a0 08e204f bdd87a0 08e204f bdd87a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import gradio as gr
from tokencost import count_message_tokens, count_string_tokens, calculate_prompt_cost, calculate_completion_cost
def compute_all(prompt_string, completion_string, model):
prompt_cost = calculate_prompt_cost(prompt_string, model)
completion_cost = calculate_completion_cost(completion_string, model)
prompt_tokens = count_string_tokens(prompt_string, model)
completion_tokens = count_string_tokens(completion_string, model)
return prompt_tokens, prompt_cost, completion_tokens, completion_cost
with gr.Blocks(theme='soft') as demo:
with gr.Row():
with gr.Column():
prompt = gr.Textbox(value="Hello world")
completion = gr.Textbox(value="...")
model = gr.Dropdown(value="gpt-3.5-turbo", choices=["gpt-3.5-turbo", "gpt-3.5-turbo"])
button = gr.Button("Compute costs!")
with gr.Column():
with gr.Row():
prompt_tokens = gr.Textbox(interactive=False)
prompt_cost = gr.Textbox(interactive=False)
with gr.Row():
completion_tokens = gr.Textbox(interactive=False)
completion_cost = gr.Textbox(interactive=False)
button.click(compute_all, inputs=[prompt, completion, model], outputs=[prompt_tokens, prompt_cost, completion_tokens, completion_cost])
if __name__ == "__main__":
demo.launch() |