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)