Spaces:
Sleeping
Sleeping
randomshit11
commited on
Commit
•
798b314
1
Parent(s):
f429adb
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,27 @@
|
|
1 |
-
|
2 |
-
from fastapi
|
3 |
-
from
|
4 |
-
import
|
5 |
|
6 |
-
# Load the model pipeline
|
7 |
-
pipe = pipeline("image-classification", "dima806/medicinal_plants_image_detection")
|
8 |
-
|
9 |
-
# Define the image classification function
|
10 |
-
def image_classifier(image):
|
11 |
-
# Perform image classification
|
12 |
-
outputs = pipe(image)
|
13 |
-
results = {}
|
14 |
-
for result in outputs:
|
15 |
-
results[result['label']] = result['score']
|
16 |
-
return results
|
17 |
-
|
18 |
-
# Define FastAPI app
|
19 |
app = FastAPI()
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# Define route for Gradio interface
|
25 |
@app.get("/")
|
26 |
-
async def
|
27 |
-
return
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
#
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from fastapi import FastAPI, File, UploadFile
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from predict import read_image, transformacao
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Add CORS middleware to allow requests from any origin (for development)
|
9 |
+
app.add_middleware(
|
10 |
+
CORSMiddleware,
|
11 |
+
allow_origins=["*"],
|
12 |
+
allow_methods=["*"],
|
13 |
+
allow_headers=["*"],
|
14 |
+
)
|
15 |
|
|
|
16 |
@app.get("/")
|
17 |
+
async def root():
|
18 |
+
return {"message": "Welcome to the medicinal plants image detection API!"}
|
19 |
|
20 |
+
@app.post("/uploadfile/")
|
21 |
+
async def create_upload_file(file: UploadFile = File(...)):
|
22 |
+
contents = await file.read()
|
23 |
+
# read image
|
24 |
+
imagem = read_image(contents)
|
25 |
+
# transform and prediction
|
26 |
+
prediction = transformacao(imagem)
|
27 |
+
return prediction
|