DrAbbas commited on
Commit
a7a221e
ยท
verified ยท
1 Parent(s): e5e8a52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -1273,6 +1273,9 @@ def yolo11_fast_classify(img, declared_text):
1273
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1274
  # ุฎุฑูŠุทุฉ ุงู„ูƒู„ู…ุงุช ุงู„ู…ูุชุงุญูŠุฉ โ†’ ุงุณู… ุงู„ุตู†ู ููŠ ู‚ุงุนุฏุฉ ุงู„ุจูŠุงู†ุงุช
1275
  DECLARED_KEYWORD_MAP = {
 
 
 
1276
  # ุฃุฑุฒ
1277
  'rice': 'rice', 'ุฃุฑุฒ': 'rice', 'ุฑุฒ': 'rice',
1278
  'sella': 'rice', 'basmati': 'rice', 'ุจุณู…ุชูŠ': 'rice',
@@ -1285,16 +1288,32 @@ def yolo11_fast_classify(img, declared_text):
1285
  'oil': 'oil', 'ุฒูŠุช': 'oil',
1286
  # ุดุงูŠ
1287
  'tea': 'tea', 'ุดุงูŠ': 'tea',
 
 
 
 
 
 
1288
  # ุจู„ุงุณุชูŠูƒ
1289
  'plastic': 'plastic', 'ุจู„ุงุณุชูŠูƒ': 'plastic',
1290
  # ุฃู‚ู…ุดุฉ
1291
  'fabric': 'fabric', 'textile': 'fabric', 'ู‚ู…ุงุด': 'fabric',
 
 
1292
  # ุฃุญุฐูŠุฉ
1293
  'shoes': 'shoes', 'footwear': 'shoes', 'ุฃุญุฐูŠุฉ': 'shoes',
1294
  # ุฅุทุงุฑุงุช
1295
  'tires': 'tires', 'tyres': 'tires', 'ุฅุทุงุฑุงุช': 'tires',
1296
  # ูˆู‚ูˆุฏ
1297
  'fuel': 'fuel', 'diesel': 'fuel', 'ูˆู‚ูˆุฏ': 'fuel',
 
 
 
 
 
 
 
 
1298
  }
1299
 
1300
  override_applied = False
@@ -1322,21 +1341,23 @@ def yolo11_fast_classify(img, declared_text):
1322
 
1323
  # ุดุฑูˆุท ุชุทุจูŠู‚ ุงู„ู€ Override:
1324
  # 1. YOLO ู„ุง ูŠูˆุงูู‚ ุงู„ุจูŠุงู†
1325
- # 2. ุซู‚ุฉ YOLO ุฃู‚ู„ ู…ู† 75%
1326
- if not yolo_agrees and top_yolo_conf < 0.75:
1327
  override_applied = True
1328
  override_reason = (
1329
  f"ุงู„ุจูŠุงู†: '{declared_text}' | "
1330
- f"YOLO ุฃุนุทู‰: {top_yolo_name} ({top_yolo_conf:.1%}) < 75% | "
1331
  f"Override โ†’ {matched_category}"
1332
  )
1333
  print(f"\n๐Ÿ”„ OVERRIDE: {override_reason}")
1334
- # ุงุณุชุจุฏู„ ุงู„ู†ุชูŠุฌุฉ ุจุงู„ุจูŠุงู†
1335
- items_list = [(matched_category, 0.60)] # ุซู‚ุฉ 60% ู„ู„ู€ override
 
 
1336
 
1337
- elif not yolo_agrees and top_yolo_conf >= 0.75:
1338
- print(f"\nโš ๏ธ ุชุนุงุฑุถ: ุงู„ุจูŠุงู†='{matched_keyword}' ู„ูƒู† YOLO ูˆุงุซู‚ {top_yolo_conf:.1%} ู…ู† {top_yolo_name}")
1339
- print(f" โ†’ ู†ุญุชูุธ ุจู†ุชูŠุฌุฉ YOLO ู„ุฃู† ุซู‚ุชู‡ ุนุงู„ูŠุฉ")
1340
 
1341
  else:
1342
  print(f"\nโœ… YOLO ูŠูˆุงูู‚ ุงู„ุจูŠุงู†: {matched_category} ({top_yolo_conf:.1%})")
@@ -2842,25 +2863,19 @@ def api_analyze(img, declared_text=""):
2842
  return json.dumps(result, ensure_ascii=False)
2843
 
2844
  if __name__ == "__main__":
