File size: 963 Bytes
44a9798
7fefcad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a9798
 
 
7fefcad
 
44a9798
 
7fefcad
44a9798
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from transformers import pipeline
import gradio as gr

# Load the model pipeline
pipe = pipeline("image-classification", "dima806/medicinal_plants_image_detection")

# Define the image classification function
def image_classifier(image):
    # Perform image classification
    outputs = pipe(image)
    results = {}
    for result in outputs:
        results[result['label']] = result['score']
    return results

# Define FastAPI app
app = FastAPI()

# Define Gradio Interface
gr_interface = gr.Interface(fn=image_classifier, inputs=gr.inputs.Image(), outputs="label")

# Define route for Gradio interface
@app.get("/")
async def gr_interface_route(request: Request):
    return HTMLResponse(gr_interface.launch(request))

# Expose the FastAPI app using Uvicorn (for local testing)
# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)