-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashboard.py
126 lines (100 loc) · 3.91 KB
/
dashboard.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
import streamlit as st
import pandas as pd
from st_aggrid import AgGrid, GridOptionsBuilder
from data_analysis import DataAnalysis
from data_ingestion import DataIngestion
from visualization import Visualization
from sidebar import sidebar
# Set the page layout to wide
st.set_page_config(layout="wide")
def main_dashboard():
"""Render the main dashboard."""
st.title("ChatGPT Cost Dashboard")
# Define tabs
tabs = st.tabs(["Conversations", "Statistics", "Advanced Analytics"])
# Initialize data processor
data_analysis = DataAnalysis()
data_ingestion = DataIngestion()
# Conversations Tab
with tabs[0]:
st.header("Conversations and Messages")
# Load and display conversation summary
conversation_summary_df = data_analysis.calculate_conversation_summary()
if not conversation_summary_df.empty:
st.write("### Conversations Data")
# Setup AgGrid options for single row selection
gb = GridOptionsBuilder.from_dataframe(conversation_summary_df)
gb.configure_selection("single")
grid_options = gb.build()
# Display the dataframe using AgGrid
grid_response = AgGrid(
conversation_summary_df,
gridOptions=grid_options,
height=300,
fit_columns_on_grid_load=True,
)
selected_rows = None
# Check if any rows are selected
selected_rows: pd.DataFrame = grid_response.get("selected_rows", None)
# print(selected_rows)
if selected_rows is not None:
selected_conversation = selected_rows["conversation_id"][0]
messages_df = data_analysis.load_conversation_messages(
selected_conversation
)
st.write(f"### Messages Data for Conversation: {selected_conversation}")
st.dataframe(
messages_df.drop(
columns=[
"conversation_id",
"conversation_title",
"conversation_create_time",
"conversation_update_time",
"current_node",
"is_archived",
"default_model_slug",
]
).style.format(
{
"input_cost": "${:,.5f}",
"output_cost": "${:,.5f}",
"total_cost": "${:,.5f}",
}
),
use_container_width=True,
)
else:
st.write(
"Select a conversation from the table above to view its messages."
)
else:
st.write("No conversations data found or incorrect columns loaded.")
# Statistics Tab
with tabs[1]:
st.header("Statistics")
# Initialize Visualization object
visualization = Visualization(data_analysis)
# Display summary statistics
visualization.display_summary_statistics()
# Period Breakdown
visualization.display_period_breakdown()
# Advanced Analytics Tab
with tabs[2]:
st.header("Advanced Analytics")
# Sentiment Analysis
visualization.display_sentiment_analysis()
# Keyword Analysis
visualization.display_keyword_analysis()
# Cost Forecasting
visualization.display_cost_forecasting()
# Token Efficiency
visualization.display_token_efficiency()
def main():
"""Main application logic."""
if "file_processed" not in st.session_state:
st.session_state.file_processed = False
file_uploaded = sidebar()
if st.session_state.file_processed:
main_dashboard()
if __name__ == "__main__":
main()