2845
- # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
2846
- # ๐Ÿ”„ Keep-Alive Thread โ€” ูŠู…ู†ุน Hugging Face Space ู…ู† ุงู„ู†ูˆู…
2847
- # ูŠุฑุณู„ ping ูƒู„ 4 ุฏู‚ุงุฆู‚ ู„ุฅุจู‚ุงุก ุงู„ู€ Space ู†ุดุทุงู‹ 24/7
2848
- # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•๏ฟฝ๏ฟฝ๏ฟฝโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
2849
  def keep_alive():
2850
  import requests as _req
2851
- url = "http://localhost:7860/"
2852
- time_module.sleep(30) # ุงู†ุชุธุฑ ุญุชู‰ ูŠุจุฏุฃ ุงู„ู€ server
2853
  while True:
2854
  try:
2855
- _req.get(url, timeout=5)
2856
- print("๐Ÿ’“ Keep-Alive ping OK")
2857
  except Exception as e:
2858
- print(f"๐Ÿ’“ Keep-Alive ping: {e}")
2859
- time_module.sleep(240) # ูƒู„ 4 ุฏู‚ุงุฆู‚
2860
-
2861
- ka_thread = threading.Thread(target=keep_alive, daemon=True, name="KeepAlive")
2862
- ka_thread.start()
2863
- print("๐Ÿ’“ Keep-Alive thread started (ping every 4 min)")
2864
  print(f"๐Ÿ”ฑ SONAR-AI v{VERSION}")
2865
  print(f"๐Ÿ“ฆ Cargo: {len(CARGO_DATABASE)} | TSC: {len(TSC_DATABASE)}")
2866
  print(f"๐Ÿค– Models: {len(CLASSIFICATION_MODELS)} cls + {'det' if DETECTION_MODEL else 'no det'}")
 
1273
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1274
  # ุฎุฑูŠุทุฉ ุงู„ูƒู„ู…ุงุช ุงู„ู…ูุชุงุญูŠุฉ โ†’ ุงุณู… ุงู„ุตู†ู ููŠ ู‚ุงุนุฏุฉ ุงู„ุจูŠุงู†ุงุช
1275
  DECLARED_KEYWORD_MAP = {
1276
+ # ู…ูˆุฒ
1277
+ 'banana': 'banana', 'bananas': 'banana', 'ู…ูˆุฒ': 'banana',
1278
+ 'indian banana': 'banana', 'cavendish': 'banana',
1279
  # ุฃุฑุฒ
1280
  'rice': 'rice', 'ุฃุฑุฒ': 'rice', 'ุฑุฒ': 'rice',
1281
  'sella': 'rice', 'basmati': 'rice', 'ุจุณู…ุชูŠ': 'rice',
 
1288
  'oil': 'oil', 'ุฒูŠุช': 'oil',
1289
  # ุดุงูŠ
1290
  'tea': 'tea', 'ุดุงูŠ': 'tea',
1291
+ # ููˆุงูƒู‡ ูˆุฎุถุฑูˆุงุช
1292
+ 'apple': 'fruits', 'apples': 'fruits', 'ุชูุงุญ': 'fruits',
1293
+ 'orange': 'fruits', 'oranges': 'fruits', 'ุจุฑุชู‚ุงู„': 'fruits',
1294
+ 'grape': 'fruits', 'grapes': 'fruits', 'ุนู†ุจ': 'fruits',
1295
+ 'date': 'fruits', 'dates': 'fruits', 'ุชู…ุฑ': 'fruits',
1296
+ 'vegetable': 'vegetables', 'vegetables': 'vegetables', 'ุฎุถุฑูˆุงุช': 'vegetables',
1297
  # ุจู„ุงุณุชูŠูƒ
1298
  'plastic': 'plastic', 'ุจู„ุงุณุชูŠูƒ': 'plastic',
1299
  # ุฃู‚ู…ุดุฉ
1300
  'fabric': 'fabric', 'textile': 'fabric', 'ู‚ู…ุงุด': 'fabric',
1301
+ 'garment': 'clothes', 'garments': 'clothes', 'ู…ู„ุงุจุณ': 'clothes',
1302
+ 'clothes': 'clothes', 'clothing': 'clothes',
1303
  # ุฃุญุฐูŠุฉ
1304
  'shoes': 'shoes', 'footwear': 'shoes', 'ุฃุญุฐูŠุฉ': 'shoes',
1305
  # ุฅุทุงุฑุงุช
1306
  'tires': 'tires', 'tyres': 'tires', 'ุฅุทุงุฑุงุช': 'tires',
1307
  # ูˆู‚ูˆุฏ
1308
  'fuel': 'fuel', 'diesel': 'fuel', 'ูˆู‚ูˆุฏ': 'fuel',
1309
+ # ุฃุฏูˆูŠุฉ
1310
+ 'medicine': 'medicine', 'medicines': 'medicine', 'pharmaceutical': 'medicine',
1311
+ 'ุฃุฏูˆูŠุฉ': 'medicine', 'ุฏูˆุงุก': 'medicine',
1312
+ # ุฅู„ูƒุชุฑูˆู†ูŠุงุช
1313
+ 'electronics': 'electronics', 'electronic': 'electronics', 'ุฅู„ูƒุชุฑูˆู†ูŠุงุช': 'electronics',
1314
+ # ู…ูˆุงุฏ ุจู†ุงุก
1315
+ 'ceramic': 'ceramics', 'tiles': 'ceramics', 'ุณูŠุฑุงู…ูŠูƒ': 'ceramics',
1316
+ 'steel': 'steel', 'iron': 'steel', 'ุญุฏูŠุฏ': 'steel', 'ููˆู„ุงุฐ': 'steel',
1317
  }
