Compare commits
5 Commits
2712a026ab
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c6e6432f19 | |||
| 2f7aab5891 | |||
| a2b6730b88 | |||
| cb1294bd4d | |||
| 7e12ef3738 |
@@ -20,4 +20,5 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Set the entrypoint
|
# Set the entrypoint
|
||||||
CMD ["python", "start_moondream.py"]
|
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "3000"]
|
||||||
|
|
||||||
|
|||||||
33
app (1).py
Normal file
33
app (1).py
Normal file
@@ -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)})
|
||||||
57
app (2).py
Normal file
57
app (2).py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import os
|
||||||
|
import io
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from fastapi import FastAPI, File, UploadFile, Form, Query
|
||||||
|
from fastapi.responses import JSONResponse, StreamingResponse
|
||||||
|
from PIL import Image
|
||||||
|
import moondream as md
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
image_bytes = await image.read()
|
||||||
|
img = Image.open(io.BytesIO(image_bytes))
|
||||||
|
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)})
|
||||||
|
|
||||||
|
@app.post("/query")
|
||||||
|
async def query_image(
|
||||||
|
image: UploadFile = File(...),
|
||||||
|
question: str = Form(...),
|
||||||
|
stream: bool = Form(False)
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
image_bytes = await image.read()
|
||||||
|
img = Image.open(io.BytesIO(image_bytes))
|
||||||
|
|
||||||
|
if stream:
|
||||||
|
def generate():
|
||||||
|
result = model.query(img, question, stream=True)
|
||||||
|
for chunk in result["chunk"]:
|
||||||
|
yield chunk
|
||||||
|
|
||||||
|
return StreamingResponse(generate(), media_type="text/plain")
|
||||||
|
else:
|
||||||
|
result = model.query(img, question)
|
||||||
|
return JSONResponse(content={
|
||||||
|
"answer": result["answer"],
|
||||||
|
"request_id": result["request_id"]
|
||||||
|
})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return JSONResponse(status_code=500, content={"error": str(e)})
|
||||||
64
app.py
Normal file
64
app.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import os
|
||||||
|
import io
|
||||||
|
import base64
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from fastapi import FastAPI, HTTPException
|
||||||
|
from fastapi.responses import JSONResponse, StreamingResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from PIL import Image
|
||||||
|
import moondream as md
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
# Utility to decode base64
|
||||||
|
def decode_base64_image(base64_str: str) -> Image.Image:
|
||||||
|
try:
|
||||||
|
image_data = base64.b64decode(base64_str.split(",")[-1])
|
||||||
|
return Image.open(io.BytesIO(image_data))
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError("Invalid base64 image") from e
|
||||||
|
|
||||||
|
# Request schemas
|
||||||
|
class CaptionRequest(BaseModel):
|
||||||
|
base64_image: str
|
||||||
|
length: str = "short"
|
||||||
|
|
||||||
|
class QueryRequest(BaseModel):
|
||||||
|
base64_image: str
|
||||||
|
question: str
|
||||||
|
stream: bool = False
|
||||||
|
|
||||||
|
@app.post("/caption")
|
||||||
|
async def generate_caption(payload: CaptionRequest):
|
||||||
|
try:
|
||||||
|
img = decode_base64_image(payload.base64_image)
|
||||||
|
response = model.caption(img, length=payload.length)
|
||||||
|
return {"caption": response["caption"]}
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
@app.post("/query")
|
||||||
|
async def query_image(payload: QueryRequest):
|
||||||
|
try:
|
||||||
|
img = decode_base64_image(payload.base64_image)
|
||||||
|
|
||||||
|
if payload.stream:
|
||||||
|
def generate():
|
||||||
|
result = model.query(img, payload.question, stream=True)
|
||||||
|
for chunk in result["chunk"]:
|
||||||
|
yield chunk
|
||||||
|
return StreamingResponse(generate(), media_type="text/plain")
|
||||||
|
else:
|
||||||
|
result = model.query(img, payload.question)
|
||||||
|
return {"answer": result["answer"]}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
moondream
|
moondream
|
||||||
Pillow
|
Pillow
|
||||||
python-dotenv
|
python-dotenv
|
||||||
|
fastapi
|
||||||
|
uvicorn
|
||||||
|
python-multipart
|
||||||
|
pydantic
|
||||||
@@ -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"])
|
|
||||||
Reference in New Issue
Block a user