Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,20 @@ import torch.nn as nn
|
|
4 |
from torchvision import transforms, models
|
5 |
import pickle
|
6 |
from resnest.torch import resnest50
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
with open('class_names.pkl', 'rb') as f:
|
9 |
class_names = pickle.load(f)
|
@@ -66,6 +80,42 @@ def predict_image(img):
|
|
66 |
f"Top 3: {results}\n"
|
67 |
f"------------------------\n")
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
return best_class, best_conf, results
|
70 |
|
71 |
# 创建Gradio界面
|
|
|
4 |
from torchvision import transforms, models
|
5 |
import pickle
|
6 |
from resnest.torch import resnest50
|
7 |
+
import mysql.connector
|
8 |
+
from datetime import datetime
|
9 |
+
import os
|
10 |
+
|
11 |
+
|
12 |
+
def get_db_connection():
|
13 |
+
return mysql.connector.connect(
|
14 |
+
host=os.environ['DB_HOST'],
|
15 |
+
port=os.environ['DB_PORT'],
|
16 |
+
user=os.environ['DB_USER'],
|
17 |
+
password=os.environ['DB_PASSWORD'],
|
18 |
+
database=os.environ['DB_NAME']
|
19 |
+
)
|
20 |
+
|
21 |
|
22 |
with open('class_names.pkl', 'rb') as f:
|
23 |
class_names = pickle.load(f)
|
|
|
80 |
f"Top 3: {results}\n"
|
81 |
f"------------------------\n")
|
82 |
|
83 |
+
try:
|
84 |
+
conn = get_db_connection()
|
85 |
+
cursor = conn.cursor()
|
86 |
+
|
87 |
+
# 创建表(如果尚未创建)
|
88 |
+
cursor.execute('''
|
89 |
+
CREATE TABLE IF NOT EXISTS predictions (
|
90 |
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
91 |
+
predicted_class VARCHAR(255),
|
92 |
+
confidence FLOAT,
|
93 |
+
top3_results TEXT,
|
94 |
+
timestamp DATETIME
|
95 |
+
)
|
96 |
+
''')
|
97 |
+
|
98 |
+
# 插入数据
|
99 |
+
insert_query = '''
|
100 |
+
INSERT INTO predictions
|
101 |
+
(predicted_class, confidence, top3_results, timestamp)
|
102 |
+
VALUES (%s, %s, %s, %s)
|
103 |
+
'''
|
104 |
+
cursor.execute(insert_query, (
|
105 |
+
best_class,
|
106 |
+
best_conf,
|
107 |
+
str(results),
|
108 |
+
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
109 |
+
))
|
110 |
+
|
111 |
+
conn.commit()
|
112 |
+
except Exception as e:
|
113 |
+
print(f"Database error: {str(e)}")
|
114 |
+
finally:
|
115 |
+
if conn.is_connected():
|
116 |
+
cursor.close()
|
117 |
+
conn.close()
|
118 |
+
|
119 |
return best_class, best_conf, results
|
120 |
|
121 |
# 创建Gradio界面
|