File size: 1,563 Bytes
bdd87a0
7306a42
bdd87a0
7306a42
bdd87a0
08e204f
 
 
 
 
 
 
 
 
 
 
7306a42
 
 
08e204f
 
7306a42
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
37
38
39
40
import gradio as gr
import pandas as pd

model_data = pd.read_json("model_prices.json")

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:
    gr.Markdown("""### Text-to-$: calculate the price of your LLM runs
Based on data from the great [tokencost](https://github.com/AgentOps-AI/tokencost/blob/main/tokencost/model_prices.json).
""")
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(value="Lorem ipsum dolor sit amet...")
            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()