Spaces:
Sleeping
Sleeping
mateoluksenberg
commited on
Commit
•
7b48e6a
1
Parent(s):
5420729
Upload 4 files
Browse files- Dockerfile +30 -0
- app.py +43 -0
- cm5.jpg +0 -0
- requirements.txt +0 -0
Dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Use the official Python 3.9 image
|
2 |
+
FROM python:3.10
|
3 |
+
|
4 |
+
## set the working directory to /code
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
## Copy the current directory contents in the container at /code
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
## Install the requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
12 |
+
|
13 |
+
# Set up a new user named "user"
|
14 |
+
RUN useradd user
|
15 |
+
# Switch to the "user" user
|
16 |
+
USER user
|
17 |
+
|
18 |
+
# Set home to the user's home directory
|
19 |
+
|
20 |
+
ENV HOME=/home/user \
|
21 |
+
PATH=/home/user/.local/bin:$PATH
|
22 |
+
|
23 |
+
# Set the working directory to the user's home directory
|
24 |
+
WORKDIR $HOME/app
|
25 |
+
|
26 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
27 |
+
COPY --chown=user . $HOME/app
|
28 |
+
|
29 |
+
## Start the FASTAPI App on port 7860
|
30 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Load the image classification pipeline
|
9 |
+
pipe = pipeline("image-classification", model="mateoluksenberg/dit-base-Classifier_CM05")
|
10 |
+
|
11 |
+
# Sample image path (for testing)
|
12 |
+
image_path = 'cm5.jpg'
|
13 |
+
|
14 |
+
# Async function to classify an image
|
15 |
+
async def classify_image(image_path: str):
|
16 |
+
try:
|
17 |
+
image = Image.open(image_path).convert('RGB')
|
18 |
+
|
19 |
+
image_bytes = io.BytesIO()
|
20 |
+
image.save(image_bytes, format='JPEG')
|
21 |
+
image_bytes = image_bytes.getvalue()
|
22 |
+
|
23 |
+
# Perform image classification
|
24 |
+
result = pipe(image_bytes)
|
25 |
+
|
26 |
+
return result[0] # Return the top prediction
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
# Handle exceptions, for example: file not found, image format issues, etc.
|
30 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
31 |
+
|
32 |
+
@app.get("/")
|
33 |
+
async def home(image_path: str = image_path):
|
34 |
+
try:
|
35 |
+
result = await classify_image(image_path)
|
36 |
+
return {"message": "Hello World", "classification_result": result}
|
37 |
+
|
38 |
+
except HTTPException as e:
|
39 |
+
raise e
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
raise HTTPException(status_code=500, detail=f"Error classifying image: {str(e)}")
|
43 |
+
|
cm5.jpg
ADDED
requirements.txt
ADDED
File without changes
|