-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (63 loc) · 2.43 KB
/
Copy pathmain.py
File metadata and controls
78 lines (63 loc) · 2.43 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
from fastapi import FastAPI, UploadFile, File
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from datetime import datetime
from ultralytics import YOLO
from PIL import Image
import sqlite3
import io
app = FastAPI()
model = YOLO("runs/classify/train/weights/best.pt")
LABELS = {0: "Benign", 1: "Malignant"}
app.mount("/static", StaticFiles(directory="Static"), name="static")
@app.get("/") # Serve the dashboard HTML page
def serve_dashboard():
return FileResponse("Static/index.html")
@app.post("/images") # API endpoint for image upload and prediction
async def upload_image(file: UploadFile = File(...)):
contents = await file.read()
image = Image.open(io.BytesIO(contents))
results = model(image)
probs = results[0].probs
prediction = LABELS[probs.top1]
confidence = float(probs.top1conf)
log_prediction(file.filename, prediction, confidence)
return {"prediction": prediction, "confidence": confidence}
def log_prediction(filename, prediction, confidence): # Log predictions to SQLite database
con = sqlite3.connect("predictions.db")
cur = con.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS predictions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT,
prediction TEXT,
confidence REAL,
timestamp TEXT
)
""")
cur.execute(
"INSERT INTO predictions VALUES (NULL, ?, ?, ?, ?)",
(filename, prediction, confidence, datetime.now().isoformat())
)
con.commit()
con.close()
@app.get("/dashboard/stats")
def get_stats():
con = sqlite3.connect("predictions.db")
cur = con.cursor()
total = cur.execute("SELECT COUNT(*) FROM predictions").fetchone()[0]
benign = cur.execute("SELECT COUNT(*) FROM predictions WHERE prediction='benign'").fetchone()[0]
malignant = cur.execute("SELECT COUNT(*) FROM predictions WHERE prediction='malignant'").fetchone()[0]
avg_conf = cur.execute("SELECT AVG(confidence) FROM predictions").fetchone()[0]
recent = cur.execute("SELECT * FROM predictions ORDER BY id DESC LIMIT 10").fetchall()
con.close()
return {
"total": total,
"benign": benign,
"malignant": malignant,
"avg_confidence": round(avg_conf, 3),
"recent": recent
}
if __name__ == "__main__": # Run the FastAPI app using Uvicorn
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)