From 7e12ef3738e2d4e947b2c10e09b57c101af2e085 Mon Sep 17 00:00:00 2001 From: kar Date: Mon, 21 Apr 2025 16:58:51 +0530 Subject: [PATCH] PAI --- Dockerfile | 3 ++- app.py | 33 +++++++++++++++++++++++++++++++++ requirements.txt | 4 +++- start_moondream.py | 20 -------------------- 4 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 app.py delete mode 100644 start_moondream.py diff --git a/Dockerfile b/Dockerfile index ac64d62..f61a4a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,4 +20,5 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . # Set the entrypoint -CMD ["python", "start_moondream.py"] +CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "3000"] + diff --git a/app.py b/app.py new file mode 100644 index 0000000..76aa557 --- /dev/null +++ b/app.py @@ -0,0 +1,33 @@ +import os +from dotenv import load_dotenv +from fastapi import FastAPI, File, UploadFile, Form +from fastapi.responses import JSONResponse +from PIL import Image +import moondream as md +import io + +# Load environment variables +load_dotenv() +api_key = os.getenv("MOON_DREAM_KEY") + +# Initialize Moondream model +model = md.vl(api_key=api_key) + +# FastAPI app +app = FastAPI() + +@app.post("/caption") +async def generate_caption( + image: UploadFile = File(...), + length: str = Form("short") +): + try: + # Read the uploaded image + image_bytes = await image.read() + img = Image.open(io.BytesIO(image_bytes)) + + # Generate caption + response = model.caption(img, length=length) + return JSONResponse(content={"caption": response["caption"]}) + except Exception as e: + return JSONResponse(status_code=500, content={"error": str(e)}) diff --git a/requirements.txt b/requirements.txt index faa2c2a..b36e2a2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ moondream Pillow python-dotenv - +fastapi +uvicorn +python-multipart \ No newline at end of file diff --git a/start_moondream.py b/start_moondream.py deleted file mode 100644 index 2147248..0000000 --- a/start_moondream.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -from dotenv import load_dotenv -import moondream as md -from PIL import Image - -# Load .env -load_dotenv() - -# Get key from env -api_key = os.getenv("MOON_DREAM_KEY") - -# Use Moondream cloud model -model = md.vl(api_key=api_key) - -# Load image -image = Image.open("nat.jpg") - -# Generate caption -caption_response = model.caption(image, length="short") -print(caption_response["caption"])