File size: 15,640 Bytes
02e476c
2997c9b
 
 
46a2215
02e476c
f834afc
2997c9b
f834afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2997c9b
02e476c
2997c9b
f834afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2997c9b
02e476c
2997c9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c309035
 
 
 
2997c9b
 
229a3d1
 
c309035
2997c9b
229a3d1
2997c9b
229a3d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2997c9b
c309035
 
f834afc
 
229a3d1
 
41a2f73
c309035
 
 
 
 
 
229a3d1
c309035
229a3d1
 
 
c309035
 
 
2997c9b
 
c309035
f834afc
 
2997c9b
c309035
f834afc
 
c309035
 
f834afc
c309035
 
f834afc
 
 
 
c309035
e26b1a8
229a3d1
 
 
f834afc
c309035
229a3d1
 
f834afc
229a3d1
 
 
f834afc
 
229a3d1
f834afc
229a3d1
 
 
 
f834afc
c309035
229a3d1
 
f834afc
229a3d1
 
f834afc
 
229a3d1
f834afc
229a3d1
 
 
 
 
 
 
 
 
 
f834afc
229a3d1
 
f834afc
 
 
 
229a3d1
f834afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229a3d1
46a2215
c309035
f834afc
46a2215
 
 
 
 
 
f834afc
46a2215
 
 
 
f834afc
46a2215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c309035
229a3d1
 
f834afc
229a3d1
 
 
 
 
c309035
2997c9b
229a3d1
 
 
 
 
4a3be07
 
 
 
229a3d1
 
f834afc
229a3d1
 
 
 
 
 
4a3be07
b185736
 
f834afc
b185736
4a3be07
b185736
 
4a3be07
b185736
f834afc
 
229a3d1
46a2215
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import gradio as gr
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

# Updated pricing data - restructured for better comparison
aws_instances = {
    # T4 GPU Instances (entry level)
    "g4dn.xlarge": {"vcpus": 4, "memory": 16, "gpu": "1x NVIDIA T4", "hourly_rate": 0.526, "gpu_memory": "16GB", "tier": "Entry"},
    "g4dn.2xlarge": {"vcpus": 8, "memory": 32, "gpu": "1x NVIDIA T4", "hourly_rate": 0.752, "gpu_memory": "16GB", "tier": "Entry"},
    
    # A10G GPU Instances (mid-tier)
    "g5.xlarge": {"vcpus": 4, "memory": 16, "gpu": "1x NVIDIA A10G", "hourly_rate": 0.65, "gpu_memory": "24GB", "tier": "Mid"},
    "g5.2xlarge": {"vcpus": 8, "memory": 32, "gpu": "1x NVIDIA A10G", "hourly_rate": 1.006, "gpu_memory": "24GB", "tier": "Mid"},
    
    # V100 GPU Instances (high-tier)
    "p3.2xlarge": {"vcpus": 8, "memory": 61, "gpu": "1x NVIDIA V100", "hourly_rate": 3.06, "gpu_memory": "16GB", "tier": "High"},
    
    
    # Added comparable instances to match GCP
    "p4d.xlarge": {"vcpus": 12, "memory": 85, "gpu": "1x NVIDIA A100", "hourly_rate": 4.10, "gpu_memory": "40GB", "tier": "Premium"},
    "p4d.2xlarge": {"vcpus": 24, "memory": 170, "gpu": "2x NVIDIA A100", "hourly_rate": 8.20, "gpu_memory": "2x40GB", "tier": "Premium"},
    "p4d.4xlarge": {"vcpus": 48, "memory": 340, "gpu": "4x NVIDIA A100", "hourly_rate": 16.40, "gpu_memory": "4x40GB", "tier": "Premium"},
}

