File size: 1,417 Bytes
416d779 a0dc8ce 54d4cab 1d48320 d44e115 805c775 bc1fd68 ba9ac6e 805c775 1d48320 ad9892d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import JSONResponse
from gradio_client import Client
import os
import tempfile
import shutil
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import FileResponse
app = FastAPI()
client = Client("Ashrafb/moondream_captioning")
# Create the "uploads" directory if it doesn't exist
os.makedirs("uploads", exist_ok=True)
# Define a function to save uploaded file
async def save_upload_file(upload_file: UploadFile) -> str:
file_path = os.path.join("uploads", upload_file.filename)
with open(file_path, "wb") as buffer:
buffer.write(await upload_file.read())
return file_path
@app.post("/get_caption")
async def get_caption(image: UploadFile = File(...), context: str = None):
# Save the uploaded image to a temporary file
file_path = await save_upload_file(image)
# Pass the file path to the client for prediction
result = client.predict(file_path, context, api_name="/get_caption")
return {"caption": result}
app.mount("/", StaticFiles(directory="static", html=True), name="static")
@app.get("/")
def index() -> FileResponse:
return FileResponse(path="/app/static/index.html", media_type="text/html")
|