File size: 935 Bytes
7fefcad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
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 Gradio Interface
gr_interface = gr.Interface(fn=image_classifier, inputs=gr.Image(type="pil"), outputs="label")

# Define FastAPI app
app = FastAPI()

# Define route for Gradio interface
@app.get("/")
async def gr_interface_route():
    return HTMLResponse(gr_interface.launch(inline=False, inbrowser=True))

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