gcp_instances = {
    # T4 GPU Instances (entry level)
    "n1-standard-4-t4": {"vcpus": 4, "memory": 15, "gpu": "1x NVIDIA T4", "hourly_rate": 0.49, "gpu_memory": "16GB", "tier": "Entry"},
    "n1-standard-8-t4": {"vcpus": 8, "memory": 30, "gpu": "1x NVIDIA T4", "hourly_rate": 0.69, "gpu_memory": "16GB", "tier": "Entry"},
    
    # L4 GPU Instances (mid-tier)
    "g2-standard-4": {"vcpus": 4, "memory": 16, "gpu": "1x NVIDIA L4", "hourly_rate": 0.59, "gpu_memory": "24GB", "tier": "Mid"},
    "g2-standard-8": {"vcpus": 8, "memory": 32, "gpu": "1x NVIDIA L4", "hourly_rate": 0.89, "gpu_memory": "24GB", "tier": "Mid"},
    
    # Added comparable V100 instance
    "n1-standard-8-v100": {"vcpus": 8, "memory": 60, "gpu": "1x NVIDIA V100", "hourly_rate": 2.95, "gpu_memory": "16GB", "tier": "High"},
    
    # A100 GPU Instances (premium)
    "a2-highgpu-1g": {"vcpus": 12, "memory": 85, "gpu": "1x NVIDIA A100", "hourly_rate": 1.46, "gpu_memory": "40GB", "tier": "Premium"},
    "a2-highgpu-2g": {"vcpus": 24, "memory": 170, "gpu": "2x NVIDIA A100", "hourly_rate": 2.93, "gpu_memory": "2x40GB", "tier": "Premium"},
    "a2-highgpu-4g": {"vcpus": 48, "memory": 340, "gpu": "4x NVIDIA A100", "hourly_rate": 5.86, "gpu_memory": "4x40GB", "tier": "Premium"},
}

api_pricing = {
    "OpenAI": {
        "GPT-3.5-Turbo": {"input_per_1M": 0.5, "output_per_1M": 1.5, "token_context": 16385},
        "GPT-4o": {"input_per_1M": 5.0, "output_per_1M": 15.0, "token_context": 32768},
        "GPT-4o-mini": {"input_per_1M": 2.5, "output_per_1M": 7.5, "token_context": 32768},
    },
    "TogetherAI": {
        "Llama-3-8B": {"input_per_1M": 0.15, "output_per_1M": 0.15, "token_context": 8192},
        "Llama-3-70B": {"input_per_1M": 0.9, "output_per_1M": 0.9, "token_context": 8192},
        "Llama-2-13B": {"input_per_1M": 0.6, "output_per_1M": 0.6, "token_context": 4096},
        "Llama-2-70B": {"input_per_1M": 2.5, "output_per_1M": 2.5, "token_context": 4096},
        "DeepSeek-Coder-33B": {"input_per_1M": 2.0, "output_per_1M": 2.0, "token_context": 16384},
    },
    "Anthropic": {
        "Claude-3-Opus": {"input_per_1M": 15.0, "output_per_1M": 75.0, "token_context": 200000},
        "Claude-3-Sonnet": {"input_per_1M": 3.0, "output_per_1M": 15.0, "token_context": 200000},
        "Claude-3-Haiku": {"input_per_1M": 0.25, "output_per_1M": 1.25, "token_context": 200000},
    }
}

model_sizes = {
    "Small (7B parameters)": {"memory_required": 14},
    "Medium (13B parameters)": {"memory_required": 26},
    "Large (70B parameters)": {"memory_required": 140},
    "XL (180B parameters)": {"memory_required": 360},
}

def calculate_aws_cost(instance, hours, storage, reserved=False, spot=False, years=1):
    data = aws_instances[instance]
    rate = data['hourly_rate']
    if spot:
        rate *= 0.3
    elif reserved:
        factors = {1: 0.6, 3: 0.4}
        rate *= factors.get(years, 0.6)
    compute = rate * hours
    storage_cost = storage * 0.10
    return {'total_cost': compute + storage_cost, 'details': data}

def calculate_gcp_cost(instance, hours, storage, reserved=False, spot=False, years=1):
    data = gcp_instances[instance]
    rate = data['hourly_rate']
    if spot:
        rate *= 0.2
    elif reserved:
        factors = {1: 0.7, 3: 0.5}
        rate *= factors.get(years, 0.7)
    compute = rate * hours
    storage_cost = storage * 0.04
    return {'total_cost': compute + storage_cost, 'details': data}

