File size: 6,387 Bytes
90c9a37 |
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 |
import streamlit as st
import asyncio
import tokonomics
from utils import create_model_hierarchy
st.set_page_config(page_title="LLM Pricing App", layout="wide")
# --------------------------
# Async Data Loading Function
# --------------------------
async def load_data():
"""Simulate loading data asynchronously."""
AVAILABLE_MODELS = await tokonomics.get_available_models()
hierarchy = create_model_hierarchy(AVAILABLE_MODELS)
FILTERED_MODELS = []
MODEL_PRICING = {}
PROVIDERS = list(hierarchy.keys())
for provider in PROVIDERS:
for model_family in hierarchy[provider]:
for model_version in hierarchy[provider][model_family].keys():
for region in hierarchy[provider][model_family][model_version]:
model_id = hierarchy[provider][model_family][model_version][region]
MODEL_PRICING[model_id] = await tokonomics.get_model_costs(model_id)
FILTERED_MODELS.append(model_id)
return FILTERED_MODELS, MODEL_PRICING, PROVIDERS
# --------------------------
# Provider Change Function
# --------------------------
def provider_change(provider, selected_type, all_types=["text", "vision", "video", "image"]):
"""Filter models based on the selected provider and type."""
all_models = st.session_state.get("models", [])
new_models = []
others = [a_type for a_type in all_types if selected_type != a_type]
for model_name in all_models:
if provider in model_name:
if selected_type in model_name:
new_models.append(model_name)
elif any(other in model_name for other in others):
continue
else:
new_models.append(model_name)
return new_models if new_models else all_models
# --------------------------
# Estimate Cost Function (Updated)
# --------------------------
def estimate_cost(num_alerts, input_size, output_size, model_id):
pricing = st.session_state.get("pricing", {})
cost_token = pricing.get(model_id)
if not cost_token:
return "NA"
input_tokens = round(input_size * 1.3)
output_tokens = round(output_size * 1.3)
price_day = cost_token.get("input_cost_per_token", 0) * input_tokens + cost_token.get("output_cost_per_token", 0) * output_tokens
price_total = price_day * num_alerts
return f"""## Estimated Cost:
Day Price: {price_total:0.2f} USD
Month Price: {price_total * 31:0.2f} USD
Year Price: {price_total * 365:0.2f} USD
"""
# --------------------------
# Load Data into Session State (only once)
# --------------------------
if "data_loaded" not in st.session_state:
with st.spinner("Loading pricing data..."):
models, pricing, providers = asyncio.run(load_data())
st.session_state["models"] = models
st.session_state["pricing"] = pricing
st.session_state["providers"] = providers
st.session_state["data_loaded"] = True
# --------------------------
# Sidebar
# --------------------------
with st.sidebar:
st.image("https://cdn.prod.website-files.com/630f558f2a15ca1e88a2f774/631f1436ad7a0605fecc5e15_Logo.svg", use_container_width=True)
st.divider()
st.sidebar.title("LLM Pricing Calculator")
# --------------------------
# Main Content Layout (Model Selection Tab)
# --------------------------
tab1, tab2 = st.tabs(["Model Selection", "About"])
with tab1:
st.header("LLM Pricing App")
# --- Row 1: Provider/Type and Model Selection ---
col_left, col_right = st.columns(2)
with col_left:
selected_provider = st.selectbox("Select a provider", st.session_state["providers"])
selected_type = st.radio("Select type", options=["text", "image"], index=0)
with col_right:
# Filter models based on the selected provider and type
filtered_models = provider_change(selected_provider, selected_type)
if filtered_models:
selected_model = st.selectbox("Select a model", options=filtered_models)
else:
selected_model = None
st.write("No models available")
# --- Row 2: Alert Stats ---
col1, col2, col3 = st.columns(3)
with col1:
num_alerts = st.number_input(
"Security Alerts Per Day",
value=100,
min_value=1,
step=1,
help="Number of security alerts to analyze daily"
)
with col2:
input_size = st.number_input(
"Alert Content Size (characters)",
value=1000,
min_value=1,
step=1,
help="Include logs, metadata, and context per alert"
)
with col3:
output_size = st.number_input(
"Analysis Output Size (characters)",
value=500,
min_value=1,
step=1,
help="Expected length of security analysis and recommendations"
)
# --- Row 3: Buttons ---
btn_col1, btn_col2 = st.columns(2)
with btn_col1:
if st.button("Estimate"):
if selected_model:
st.session_state["result"] = estimate_cost(num_alerts, input_size, output_size, selected_model)
else:
st.session_state["result"] = "No model selected."
with btn_col2:
if st.button("Refresh Pricing Data"):
with st.spinner("Refreshing pricing data..."):
models, pricing, providers = asyncio.run(load_data())
st.session_state["models"] = models
st.session_state["pricing"] = pricing
st.session_state["providers"] = providers
st.success("Pricing data refreshed!")
st.divider()
# --- Display Results ---
st.markdown("### Results")
if "result" in st.session_state:
st.write(st.session_state["result"])
else:
st.write("Use the buttons above to estimate costs.")
# --- Clear Button Below Results ---
if st.button("Clear"):
st.session_state.pop("result", None)
st.rerun()
with tab2:
st.markdown(
"""
## About This App
This is based on the tokonomics package.
- The app downloads the latest pricing from the LiteLLM repository.
- Using simple maths to estimate the total tokens.
- Version 0.1
Website: [https://www.priam.ai](https://www.priam.ai)
"""
)
|