Spaces:
Sleeping
Sleeping
Commit ·
aa84dd7
1
Parent(s): 2a2be11
delete
Browse files
app.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import numpy as np
|
| 3 |
-
import tensorflow as tf
|
| 4 |
-
from tensorflow.keras.models import load_model # type: ignore
|
| 5 |
-
from tensorflow.keras.preprocessing import image # type: ignore
|
| 6 |
-
from tensorflow.keras.applications.densenet import preprocess_input # type: ignore
|
| 7 |
-
|
| 8 |
-
from fastapi import FastAPI, File, UploadFile, Request
|
| 9 |
-
from fastapi.templating import Jinja2Templates
|
| 10 |
-
from fastapi.responses import HTMLResponse
|
| 11 |
-
import uvicorn
|
| 12 |
-
|
| 13 |
-
# Initialize FastAPI app
|
| 14 |
-
app = FastAPI()
|
| 15 |
-
|
| 16 |
-
# Set up template rendering (similar to Flask’s render_template)
|
| 17 |
-
templates = Jinja2Templates(directory="templates")
|
| 18 |
-
|
| 19 |
-
# Define paths
|
| 20 |
-
BASE_DIR = os.path.dirname(__file__)
|
| 21 |
-
MODEL_PATH = os.path.join(BASE_DIR, 'uploads', "densenet_ship.h5")
|
| 22 |
-
|
| 23 |
-
# Load the model
|
| 24 |
-
model = load_model(MODEL_PATH)
|
| 25 |
-
|
| 26 |
-
# Define ship categories
|
| 27 |
-
val_dict = {
|
| 28 |
-
0: 'Aircraft Carrier',
|
| 29 |
-
1: 'Bulkers',
|
| 30 |
-
2: 'Car Carrier',
|
| 31 |
-
3: 'Container Ship',
|
| 32 |
-
4: 'Cruise',
|
| 33 |
-
5: 'DDG',
|
| 34 |
-
6: 'Recreational',
|
| 35 |
-
7: 'Sailboat',
|
| 36 |
-
8: 'Submarine',
|
| 37 |
-
9: 'Tug'
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
# Define Routes
|
| 41 |
-
|
| 42 |
-
@app.get("/", response_class=HTMLResponse)
|
| 43 |
-
async def index(request: Request):
|
| 44 |
-
return templates.TemplateResponse("index.html", {"request": request})
|
| 45 |
-
|
| 46 |
-
@app.get("/about", response_class=HTMLResponse)
|
| 47 |
-
async def about(request: Request):
|
| 48 |
-
return templates.TemplateResponse("about.html", {"request": request})
|
| 49 |
-
|
| 50 |
-
@app.get("/service", response_class=HTMLResponse)
|
| 51 |
-
async def service(request: Request):
|
| 52 |
-
return templates.TemplateResponse("service.html", {"request": request})
|
| 53 |
-
|
| 54 |
-
@app.post("/predict/")
|
| 55 |
-
async def predict(image_file: UploadFile = File(...)):
|
| 56 |
-
# Save uploaded file
|
| 57 |
-
file_path = os.path.join(BASE_DIR, 'uploads', image_file.filename)
|
| 58 |
-
|
| 59 |
-
with open(file_path, "wb") as buffer:
|
| 60 |
-
buffer.write(await image_file.read())
|
| 61 |
-
|
| 62 |
-
# Load and preprocess image
|
| 63 |
-
img = image.load_img(file_path, target_size=(224, 224))
|
| 64 |
-
img = image.img_to_array(img)
|
| 65 |
-
img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))
|
| 66 |
-
img = preprocess_input(img)
|
| 67 |
-
|
| 68 |
-
# Make prediction
|
| 69 |
-
pred = model.predict(img)
|
| 70 |
-
pred = pred.flatten()
|
| 71 |
-
|
| 72 |
-
# Get predicted category
|
| 73 |
-
predicted_class = val_dict[np.argmax(pred)]
|
| 74 |
-
|
| 75 |
-
# Return result
|
| 76 |
-
return {"category": predicted_class, "message": f"The Ship Category is {predicted_class}"}
|
| 77 |
-
|
| 78 |
-
# Run the FastAPI app with uvicorn (needed when not using Docker Spaces)
|
| 79 |
-
if __name__ == "__main__":
|
| 80 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|