def calculate_api_cost(provider, model, in_tokens, out_tokens, calls):
    m = api_pricing[provider][model]
    input_cost = in_tokens * m['input_per_1M'] / 1000000
    output_cost = out_tokens * m['output_per_1M'] / 1000000
    call_cost = calls * 0.0001 if provider == 'TogetherAI' else 0
    return {'total_cost': input_cost + output_cost + call_cost, 'details': m}

def filter_compatible(instances, min_mem):
    res = {}
    for name, data in instances.items():
        mem_str = data['gpu_memory']
        if 'x' in mem_str and not mem_str.startswith(('1x','2x','4x','8x')):
            val = int(mem_str.replace('GB',''))
        elif 'x' in mem_str:
            parts = mem_str.split('x')
            val = int(parts[0]) * int(parts[1].replace('GB',''))
        else:
            val = int(mem_str.replace('GB',''))
        if val >= min_mem:
            res[name] = data
    return res

def generate_cost_comparison(
    compute_hours, tokens_per_month, input_ratio, api_calls,
    model_size, storage_gb, reserved_instances, spot_instances, multi_year_commitment,
    comparison_tier
):
    years = int(multi_year_commitment)
    in_tokens = tokens_per_month * 1000000 * (input_ratio/100)
    out_tokens = tokens_per_month * 1000000 - in_tokens
    min_mem = model_sizes[model_size]['memory_required']

    # Filter by both memory requirements and tier if a tier is selected
    aws_comp = filter_compatible(aws_instances, min_mem)
    gcp_comp = filter_compatible(gcp_instances, min_mem)
    
    if comparison_tier != "All":
        aws_comp = {k: v for k, v in aws_comp.items() if v.get('tier', '') == comparison_tier}
        gcp_comp = {k: v for k, v in gcp_comp.items() if v.get('tier', '') == comparison_tier}

    results = []

    # AWS table
    aws_html = '<h3>AWS Instances</h3>'
    aws_html += '<table width="100%"><tr><th>Instance</th><th>vCPUs</th><th>Memory</th><th>GPU</th><th>Tier</th><th>Monthly Cost ($)</th></tr>'
    if aws_comp:
        for inst in aws_comp:
            res = calculate_aws_cost(inst, compute_hours, storage_gb, reserved_instances, spot_instances, years)
            aws_html += f'<tr><td>{inst}</td><td>{res["details"]["vcpus"]}</td><td>{res["details"]["memory"]}GB</td><td>{res["details"]["gpu"]}</td><td>{res["details"].get("tier", "")}</td><td>${res["total_cost"]:.2f}</td></tr>'
        # best AWS
        best_aws = min(aws_comp, key=lambda x: calculate_aws_cost(x, compute_hours, storage_gb, reserved_instances, spot_instances, years)['total_cost'])
        best_aws_cost = calculate_aws_cost(best_aws, compute_hours, storage_gb, reserved_instances, spot_instances, years)['total_cost']
        best_aws_tier = aws_instances[best_aws].get('tier', '')
        results.append({'provider': f'AWS ({best_aws})', 'cost': best_aws_cost, 'type': 'Cloud', 'tier': best_aws_tier})
    else:
        aws_html += '<tr><td colspan="6">No compatible instances</td></tr>'
    aws_html += '</table>'

    # GCP table
    gcp_html = '<h3>GCP Instances</h3>'
    gcp_html += '<table width="100%"><tr><th>Instance</th><th>vCPUs</th><th>Memory</th><th>GPU</th><th>Tier</th><th>Monthly Cost ($)</th></tr>'
    if gcp_comp:
        for inst in gcp_comp:
            res = calculate_gcp_cost(inst, compute_hours, storage_gb, reserved_instances, spot_instances, years)
            gcp_html += f'<tr><td>{inst}</td><td>{res["details"]["vcpus"]}</td><td>{res["details"]["memory"]}GB</td><td>{res["details"]["gpu"]}</td><td>{res["details"].get("tier", "")}</td><td>${res["total_cost"]:.2f}</td></tr>'
        best_gcp = min(gcp_comp, key=lambda x: calculate_gcp_cost(x, compute_hours, storage_gb, reserved_instances, spot_instances, years)['total_cost'])
        best_gcp_cost = calculate_gcp_cost(best_gcp, compute_hours, storage_gb, reserved_instances, spot_instances, years)['total_cost']
        best_gcp_tier = gcp_instances[best_gcp].get('tier', '')
        results.append({'provider': f'GCP ({best_gcp})', 'cost': best_gcp_cost, 'type': 'Cloud', 'tier': best_gcp_tier})
    else:
        gcp_html += '<tr><td colspan="6">No compatible instances</td></tr>'
    gcp_html += '</table>'

    # API table
    api_html = '<h3>API Options</h3>'
    api_html += '<table width="100%"><tr><th>Provider</th><th>Model</th><th>Input Cost</th><th>Output Cost</th><th>Total Cost ($)</th><th>Context</th></tr>'
    api_costs = {}
    for prov in api_pricing:
        for mdl in api_pricing[prov]:
            res = calculate_api_cost(prov, mdl, in_tokens, out_tokens, api_calls)
            details = api_pricing[prov][mdl]
            api_html += f'<tr><td>{prov}</td><td>{mdl}</td><td>${in_tokens * details["input_per_1M"] / 1000000:.2f}</td><td>${out_tokens * details["output_per_1M"] / 1000000:.2f}</td><td>${res["total_cost"]:.2f}</td><td>{details["token_context"]:,}</td></tr>'
            api_costs[(prov, mdl)] = res['total_cost']
    api_html += '</table>'
    
    if api_costs:
        best_api = min(api_costs, key=api_costs.get)
        results.append({'provider': f'{best_api[0]} ({best_api[1]})', 'cost': api_costs[best_api], 'type': 'API', 'tier': 'API'})

    # Direct comparison tables for similar instances
    direct_comparison_html = ""
    if comparison_tier != "All" and comparison_tier != "API":
        direct_comparison_html = f'<h3>Direct {comparison_tier} Tier Comparison</h3>'
        direct_comparison_html += '<table width="100%"><tr><th>Provider</th><th>Instance</th><th>vCPUs</th><th>Memory</th><th>GPU</th><th>Monthly Cost ($)</th></tr>'
        
        aws_filtered = {k: v for k, v in aws_instances.items() if v.get('tier', '') == comparison_tier}
        gcp_filtered = {k: v for k, v in gcp_instances.items() if v.get('tier', '') == comparison_tier}
        
        # Group by vCPU for comparison
        vcpu_groups = {}
        for inst, data in aws_filtered.items():
            vcpu = data['vcpus']
            if vcpu not in vcpu_groups:
                vcpu_groups[vcpu] = {'aws': [], 'gcp': []}
            vcpu_groups[vcpu]['aws'].append(inst)
        
        for inst, data in gcp_filtered.items():
            vcpu = data['vcpus']
            if vcpu not in vcpu_groups:
                vcpu_groups[vcpu] = {'aws': [], 'gcp': []}
            vcpu_groups[vcpu]['gcp'].append(inst)
        
        # Display direct comparisons
        for vcpu in sorted(vcpu_groups.keys()):
            group = vcpu_groups[vcpu]
            for aws_inst in group['aws']:
                aws_cost = calculate_aws_cost(aws_inst, compute_hours, storage_gb, reserved_instances, spot_instances, years)
                aws_data = aws_cost['details']
                direct_comparison_html += f'<tr><td>AWS</td><td>{aws_inst}</td><td>{aws_data["vcpus"]}</td><td>{aws_data["memory"]}GB</td><td>{aws_data["gpu"]}</td><td>${aws_cost["total_cost"]:.2f}</td></tr>'
            
            for gcp_inst in group['gcp']:
                gcp_cost = calculate_gcp_cost(gcp_inst, compute_hours, storage_gb, reserved_instances, spot_instances, years)
                gcp_data = gcp_cost['details']
                direct_comparison_html += f'<tr><td>GCP</td><td>{gcp_inst}</td><td>{gcp_data["vcpus"]}</td><td>{gcp_data["memory"]}GB</td><td>{gcp_data["gpu"]}</td><td>${gcp_cost["total_cost"]:.2f}</td></tr>'
            
            # Add separator between different vCPU groups
            if vcpu != sorted(vcpu_groups.keys())[-1]:
                direct_comparison_html += '<tr><td colspan="6" style="border-bottom: 1px solid #ccc; height: 10px;"></td></tr>'
        
        direct_comparison_html += '</table>'

    # Chart with annotations
    df = pd.DataFrame(results)
    colors = {'Entry': '#66BB6A', 'Mid': '#42A5F5', 'High': '#FFA726', 'Premium': '#EF5350', 'API': '#AB47BC'}
    
    # Create figure using plotly graph objects for more control
    fig = go.Figure()
    
    # Add bars
    for i, row in df.iterrows():
        tier_color = colors.get(row.get('tier', 'API'), '#9E9E9E')
        fig.add_trace(go.Bar(
            x=[row['provider']], 
            y=[row['cost']], 
            name=row['provider'],
            marker_color=tier_color
        ))
    
    # Add annotations on top of each bar
    for i, row in df.iterrows():
        fig.add_annotation(
            x=row['provider'],
            y=row['cost'],
            text=f"${row['cost']:.2f}",
            showarrow=False,
            yshift=10,  # Position above the bar
            font=dict(size=14)
        )
    
    # Update layout
    fig.update_layout(
        showlegend=False,
        height=500,
        yaxis=dict(title='Monthly Cost ($)', tickprefix='$'),
        xaxis=dict(title=''),
        title='Cost Comparison'
    )

    html = f"""
    <div style='padding:20px;font-family:Arial;'>
      {direct_comparison_html}
      {aws_html}
      {gcp_html}
      {api_html}
    </div>
    """
    return html, fig

