Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,49 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
import io
|
| 3 |
-
import torch
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from clip_interrogator import Config, Interrogator
|
| 7 |
-
from fastapi import FastAPI
|
| 8 |
-
from fastapi.staticfiles import StaticFiles
|
| 9 |
-
from fastapi.responses import FileResponse
|
| 10 |
-
# Initialize FastAPI app
|
| 11 |
-
app = FastAPI()
|
| 12 |
|
| 13 |
-
# Initialize CLIP Interrogator
|
| 14 |
-
config = Config()
|
| 15 |
-
config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 16 |
-
config.blip_offload = False if torch.cuda.is_available() else True
|
| 17 |
-
config.chunk_size = 2048
|
| 18 |
-
config.flavor_intermediate_count = 512
|
| 19 |
-
config.blip_num_beams = 64
|
| 20 |
-
ci = Interrogator(config)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
@app.post("/inference/")
|
| 24 |
-
async def image_inference(file: UploadFile = File(...), mode: str = 'fast', best_max_flavors: int = 4):
|
| 25 |
-
# Read image file
|
| 26 |
-
image_bytes = await file.read()
|
| 27 |
-
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 28 |
-
|
| 29 |
-
# Perform inference based on mode
|
| 30 |
-
prompt_results = []
|
| 31 |
-
if mode == 'best':
|
| 32 |
-
prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
|
| 33 |
-
prompt_results.append(prompt_result)
|
| 34 |
-
elif mode == 'classic':
|
| 35 |
-
prompt_result = ci.interrogate_classic(image)
|
| 36 |
-
prompt_results.append(prompt_result)
|
| 37 |
-
else:
|
| 38 |
-
prompt_result = ci.interrogate_fast(image)
|
| 39 |
-
prompt_results.append(prompt_result)
|
| 40 |
-
|
| 41 |
-
# Return prompt results
|
| 42 |
-
return {"prompt_results": prompt_results}
|
| 43 |
-
|
| 44 |
-
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 45 |
-
|
| 46 |
-
@app.get("/")
|
| 47 |
-
def index() -> FileResponse:
|
| 48 |
-
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
| 49 |
-
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
exec(os.environ.get('CODE'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|