Commit
·
4014f2e
1
Parent(s):
765cbc1
feat: adicionar armazenamento de métricas de inferência em banco de dados SQLite e criar gráficos de desempenho
Browse files
app.py
CHANGED
@@ -7,6 +7,8 @@ import onnxruntime as ort
|
|
7 |
from collections import deque
|
8 |
import gradio as gr
|
9 |
import os
|
|
|
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
|
12 |
# Model info
|
@@ -52,6 +54,66 @@ def download_model():
|
|
52 |
raise e
|
53 |
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
class SignatureDetector:
|
56 |
def __init__(self, model_path):
|
57 |
self.model_path = model_path
|
@@ -64,23 +126,101 @@ class SignatureDetector:
|
|
64 |
MODEL_PATH, providers=["CPUExecutionProvider"]
|
65 |
)
|
66 |
|
67 |
-
|
68 |
-
self.inference_times = deque(maxlen=50) # Store last 50 inference times
|
69 |
-
self.total_inferences = 0
|
70 |
-
self.avg_inference_time = 0
|
71 |
|
72 |
def update_metrics(self, inference_time):
|
73 |
-
|
74 |
-
self.
|
75 |
-
self.avg_inference_time = sum(self.inference_times) / len(self.inference_times)
|
76 |
|
77 |
def get_metrics(self):
|
|
|
78 |
return {
|
79 |
-
"times":
|
80 |
-
"total_inferences": self.
|
81 |
-
"avg_time": self.
|
82 |
}
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
def preprocess(self, img):
|
85 |
# Convert PIL Image to cv2 format
|
86 |
img_cv2 = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
@@ -249,64 +389,8 @@ def create_gradio_interface():
|
|
249 |
}
|
250 |
)
|
251 |
|
252 |
-
#
|
253 |
-
|
254 |
-
|
255 |
-
# Configuração do estilo dos plots
|
256 |
-
plt.style.use("dark_background")
|
257 |
-
|
258 |
-
# Criar figura do histograma
|
259 |
-
hist_fig, hist_ax = plt.subplots(figsize=(8, 4), facecolor="#f0f0f5")
|
260 |
-
hist_ax.set_facecolor("#f0f0f5")
|
261 |
-
hist_data.hist(
|
262 |
-
bins=20, ax=hist_ax, color="#4F46E5", alpha=0.7, edgecolor="white"
|
263 |
-
)
|
264 |
-
hist_ax.set_title(
|
265 |
-
"Distribuição dos Tempos de Inferência",
|
266 |
-
pad=15,
|
267 |
-
fontsize=12,
|
268 |
-
color="#1f2937",
|
269 |
-
)
|
270 |
-
hist_ax.set_xlabel("Tempo (ms)", color="#374151")
|
271 |
-
hist_ax.set_ylabel("Frequência", color="#374151")
|
272 |
-
hist_ax.tick_params(colors="#4b5563")
|
273 |
-
hist_ax.grid(True, linestyle="--", alpha=0.3)
|
274 |
-
|
275 |
-
# Criar figura do gráfico de linha
|
276 |
-
line_fig, line_ax = plt.subplots(figsize=(8, 4), facecolor="#f0f0f5")
|
277 |
-
line_ax.set_facecolor("#f0f0f5")
|
278 |
-
line_data.plot(
|
279 |
-
x="Inferência",
|
280 |
-
y="Tempo (ms)",
|
281 |
-
ax=line_ax,
|
282 |
-
color="#4F46E5",
|
283 |
-
alpha=0.7,
|
284 |
-
label="Tempo",
|
285 |
-
)
|
286 |
-
line_data.plot(
|
287 |
-
x="Inferência",
|
288 |
-
y="Média",
|
289 |
-
ax=line_ax,
|
290 |
-
color="#DC2626",
|
291 |
-
linestyle="--",
|
292 |
-
label="Média",
|
293 |
-
)
|
294 |
-
line_ax.set_title(
|
295 |
-
"Tempo de Inferência por Execução", pad=15, fontsize=12, color="#1f2937"
|
296 |
-
)
|
297 |
-
line_ax.set_xlabel("Número da Inferência", color="#374151")
|
298 |
-
line_ax.set_ylabel("Tempo (ms)", color="#374151")
|
299 |
-
line_ax.tick_params(colors="#4b5563")
|
300 |
-
line_ax.grid(True, linestyle="--", alpha=0.3)
|
301 |
-
line_ax.legend(frameon=True, facecolor="#f0f0f5", edgecolor="none")
|
302 |
-
|
303 |
-
# Ajustar layout
|
304 |
-
hist_fig.tight_layout()
|
305 |
-
line_fig.tight_layout()
|
306 |
-
|
307 |
-
# Fechar as figuras para liberar memória
|
308 |
-
plt.close(hist_fig)
|
309 |
-
plt.close(line_fig)
|
310 |
|
311 |
return (
|
312 |
output_image,
|
@@ -428,6 +512,13 @@ def create_gradio_interface():
|
|
428 |
outputs=[output_image, total_inferences, hist_plot, line_plot],
|
429 |
)
|
430 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
431 |
return iface
|
432 |
|
433 |
|
|
|
7 |
from collections import deque
|
8 |
import gradio as gr
|
9 |
import os
|
10 |
+
import sqlite3
|
11 |
+
from datetime import datetime
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
|
14 |
# Model info
|
|
|
54 |
raise e
|
55 |
|
56 |
|
57 |
+
class MetricsStorage:
|
58 |
+
def __init__(self, db_path="metrics.db"):
|
59 |
+
self.db_path = db_path
|
60 |
+
self.setup_database()
|
61 |
+
|
62 |
+
def setup_database(self):
|
63 |
+
"""Initialize the SQLite database and create tables if they don't exist"""
|
64 |
+
with sqlite3.connect(self.db_path) as conn:
|
65 |
+
cursor = conn.cursor()
|
66 |
+
cursor.execute(
|
67 |
+
"""
|
68 |
+
CREATE TABLE IF NOT EXISTS inference_metrics (
|
69 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
70 |
+
inference_time REAL,
|
71 |
+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
72 |
+
)
|
73 |
+
"""
|
74 |
+
)
|
75 |
+
conn.commit()
|
76 |
+
|
77 |
+
def add_metric(self, inference_time):
|
78 |
+
"""Add a new inference time measurement to the database"""
|
79 |
+
with sqlite3.connect(self.db_path) as conn:
|
80 |
+
cursor = conn.cursor()
|
81 |
+
cursor.execute(
|
82 |
+
"INSERT INTO inference_metrics (inference_time) VALUES (?)",
|
83 |
+
(inference_time,),
|
84 |
+
)
|
85 |
+
conn.commit()
|
86 |
+
|
87 |
+
def get_recent_metrics(self, limit=50):
|
88 |
+
"""Get the most recent metrics from the database"""
|
89 |
+
with sqlite3.connect(self.db_path) as conn:
|
90 |
+
cursor = conn.cursor()
|
91 |
+
cursor.execute(
|
92 |
+
"SELECT inference_time FROM inference_metrics ORDER BY timestamp DESC LIMIT ?",
|
93 |
+
(limit,),
|
94 |
+
)
|
95 |
+
results = cursor.fetchall()
|
96 |
+
return [r[0] for r in results]
|
97 |
+
|
98 |
+
def get_total_inferences(self):
|
99 |
+
"""Get the total number of inferences recorded"""
|
100 |
+
with sqlite3.connect(self.db_path) as conn:
|
101 |
+
cursor = conn.cursor()
|
102 |
+
cursor.execute("SELECT COUNT(*) FROM inference_metrics")
|
103 |
+
return cursor.fetchone()[0]
|
104 |
+
|
105 |
+
def get_average_time(self, limit=50):
|
106 |
+
"""Get the average inference time from the most recent entries"""
|
107 |
+
with sqlite3.connect(self.db_path) as conn:
|
108 |
+
cursor = conn.cursor()
|
109 |
+
cursor.execute(
|
110 |
+
"SELECT AVG(inference_time) FROM (SELECT inference_time FROM inference_metrics ORDER BY timestamp DESC LIMIT ?)",
|
111 |
+
(limit,),
|
112 |
+
)
|
113 |
+
result = cursor.fetchone()[0]
|
114 |
+
return result if result is not None else 0
|
115 |
+
|
116 |
+
|
117 |
class SignatureDetector:
|
118 |
def __init__(self, model_path):
|
119 |
self.model_path = model_path
|
|
|
126 |
MODEL_PATH, providers=["CPUExecutionProvider"]
|
127 |
)
|
128 |
|
129 |
+
self.metrics_storage = MetricsStorage()
|
|
|
|
|
|
|
130 |
|
131 |
def update_metrics(self, inference_time):
|
132 |
+
"""Update metrics in persistent storage"""
|
133 |
+
self.metrics_storage.add_metric(inference_time)
|
|
|
134 |
|
135 |
def get_metrics(self):
|
136 |
+
"""Get current metrics from storage"""
|
137 |
return {
|
138 |
+
"times": self.metrics_storage.get_recent_metrics(),
|
139 |
+
"total_inferences": self.metrics_storage.get_total_inferences(),
|
140 |
+
"avg_time": self.metrics_storage.get_average_time(),
|
141 |
}
|
142 |
|
143 |
+
def load_initial_metrics(self):
|
144 |
+
"""Load initial metrics for display"""
|
145 |
+
metrics = self.get_metrics()
|
146 |
+
|
147 |
+
if not metrics["times"]: # Se não houver dados
|
148 |
+
return None, None, None, None
|
149 |
+
|
150 |
+
# Criar plots data
|
151 |
+
hist_data = pd.DataFrame({"Tempo (ms)": metrics["times"]})
|
152 |
+
line_data = pd.DataFrame(
|
153 |
+
{
|
154 |
+
"Inferência": range(len(metrics["times"])),
|
155 |
+
"Tempo (ms)": metrics["times"],
|
156 |
+
"Média": [metrics["avg_time"]] * len(metrics["times"]),
|
157 |
+
}
|
158 |
+
)
|
159 |
+
|
160 |
+
# Criar plots
|
161 |
+
hist_fig, line_fig = self.create_plots(hist_data, line_data)
|
162 |
+
|
163 |
+
return (
|
164 |
+
None, # output_image
|
165 |
+
f"Total de Inferências: {metrics['total_inferences']}",
|
166 |
+
hist_fig,
|
167 |
+
line_fig,
|
168 |
+
)
|
169 |
+
|
170 |
+
def create_plots(self, hist_data, line_data):
|
171 |
+
"""Helper method to create plots"""
|
172 |
+
plt.style.use("dark_background")
|
173 |
+
|
174 |
+
# Histograma
|
175 |
+
hist_fig, hist_ax = plt.subplots(figsize=(8, 4), facecolor="#f0f0f5")
|
176 |
+
hist_ax.set_facecolor("#f0f0f5")
|
177 |
+
hist_data.hist(
|
178 |
+
bins=20, ax=hist_ax, color="#4F46E5", alpha=0.7, edgecolor="white"
|
179 |
+
)
|
180 |
+
hist_ax.set_title(
|
181 |
+
"Distribuição dos Tempos de Inferência",
|
182 |
+
pad=15,
|
183 |
+
fontsize=12,
|
184 |
+
color="#1f2937",
|
185 |
+
)
|
186 |
+
hist_ax.set_xlabel("Tempo (ms)", color="#374151")
|
187 |
+
hist_ax.set_ylabel("Frequência", color="#374151")
|
188 |
+
hist_ax.tick_params(colors="#4b5563")
|
189 |
+
hist_ax.grid(True, linestyle="--", alpha=0.3)
|
190 |
+
|
191 |
+
# Gráfico de linha
|
192 |
+
line_fig, line_ax = plt.subplots(figsize=(8, 4), facecolor="#f0f0f5")
|
193 |
+
line_ax.set_facecolor("#f0f0f5")
|
194 |
+
line_data.plot(
|
195 |
+
x="Inferência",
|
196 |
+
y="Tempo (ms)",
|
197 |
+
ax=line_ax,
|
198 |
+
color="#4F46E5",
|
199 |
+
alpha=0.7,
|
200 |
+
label="Tempo",
|
201 |
+
)
|
202 |
+
line_data.plot(
|
203 |
+
x="Inferência",
|
204 |
+
y="Média",
|
205 |
+
ax=line_ax,
|
206 |
+
color="#DC2626",
|
207 |
+
linestyle="--",
|
208 |
+
label="Média",
|
209 |
+
)
|
210 |
+
line_ax.set_title(
|
211 |
+
"Tempo de Inferência por Execução", pad=15, fontsize=12, color="#1f2937"
|
212 |
+
)
|
213 |
+
line_ax.set_xlabel("Número da Inferência", color="#374151")
|
214 |
+
line_ax.set_ylabel("Tempo (ms)", color="#374151")
|
215 |
+
line_ax.tick_params(colors="#4b5563")
|
216 |
+
line_ax.grid(True, linestyle="--", alpha=0.3)
|
217 |
+
line_ax.legend(frameon=True, facecolor="#f0f0f5", edgecolor="none")
|
218 |
+
|
219 |
+
hist_fig.tight_layout()
|
220 |
+
line_fig.tight_layout()
|
221 |
+
|
222 |
+
return hist_fig, line_fig
|
223 |
+
|
224 |
def preprocess(self, img):
|
225 |
# Convert PIL Image to cv2 format
|
226 |
img_cv2 = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
|
389 |
}
|
390 |
)
|
391 |
|
392 |
+
# Criar plots
|
393 |
+
hist_fig, line_fig = detector.create_plots(hist_data, line_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
394 |
|
395 |
return (
|
396 |
output_image,
|
|
|
512 |
outputs=[output_image, total_inferences, hist_plot, line_plot],
|
513 |
)
|
514 |
|
515 |
+
# Carregar métricas iniciais ao carregar a página
|
516 |
+
iface.load(
|
517 |
+
fn=detector.load_initial_metrics,
|
518 |
+
inputs=None,
|
519 |
+
outputs=[output_image, total_inferences, hist_plot, line_plot],
|
520 |
+
)
|
521 |
+
|
522 |
return iface
|
523 |
|
524 |
|