Spaces:
Sleeping
Sleeping
mateoluksenberg
commited on
Commit
•
3fc5824
1
Parent(s):
6d73772
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,43 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
-
def
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|