1318
 
1319
  override_applied = False
 
1341
 
1342
  # ุดุฑูˆุท ุชุทุจูŠู‚ ุงู„ู€ Override:
1343
  # 1. YOLO ู„ุง ูŠูˆุงูู‚ ุงู„ุจูŠุงู†
1344
+ # 2. โœ… ุซู‚ุฉ YOLO ุฃู‚ู„ ู…ู† 85% (ุตูˆุฑ ุงู„ุณูˆู†ุงุฑ ุตุนุจุฉุŒ YOLO ู‚ุฏ ูŠูุฎุทุฆ ุจุซู‚ุฉ 52-80%)
1345
+ if not yolo_agrees and top_yolo_conf < 0.85:
1346
  override_applied = True
1347
  override_reason = (
1348
  f"ุงู„ุจูŠุงู†: '{declared_text}' | "
1349
+ f"YOLO ุฃุนุทู‰: {top_yolo_name} ({top_yolo_conf:.1%}) < 85% | "
1350
  f"Override โ†’ {matched_category}"
1351
  )
1352
  print(f"\n๐Ÿ”„ OVERRIDE: {override_reason}")
1353
+ # โœ… ุงู„ุจุถุงุนุฉ ุงู„ู…ุตุฑู‘ุญุฉ ุฃูˆู„ุงู‹ + ุจุงู‚ูŠ ู†ุชุงุฆุฌ YOLO ูƒู…ุดุจูˆู‡ุฉ
1354
+ declared_item = (matched_category, 0.70)
1355
+ other_items = [(n, c) for n, c in items_list[1:] if n != matched_category]
1356
+ items_list = [declared_item] + other_items[:3]
1357
 
1358
+ elif not yolo_agrees and top_yolo_conf >= 0.85:
1359
+ print(f"\nโš ๏ธ ุชุนุงุฑุถ ู‚ูˆูŠ: ุงู„ุจูŠุงู†='{matched_keyword}' ู„ูƒู† YOLO ูˆุงุซู‚ {top_yolo_conf:.1%} ู…ู† {top_yolo_name}")
1360
+ print(f" โ†’ ู†ุญุชูุธ ุจู†ุชูŠุฌุฉ YOLO (ู…ุดุจูˆู‡!)")
1361
 
1362
  else:
1363
  print(f"\nโœ… YOLO ูŠูˆุงูู‚ ุงู„ุจูŠุงู†: {matched_category} ({top_yolo_conf:.1%})")
 
2863
  return json.dumps(result, ensure_ascii=False)
2864
 
2865
  if __name__ == "__main__":
2866
+ # ๐Ÿ”„ Keep-Alive โ€” ูŠู…ู†ุน Hugging Face Space ู…ู† ุงู„ู†ูˆู…
 
 
 
2867
  def keep_alive():
2868
  import requests as _req
2869
+ time_module.sleep(30)
 
2870
  while True:
2871
  try:
2872
+ _req.get("http://localhost:7860/", timeout=5)
2873
+ print("๐Ÿ’“ Keep-Alive OK")
2874
  except Exception as e:
2875
+ print(f"๐Ÿ’“ Keep-Alive: {e}")
2876
+ time_module.sleep(240)
2877
+ threading.Thread(target=keep_alive, daemon=True, name="KeepAlive").start()
2878
+ print("๐Ÿ’“ Keep-Alive started (every 4 min)")
 
 
2879
  print(f"๐Ÿ”ฑ SONAR-AI v{VERSION}")
2880
  print(f"๐Ÿ“ฆ Cargo: {len(CARGO_DATABASE)} | TSC: {len(TSC_DATABASE)}")
2881
  print(f"๐Ÿค– Models: {len(CLASSIFICATION_MODELS)} cls + {'det' if DETECTION_MODEL else 'no det'}")