aniketkumar1106 commited on
Commit
36680f9
Β·
verified Β·
1 Parent(s): 427116d

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +20 -19
server.py CHANGED
@@ -18,9 +18,9 @@ logger = logging.getLogger(__name__)
18
  DATASET_REPO = "aniketkumar1106/orbit-data"
19
  IMAGE_DIR = "Productimages"
20
  DB_TARGET = "orbiitt.db"
21
- MIN_CONFIDENCE_THRESHOLD = 0.22
22
 
23
- # BOOTSTRAP: Mandatory folder creation before FastAPI initialization
24
  os.makedirs(IMAGE_DIR, exist_ok=True)
25
 
26
  app = FastAPI()
@@ -38,24 +38,22 @@ engine = None
38
  loading_status = "System Booting..."
39
 
40
  def normalize_filename(name):
41
- """Deep normalization to match Database paths with Disk paths."""
42
  if not name: return ""
43
  name = urllib.parse.unquote(name)
44
  name = name.replace('’', "'").replace('β€˜', "'").replace('β€œ', '"').replace('”', '"').replace('–', '-')
45
  return unicodedata.normalize('NFKD', name).encode('ascii', 'ignore').decode('ascii').strip()
46
 
47
- # 3. THREADED BACKGROUND INITIALIZATION
48
  def background_sync():
49
  global engine, loading_status
50
  token = os.environ.get("HF_TOKEN")
51
 
52
- # Wipe contents but keep directory for Mount
53
  for f in os.listdir(IMAGE_DIR):
54
  p = os.path.join(IMAGE_DIR, f)
55
  try:
56
  if os.path.isfile(p): os.unlink(p)
57
  elif os.path.isdir(p): shutil.rmtree(p)
58
- except Exception: pass
59
 
60
  try:
61
  loading_status = "Syncing Assets..."
@@ -75,6 +73,10 @@ def background_sync():
75
  shutil.copy(src, f"./{DB_TARGET}")
76
  shutil.rmtree("temp_extract")
77
 
 
 
 
 
78
  loading_status = "Loading AI Engine..."
79
  try:
80
  from orbiitt_engine import OrbiittEngine
@@ -88,48 +90,47 @@ def background_sync():
88
 
89
  @app.on_event("startup")
90
  async def startup_event():
91
- # Spawns sync thread to keep health checks responsive
92
  thread = threading.Thread(target=background_sync, daemon=True)
93
  thread.start()
94
 
95
- # Mount StaticFiles
96
  app.mount("/Productimages", StaticFiles(directory=IMAGE_DIR), name="Productimages")
97
 
98
  @app.get("/health")
99
  def health():
100
  return {"status": loading_status, "ready": engine is not None}
101
 
102
- # 4. THREAD-SAFE SEARCH LOGIC
103
  @app.post("/search")
104
  async def search(text: str = Form(None), weight: float = Form(0.5), file: UploadFile = File(None)):
105
  if not engine:
106
  raise HTTPException(status_code=503, detail=f"Engine not ready: {loading_status}")
107
 
108
- # Unique buffer path for concurrency
109
- t_path = f"buffer_{os.getpid()}_{threading.get_ident()}.jpg" if file else None
110
 
111
  try:
112
- # Step 1: Intelligent Modality Balancing
113
  actual_weight = weight
114
- if not text and file: actual_weight = 0.0 # Pure Visual
115
- if text and not file: actual_weight = 1.0 # Pure Text
116
 
117
- # Step 2: Thread-Safe Async File Write
118
  if file and t_path:
119
  content = await file.read()
120
  async with await anyio.open_file(t_path, "wb") as f:
121
  await f.write(content)
122
 
123
- # Step 3: Engine Computation (Offloaded to a thread pool)
124
  results = await anyio.to_thread.run_sync(
125
- engine.search, text, t_path, actual_weight, 50
 
 
 
 
 
126
  )
127
 
128
  all_files = os.listdir(IMAGE_DIR)
129
  final_list = []
130
  seen_ids = set()
131
 
132
- # Step 4: Confidence & Deduplication logic
133
  for r in results:
134
  score = r.get('score', 0)
135
  pid = r.get('id', 'Product')
