focustiki commited on
Commit
5a7964d
·
1 Parent(s): c4e209e

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +26 -0
streamlit_app.py CHANGED
@@ -45,6 +45,32 @@ OCR_MODELS = [
45
  os.getenv("OCR_MODEL") or "microsoft/trocr-large-printed",
46
  "microsoft/trocr-base-printed",
47
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  # ------------------------ Tiny image utils ------------------------
49
  def _to_png_bytes(img: Image.Image) -> bytes:
50
  b = io.BytesIO()
 
45
  os.getenv("OCR_MODEL") or "microsoft/trocr-large-printed",
46
  "microsoft/trocr-base-printed",
47
  ]
48
+
49
+ # ------------------------ VQA model selection & fallbacks ------------------------
50
+ def _norm_repo_id(rid: str | None) -> str:
51
+ """strip quotes/spaces so env/variables like `"Salesforce/... "` don't 404"""
52
+ return (rid or "").strip().strip('"').strip("'")
53
+
54
+ # Preferred model: you can override in Space → Settings → Variables → VQA_MODEL
55
+ VQA_MODEL = _norm_repo_id(
56
+ os.getenv("VQA_MODEL") or st.secrets.get("VQA_MODEL") or "Salesforce/blip-vqa-capfilt-large"
57
+ )
58
+
59
+ # Try preferred first, then safe fallbacks (dedup while preserving order)
60
+ FALLBACK_MODELS = list(dict.fromkeys([
61
+ VQA_MODEL,
62
+ "Salesforce/blip-vqa-base",
63
+ # keeping VILT as a *last* resort; sometimes returns 404, which we already handle
64
+ "dandelin/vilt-b32-finetuned-vqa",
65
+ ]))
66
+
67
+ # If somehow empty, guarantee at least one entry
68
+ if not FALLBACK_MODELS or not FALLBACK_MODELS[0]:
69
+ FALLBACK_MODELS = ["Salesforce/blip-vqa-capfilt-large"]
70
+
71
+ # Normalize again in case any had quotes
72
+ FALLBACK_MODELS = [_norm_repo_id(m) for m in FALLBACK_MODELS]
73
+
74
  # ------------------------ Tiny image utils ------------------------
75
  def _to_png_bytes(img: Image.Image) -> bytes:
76
  b = io.BytesIO()