# UI setup
with gr.Blocks(title="Cloud Cost Estimator", theme=gr.themes.Soft(primary_hue="indigo")) as demo:
    gr.HTML('<h1 style="text-align:center;">Cloud Cost Estimator</h1>')
    with gr.Row():
        with gr.Column(scale=1):
            compute_hours = gr.Slider(label="Compute Hours per Month", minimum=1, maximum=300, value=50)
            tokens_per_month = gr.Slider(label="Tokens per Month (M)", minimum=1, maximum=200, value=5)
            input_ratio = gr.Slider(label="Input Ratio (%)", minimum=10, maximum=70, value=25)
            api_calls = gr.Slider(label="API Calls per Month", minimum=100, maximum=100000, value=5000, step=100)
            model_size = gr.Dropdown(label="Model Size", choices=list(model_sizes.keys()), value="Medium (13B parameters)")
            storage_gb = gr.Slider(label="Storage (GB)", minimum=10, maximum=1000, value=100)
            comparison_tier = gr.Radio(label="Comparison Tier", choices=["All", "Entry", "Mid", "High", "Premium", "API"], value="All")
            reserved_instances = gr.Checkbox(label="Reserved Instances", value=False)
            spot_instances = gr.Checkbox(label="Spot Instances", value=False)
            multi_year_commitment = gr.Radio(label="Commitment Period (years)", choices=["1","3"], value="1")
        with gr.Column(scale=2):
            out_html = gr.HTML()
            out_plot = gr.Plot()
    
    # Create inputs list for the function
    inputs = [compute_hours, tokens_per_month, input_ratio, api_calls,
             model_size, storage_gb, reserved_instances, spot_instances, multi_year_commitment, comparison_tier]
    outputs = [out_html, out_plot]
    
    # Initial calculation on load
    demo.load(generate_cost_comparison, inputs, outputs)
    
    # Update on each input change
    for input_component in inputs:
        input_component.change(generate_cost_comparison, inputs, outputs)

demo.launch()