@@ -166,7 +167,7 @@ async def search(text: str = Form(None), weight: float = Form(0.5), file: Upload
166
  finally:
167
  if t_path and os.path.exists(t_path):
168
  try: os.remove(t_path)
169
- except Exception: pass
170
 
171
  if __name__ == "__main__":
172
  import uvicorn
 
18
  DATASET_REPO = "aniketkumar1106/orbit-data"
19
  IMAGE_DIR = "Productimages"
20
  DB_TARGET = "orbiitt.db"
21
+ MIN_CONFIDENCE_THRESHOLD = 0.1
22
 
23
+ # BOOTSTRAP: Mandatory folder creation
24
  os.makedirs(IMAGE_DIR, exist_ok=True)
25
 
26
  app = FastAPI()
 
38
  loading_status = "System Booting..."
39
 
40
  def normalize_filename(name):
 
41
  if not name: return ""
42
  name = urllib.parse.unquote(name)
43
  name = name.replace('’', "'").replace('β€˜', "'").replace('β€œ', '"').replace('”', '"').replace('–', '-')
44
  return unicodedata.normalize('NFKD', name).encode('ascii', 'ignore').decode('ascii').strip()
45
 
46
+ # 3. BACKGROUND INITIALIZATION
47
  def background_sync():
48
  global engine, loading_status
49
  token = os.environ.get("HF_TOKEN")
50
 
 
51
  for f in os.listdir(IMAGE_DIR):
52
  p = os.path.join(IMAGE_DIR, f)
53
  try:
54
  if os.path.isfile(p): os.unlink(p)
55
  elif os.path.isdir(p): shutil.rmtree(p)
56
+ except: pass
57
 
58
  try:
59
  loading_status = "Syncing Assets..."
 
73
  shutil.copy(src, f"./{DB_TARGET}")
74
  shutil.rmtree("temp_extract")
75
 
76
+ # LOGGING FILE COUNT FOR VALIDATION
77
+ final_count = len(os.listdir(IMAGE_DIR))
78
+ logger.info(f"DISK VALIDATION: {final_count} images ready in {IMAGE_DIR}")
79
+
80
  loading_status = "Loading AI Engine..."
81
  try:
82
  from orbiitt_engine import OrbiittEngine
 
90
 
91
  @app.on_event("startup")
92
  async def startup_event():
 
93
  thread = threading.Thread(target=background_sync, daemon=True)
94
  thread.start()
95
 
 
96
  app.mount("/Productimages", StaticFiles(directory=IMAGE_DIR), name="Productimages")
97
 
98
  @app.get("/health")
99
  def health():
100
  return {"status": loading_status, "ready": engine is not None}
101
 
102
+ # 4. FIXED SEARCH LOGIC
103
  @app.post("/search")
104
  async def search(text: str = Form(None), weight: float = Form(0.5), file: UploadFile = File(None)):
105
  if not engine:
106
  raise HTTPException(status_code=503, detail=f"Engine not ready: {loading_status}")
107
 
108
+ t_path = f"buffer_{os.getpid()}.jpg" if file else None
 
109
 
110
  try:
 
111
  actual_weight = weight
112
+ if not text and file: actual_weight = 0.0
113
+ if text and not file: actual_weight = 1.0
114
 
 
115
  if file and t_path:
116
  content = await file.read()
117
  async with await anyio.open_file(t_path, "wb") as f:
118
  await f.write(content)
119
 
120
+ # THE FIX: Use lambda to ensure keyword arguments are passed correctly
121
  results = await anyio.to_thread.run_sync(
122
+ lambda: engine.search(
123
+ text_query=text,
124
+ image_file=t_path,
125
+ text_weight=actual_weight,
126
+ top_k=50
127
+ )
128
  )
129
 
130
  all_files = os.listdir(IMAGE_DIR)
131
  final_list = []
132
  seen_ids = set()
133
 
 
134
  for r in results:
135
  score = r.get('score', 0)
136
  pid = r.get('id', 'Product')
 
167
  finally:
168
  if t_path and os.path.exists(t_path):
169
  try: os.remove(t_path)
170
+ except: pass
171
 
172
  if __name__ == "__main__":
173
  import uvicorn