from fastapi import FastAPI, UploadFile, File import shutil import os from image_caption import generate_caption app = FastAPI() UPLOAD_DIR = "uploads" os.makedirs(UPLOAD_DIR, exist_ok=True) @app.post("/upload-image/") async def upload_image(file: UploadFile = File(...)): """Endpoint to accept image and return generated caption""" file_path = f"{UPLOAD_DIR}/{file.filename}" # Save the uploaded file with open(file_path, "wb") as buffer: shutil.copyfileobj(file.file, buffer) # Generate caption caption = generate_caption(file_path) return {"filename": file.filename, "caption": caption} @app.get("/") async def root(): return {"message": "Image-to-Text API is running. Use /upload-image to send an image."}