File size: 1,051 Bytes
cfef698
 
 
5542864
 
 
 
 
 
cfef698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9515f1c
 
cfef698
 
 
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
import gradio as gr
import requests

URL = "https://openrouter.ai/api/v1/models"

response = requests.get(URL)

print(response.json())

all_models = [a["name"] for a in response.json()["data"]]
name_to_dict = {a["name"]: a for a in response.json()["data"]}


def get_cost(model_name, input_token, output_token):
  model_dict = name_to_dict[model_name]
  prompt_cost = float(model_dict["pricing"]["prompt"])
  completion_cost = float(model_dict["pricing"]["completion"])

  context_length = float(model_dict["context_length"])

  # print(prompt_cost * input_token, completion_cost * output_token, context_length)
  return prompt_cost * min(context_length, input_token) + completion_cost * output_token

get_cost('Google: Gemini Pro 1.0', 1000, 1000)

demo = gr.Interface(
    fn=get_cost,
    inputs=[
        gr.Dropdown(choices=all_models),
        gr.Number(label="Input tokens"),
        gr.Number(label="Output tokens"),
    ],
    outputs="text",
    title="LLM Cost Calculator",
    description="Calculate the cost of a prompt",
)

demo.launch()