-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
225 lines (191 loc) · 7.49 KB
/
app.py
File metadata and controls
225 lines (191 loc) · 7.49 KB
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
from flask import Flask, render_template, request
import json
import pandas as pd
import plotly.express as px
from plotly.utils import PlotlyJSONEncoder
import json
from datetime import datetime
app = Flask(__name__)
def cleanse_jsonl(data: str) -> str:
# because passing a whole file as a string is obviously peak efficiency
clean_lines = []
for line in data.splitlines():
try:
json.loads(line) # oh look, it's json or it's junk
clean_lines.append(line)
except json.JSONDecodeError:
# another one bites the dust, thank heavens
pass
# returning clean data because writing it back to a file was too mainstream
return '\n'.join(clean_lines)
def load_jsonl_data(data: str):
# data as a string, because who needs files anyway
data_frames = []
for line in data.splitlines():
json_obj = json.loads(line) # magic, turning strings into dicts
# look at us, flattening jsons like it's pancake day
flattened_obj = pd.json_normalize(json_obj, sep="_")
data_frames.append(flattened_obj)
# stitching data frames together, like a patchwork of mediocrity
df = pd.concat(data_frames, ignore_index=True)
return df
def create_figure(df, x_column, y_columns, title):
# Ensure the specified columns are numeric, converting non-numeric values to NaN
for col in y_columns:
df[col] = pd.to_numeric(df[col], errors="coerce")
# Use Plotly Express to create the figure
fig = px.line(
df,
x=x_column,
y=y_columns,
labels={
"value": title,
"variable": "Metrics",
}, # Customize label names as needed
title=title,
)
# Highlight the plot title
fig.update_layout(
title={
'text': title,
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top',
'font': {
'size': 24,
'color': '#333'
}
}
)
fig.for_each_trace(lambda t: t.update(name=t.name.replace("variable=", "")))
return fig
def create_figure_util(df, x_column, y_columns, title):
for col in y_columns:
# Check if the column contains percentage values
if df[col].dtype == "object" and df[col].str.contains("%").any():
# Convert percentage strings to float, handling 'n/a' and other non-convertible strings
df[col] = (
df[col]
.str.rstrip("%")
.apply(lambda x: pd.to_numeric(x, errors="coerce"))
/ 100.0
)
else:
# Convert column to numeric, coercing errors to NaN
df[col] = pd.to_numeric(df[col], errors="coerce")
# Use Plotly Express to create the figure
fig = px.line(
df,
x=x_column,
y=y_columns,
labels={
"value": title,
"variable": "Metrics",
}, # Customize label names as needed
title=title,
)
# Highlight the plot title
fig.update_layout(
title={
'text': title,
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top',
'font': {
'size': 24,
'color': '#333'
}
}
)
fig.for_each_trace(lambda t: t.update(name=t.name.replace("variable=", "")))
# Set y-axis range to have a maximum of 100 (assuming it's percentage)
fig.update_layout(
yaxis_range=[0, 1]
) # yaxis_range is [min, max], setting max to 1 since the data is now in decimal form
return fig
def get_current_time_formatted():
now = datetime.now()
formatted_time = now.strftime("%Y-%m-%d-%H-%M")
return formatted_time
def extract_benchmark_config(content: str) -> dict:
"""
Extracts the benchmark configuration from the log file's header.
Parameters:
- content (str): The content of the log file as a string.
Returns:
- dict: A dictionary containing the benchmark configuration.
"""
# Split content by lines and extract the first line
first_line = content.splitlines()[0]
# Assuming the configuration is always in the first line and in JSON format
try:
config_json = json.loads(first_line.split(': ', 1)[1])
return config_json
except json.JSONDecodeError:
return {}
def process_jsonl_and_create_figures(content : str):
benchmark_config = extract_benchmark_config(content)
print(benchmark_config)
content = cleanse_jsonl(content)
df = load_jsonl_data(content)
fig_tpm = create_figure(df, "timestamp", ["tpm_total", "tpm_context", "tpm_gen"], "TPM over Time")
graphJSON_tpm = json.dumps(fig_tpm, cls=PlotlyJSONEncoder)
fig_rpm = create_figure(df, "timestamp", ["rpm"], "RPM over Time")
graphJSON_rpm = json.dumps(fig_rpm, cls=PlotlyJSONEncoder)
fig_util = create_figure_util(df, "timestamp", ["util_avg", "util_95th"], "Utilization over Time")
graphJSON_util = json.dumps(fig_util, cls=PlotlyJSONEncoder)
fig_ttft = create_figure(df, "timestamp", ["ttft_avg", "ttft_95th"], "Time To First Token")
graphJSON_ttft = json.dumps(fig_ttft, cls=PlotlyJSONEncoder)
fig_tbt = create_figure(df, "timestamp", ["tbt_avg", "tbt_95th"], "Seconds between two consequitive generated tokens")
graphJSON_tbt = json.dumps(fig_tbt, cls=PlotlyJSONEncoder)
fig_e2e = create_figure(df, "timestamp", ["e2e_avg", "e2e_95th"], "e2e over Time")
graphJSON_e2e = json.dumps(fig_e2e, cls=PlotlyJSONEncoder)
current_time = get_current_time_formatted()
return {
"graphJSON_tpm": graphJSON_tpm,
"graphJSON_rpm": graphJSON_rpm,
"graphJSON_util": graphJSON_util,
"graphJSON_ttft": graphJSON_ttft,
"graphJSON_tbt": graphJSON_tbt,
"graphJSON_e2e": graphJSON_e2e,
"formatted_time": current_time,
"benchmark_config": benchmark_config
}
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Handle file upload or text input
content = request.form.get('content') # Assuming text input is named 'content'
if 'file' in request.files:
file = request.files['file']
if file.filename:
content = file.read().decode('utf-8')
if content:
try:
# Process the JSONL data
figures = process_jsonl_and_create_figures(content)
# Render the template with graphs data
return render_template(
"index.html",
graphJSON_tpm=figures["graphJSON_tpm"],
graphJSON_rpm=figures["graphJSON_rpm"],
graphJSON_util=figures["graphJSON_util"],
graphJSON_ttft=figures["graphJSON_ttft"],
graphJSON_tbt=figures["graphJSON_tbt"],
graphJSON_e2e=figures["graphJSON_e2e"],
formatted_time=figures["formatted_time"],
benchmark_config=figures["benchmark_config"],
)
except Exception as e:
# Output the exception stack to the user
return f"An error occurred: {str(e)}"
else:
# No content provided, do nothing
return render_template("start.html")
else:
# For GET requests, 'graphs' is not needed, so pass an empty dictionary or None
return render_template("start.html")
if __name__ == '__main__':
app.run(debug=True)