johnnydevriese commited on
Commit
39bf943
1 Parent(s): c40106d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+
4
+ # Define the LLM models and their properties
5
+ models = {
6
+ "gpt-4o-2024-08-06": {
7
+ "input_price_per_1M": 2.50,
8
+ "output_price_per_1M": 10.00,
9
+ "max_input_tokens": 128_000,
10
+ },
11
+ "gpt-4o-mini-2024-07-18": {
12
+ "input_price_per_1M": 0.15,
13
+ "output_price_per_1M": 0.600,
14
+ "max_input_tokens": 128_000,
15
+ },
16
+ "Claude 3.5 Sonnet": {
17
+ "input_price_per_1M": 3.0,
18
+ "output_price_per_1M": 15.0,
19
+ "max_input_tokens": 200_000,
20
+ },
21
+ "GPT-3.5-turbo": {
22
+ "input_price_per_1M": 0.5,
23
+ "output_price_per_1M": 1.5,
24
+ "max_input_tokens": 4096,
25
+ },
26
+ "GPT-4": {
27
+ "input_price_per_1M": 30.0,
28
+ "output_price_per_1M": 60.0,
29
+ "max_input_tokens": 8192,
30
+ },
31
+ }
32
+
33
+ def calculate_cost(model, input_tokens, output_tokens, num_requests):
34
+ if model not in models:
35
+ return "Invalid model selected", 0, 0, 0
36
+
37
+ if input_tokens > models[model]["max_input_tokens"]:
38
+ return f"Input tokens exceed the maximum limit for {model}", 0, 0, 0
39
+
40
+ input_cost = (input_tokens / 1_000_000) * models[model]["input_price_per_1M"] * num_requests
41
+ output_cost = (output_tokens / 1_000_000) * models[model]["output_price_per_1M"] * num_requests
42
+ total_cost = input_cost + output_cost
43
+
44
+ return f"${total_cost:.6f}", input_cost, output_cost, total_cost
45
+
46
+ def compare_models(input_tokens, output_tokens, num_requests):
47
+ results = []
48
+ for model in models:
49
+ total_cost_str, input_cost, output_cost, total_cost = calculate_cost(
50
+ model, input_tokens, output_tokens, num_requests
51
+ )
52
+ results.append(
53
+ {
54
+ "Model": model,
55
+ "Input Cost": f"${input_cost:.6f}",
56
+ "Output Cost": f"${output_cost:.6f}",
57
+ "Total Cost": total_cost_str,
58
+ "Max Input Tokens": models[model]["max_input_tokens"],
59
+ "Input Price (1M)": f"${models[model]['input_price_per_1M']:.2f}",
60
+ "Output Price (1M)": f"${models[model]['output_price_per_1M']:.2f}",
61
+ }
62
+ )
63
+ return pd.DataFrame(results)
64
+
65
+ def create_interface():
66
+ with gr.Blocks() as interface:
67
+ gr.Markdown("# LLM Price Comparison Tool")
68
+ with gr.Row():
69
+ input_tokens = gr.Number(label="Input Tokens", value=100)
70
+ output_tokens = gr.Number(label="Output Tokens", value=100)
71
+ num_requests = gr.Number(label="Number of Requests", value=1, step=1)
72
+ compare_btn = gr.Button("Compare Models")
73
+ output_table = gr.DataFrame(label="Comparison Results")
74
+
75
+ compare_btn.click(
76
+ fn=compare_models,
77
+ inputs=[input_tokens, output_tokens, num_requests],
78
+ outputs=output_table,
79
+ )
80
+
81
+ return interface
82
+
83
+ # Create and launch the interface
84
+ demo = create_interface()
85
+
86
+ # Hugging Face specific launch
87
+ if __name__ == "__main__":
88
+ demo.launch()
89
+ else:
90
+ demo.launch(share=True)