File size: 1,075 Bytes
9182674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from orbiitt_engine import OrbiittEngine
import shutil
import os

app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])

# Mount your specific folder
app.mount("/Productimages", StaticFiles(directory="Productimages"), name="Productimages")

engine = OrbiittEngine()

@app.post("/search")
async def search_endpoint(text: str = Form(None), weight: float = Form(0.5), file: UploadFile = File(None)):
    temp_path = None
    if file:
        temp_path = f"temp_{file.filename}"
        with open(temp_path, "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)

    results = engine.search(text_query=text, image_file=temp_path, text_weight=weight)
    
    if temp_path and os.path.exists(temp_path):
        os.remove(temp_path)
    return {"results": results}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)