Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,7 +23,24 @@ def generate(text: str):
|
|
| 23 |
|
| 24 |
@app.post("/uploadfile/")
|
| 25 |
async def create_upload_file(file: UploadFile = File(...)):
|
|
|
|
| 26 |
request_object_content = await file.read()
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return dict(zip(categories, map(float, probs)))
|
|
|
|
| 23 |
|
| 24 |
@app.post("/uploadfile/")
|
| 25 |
async def create_upload_file(file: UploadFile = File(...)):
|
| 26 |
+
# Read the uploaded file content
|
| 27 |
request_object_content = await file.read()
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Attempt to open the image
|
| 31 |
+
img = Image.open(io.BytesIO(request_object_content))
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return {"error": "Failed to open the image file. Make sure it is a valid image file."}
|
| 34 |
+
|
| 35 |
+
# Check if img is None or not
|
| 36 |
+
if img is None:
|
| 37 |
+
return {"error": "Failed to open the image file."}
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# Assuming 'learn' is your image classifier model
|
| 41 |
+
pred, idx, probs = learn.predict(img)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return {"error": "Failed to make predictions."}
|
| 44 |
+
|
| 45 |
+
# Assuming categories is a list of category labels
|
| 46 |
return dict(zip(categories, map(float, probs)))
|