randomshit11 commited on
Commit
798b314
1 Parent(s): f429adb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -1,32 +1,27 @@
1
- from fastapi import FastAPI, Request
2
- from fastapi.responses import HTMLResponse
3
- from transformers import pipeline
4
- import gradio as gr
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
- # Define Gradio Interface
22
- gr_interface = gr.Interface(fn=image_classifier, inputs=gr.inputs.Image(), outputs="label")
 
 
 
 
 
23
 
24
- # Define route for Gradio interface
25
  @app.get("/")
26
- async def gr_interface_route(request: Request):
27
- return HTMLResponse(gr_interface.launch(request))
28
 
29
- # Expose the FastAPI app using Uvicorn (for local testing)
30
- # if __name__ == "__main__":
31
- # import uvicorn
32
- # uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
 
 
 
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