from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.responses import HTMLResponse from transformers import pipeline from PIL import Image import io app = FastAPI() # Load the image classification pipeline pipe = pipeline("image-classification", model="mateoluksenberg/dit-base-Classifier_CM05") @app.post("/classify/") async def classify_image(file: UploadFile = File(...)): try: # Read the file contents into a PIL image image = Image.open(file.file).convert('RGB') # Perform image classification result = pipe(image) # Overall result summary overall_result = "All results: " + str(result) result_with_comment = { "label": result[0]['label'], "score": result[0]['score'], "comment": overall_result } return {"classification_result": result_with_comment} # Return the top prediction with comment and overall summary except Exception as e: # Handle exceptions, for example: file not found, image format issues, etc. raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}") @app.get("/", response_class=HTMLResponse) async def home(): html_content = """