-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_app.py
188 lines (159 loc) · 5.73 KB
/
streamlit_app.py
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
"""
Streamlit App for Cost Tracker (Open WebUI function) Data Visualization
This Streamlit application processes and visualizes cost data from a JSON file.
It generates plots for total tokens used and total costs by model and user.
Author: bgeneto
Version: 0.2.2
Date: 2024-11-29
"""
import datetime
import json
from typing import Any, Dict, List, Optional
import pandas as pd
import plotly.express as px
import streamlit as st
@st.cache
def load_data(file: Any) -> Optional[List[Dict[str, Any]]]:
"""Load data from a JSON file.
Args:
file: A file-like object containing JSON data.
Returns:
A list of dictionaries with cost records if the JSON is valid, otherwise None.
"""
try:
data = json.load(file)
return data
except json.JSONDecodeError:
st.error("Invalid JSON file. Please upload a valid JSON file.")
return None
def process_data(data: List[Dict[str, Any]]) -> pd.DataFrame:
"""Process the data by extracting the month, model, cost, and user.
Args:
data: A list of dictionaries containing cost records.
Returns:
A pandas DataFrame with processed data.
"""
processed_data = []
for record in data:
timestamp = datetime.datetime.strptime(
record["timestamp"], "%Y-%m-%dT%H:%M:%S.%f"
)
month = timestamp.strftime("%Y-%m")
model = record["model"]
cost = record["total_cost"]
try:
if isinstance(cost, str):
cost = float(cost)
except ValueError:
st.error(f"Invalid cost value for model {model}.")
continue
total_tokens = record["input_tokens"] + record["output_tokens"]
user = record["user"]
processed_data.append(
{
"month": month,
"model": model,
"total_cost": cost,
"user": user,
"total_tokens": total_tokens,
}
)
return pd.DataFrame(processed_data)
def plot_data(data: pd.DataFrame, month: str) -> None:
"""Plot the data for a specific month.
Args:
data: A pandas DataFrame containing processed data.
month: A string representing the month to filter data.
"""
month_data = data[data["month"] == month]
if month_data.empty:
st.error(f"No data available for {month}.")
return
# ---------------------------------
# Model Usage Bar Plot (Total Tokens)
# ---------------------------------
month_data_models_tokens = (
month_data.groupby("model")["total_tokens"].sum().reset_index()
)
month_data_models_tokens = month_data_models_tokens.sort_values(
by="total_tokens", ascending=False
).head(10)
fig_models_tokens = px.bar(
month_data_models_tokens,
x="model",
y="total_tokens",
title=f"Top 10 Total Tokens Used by Model ({month})",
)
st.plotly_chart(fig_models_tokens, use_container_width=True)
# ---------------------------------
# Model Cost Bar Plot (Total Cost)
# ---------------------------------
month_data_models_cost = (
month_data.groupby("model")["total_cost"].sum().reset_index()
)
month_data_models_cost = month_data_models_cost.sort_values(
by="total_cost", ascending=False
).head(10)
fig_models_cost = px.bar(
month_data_models_cost,
x="model",
y="total_cost",
title=f"Top 10 Total Cost by Model ({month})",
)
st.plotly_chart(fig_models_cost, use_container_width=True)
# ---------------------------------
# User Cost Bar Plot (Total Cost)
# ---------------------------------
month_data_users = month_data.groupby("user")["total_cost"].sum().reset_index()
month_data_users = month_data_users.sort_values(by="total_cost", ascending=False)
month_data_users["total"] = month_data_users["total_cost"].sum()
month_data_users = pd.concat(
[
month_data_users,
pd.DataFrame(
{"user": ["Total"], "total_cost": [month_data_users["total"].iloc[0]]}
),
]
)
fig_users = px.bar(
month_data_users, x="user", y="total_cost", title=f"Total Cost by User ({month})"
)
st.plotly_chart(fig_users, use_container_width=True)
# ---------------------------------
# Collapsible DataFrames
# ---------------------------------
with st.expander("Show DataFrames"):
st.subheader("Month Data")
st.dataframe(month_data)
st.subheader("Month Data Models Tokens")
st.dataframe(month_data_models_tokens)
st.subheader("Month Data Models Cost")
st.dataframe(month_data_models_cost)
st.subheader("Month Data Users")
st.dataframe(month_data_users)
def main():
st.set_page_config(page_title="Cost Tracker App", page_icon="💵")
st.title("Cost Tracker for Open WebUI")
st.divider()
st.page_link(
"https://github.com/bgeneto/open-webui-cost-tracker/",
label="GitHub Page",
icon="🏠",
)
st.sidebar.title("⚙️ Options")
st.info(
"This Streamlit app processes and visualizes cost data from a JSON file. Select a JSON file below and a month to plot the data."
)
file = st.file_uploader("Upload a JSON file", type=["json"])
if file is not None:
data = load_data(file)
if data is not None:
processed_data = process_data(data)
months = processed_data["month"].unique()
month = st.sidebar.selectbox("Select a month", months)
if st.button("Process Data"):
plot_data(processed_data, month)
if st.sidebar.button("Plot Data"):
plot_data(processed_data, month)
if __name__ == "__main__":
main()