Upload 3 files
Browse files- Dockerfile +30 -0
- main.py +42 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use Python 3.10.12 as the base image
|
2 |
+
FROM python:3.9.13
|
3 |
+
|
4 |
+
# Set the working directory to /code
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
# Copy the requirements file into the container at /code/requirements.txt
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
# Upgrade pip and install the dependencies
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
12 |
+
|
13 |
+
# Create a non-root user with UID 1000
|
14 |
+
RUN useradd -m -u 1000 user
|
15 |
+
|
16 |
+
# Switch to the non-root user
|
17 |
+
USER user
|
18 |
+
|
19 |
+
# Set environment variables for the user
|
20 |
+
ENV HOME=/home/user \
|
21 |
+
PATH=/home/user/.local/bin:$PATH
|
22 |
+
|
23 |
+
# Set the working directory for the application
|
24 |
+
WORKDIR $HOME/app
|
25 |
+
|
26 |
+
# Copy the local code into the container at /home/user/app
|
27 |
+
COPY --chown=user . $HOME/app
|
28 |
+
|
29 |
+
# Specify the command to run on container start
|
30 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, HTTPException, Body
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from transformers import pipeline
|
5 |
+
from PIL import Image
|
6 |
+
import base64
|
7 |
+
from io import BytesIO
|
8 |
+
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# Load the image classification pipeline
|
12 |
+
classifier = pipeline(model="Diginsa/Plant-Disease-Detection-Project")
|
13 |
+
|
14 |
+
# CORS configuration
|
15 |
+
origins = [""] # Replace "" with the actual list of allowed origins
|
16 |
+
|
17 |
+
app.add_middleware(
|
18 |
+
CORSMiddleware,
|
19 |
+
allow_origins=origins,
|
20 |
+
allow_credentials=True,
|
21 |
+
allow_methods=["*"],
|
22 |
+
allow_headers=["*"],
|
23 |
+
)
|
24 |
+
|
25 |
+
# Endpoint to perform image classification
|
26 |
+
@app.post("/classify")
|
27 |
+
async def classify_image(encoded_image: str= Body(..., embed=True)):
|
28 |
+
try:
|
29 |
+
# Decode the base64 encoded image string
|
30 |
+
decoded_image = base64.b64decode(encoded_image)
|
31 |
+
|
32 |
+
# Create an Image object from the decoded content
|
33 |
+
image = Image.open(BytesIO(decoded_image))
|
34 |
+
|
35 |
+
# Use the classifier with the decoded image
|
36 |
+
result = classifier(images=image)
|
37 |
+
|
38 |
+
# Return the classification result as JSON
|
39 |
+
return JSONResponse(content=result, status_code=200)
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
transformers
|
3 |
+
pillow
|
4 |
+
uvicorn
|