Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- Dockerfile +17 -0
- app.py +54 -0
- flower_species_model.h5 +3 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy the current directory contents into the container at /app
|
8 |
+
COPY . /app
|
9 |
+
|
10 |
+
# Install any needed packages specified in requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Make port 8000 available to the world outside this container
|
14 |
+
EXPOSE 8000
|
15 |
+
|
16 |
+
# Run app.py when the container launches
|
17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
# from tensorflow.keras.models import load_model
|
4 |
+
# from tensorflow.keras.preprocessing import image
|
5 |
+
import numpy as np
|
6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
7 |
+
import io
|
8 |
+
import os
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["http://localhost:3000"], # Add your frontend URL
|
15 |
+
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"],
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
# Load your trained model
|
22 |
+
# model = load_model('flower_species_model.h5')
|
23 |
+
|
24 |
+
# def preprocess_image(img_file):
|
25 |
+
# img = image.load_img(img_file, target_size=(64, 64))
|
26 |
+
# img_array = image.img_to_array(img)
|
27 |
+
# img_array = np.expand_dims(img_array, axis=0)
|
28 |
+
# img_array /= 255.0
|
29 |
+
# return img_array
|
30 |
+
|
31 |
+
@app.post("/predict")
|
32 |
+
async def predict(files: list[UploadFile] = File(...)):
|
33 |
+
if not files:
|
34 |
+
return JSONResponse(content={"error": "No files uploaded"}, status_code=400)
|
35 |
+
|
36 |
+
predictions = []
|
37 |
+
for file in files:
|
38 |
+
contents = await file.read()
|
39 |
+
img = io.BytesIO(contents)
|
40 |
+
# preprocessed_img = preprocess_image(img)
|
41 |
+
# prediction = model.predict(preprocessed_img)
|
42 |
+
# predictions.append(prediction[0][0])
|
43 |
+
print("File uploaded")
|
44 |
+
|
45 |
+
threshold = 0.5
|
46 |
+
# predicted_classes = [1 if p > threshold else 0 for p in predictions]
|
47 |
+
# percentage_class_1 = (predicted_classes.count(1) / len(predicted_classes)) * 100
|
48 |
+
|
49 |
+
# return {"percentage_class_1": round(percentage_class_1, 2)}
|
50 |
+
return {"message": "Files uploaded", "percentage": 100}
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
import uvicorn
|
54 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
flower_species_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4b52398edd4b1826b7cfd3a56e88a53936f016e17cf21a87ca483845353c3201
|
3 |
+
size 8251576
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
python-multipart
|
4 |
+
numpy
|
5 |
+
tensorflow
|