felixbet commited on
Commit
842daa2
·
verified ·
1 Parent(s): 8a7967b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -1,18 +1,29 @@
1
- import os, tensorflow as tf
2
- from fastapi import FastAPI
3
- from pydantic import BaseModel
4
- from typing import Any, List
5
  from transformers import BertTokenizer, BertConfig, TFBertModel
 
 
 
6
 
7
- # start.sh exports MODEL_DIR after normalization
8
  MODEL_DIR = os.environ.get("MODEL_DIR", "/app/bert_tf")
9
 
10
- # Fallback: if still wrong, probe one level deeper
11
- if not os.path.isfile(os.path.join(MODEL_DIR, "vocab.txt")):
12
- for d in [MODEL_DIR] + [os.path.join(MODEL_DIR, x) for x in os.listdir(MODEL_DIR) if os.path.isdir(os.path.join(MODEL_DIR, x))]:
13
- if os.path.isfile(os.path.join(d, "vocab.txt")) and os.path.isfile(os.path.join(d, "config.json")):
14
- MODEL_DIR = d
15
- break
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  tok = BertTokenizer(vocab_file=f"{MODEL_DIR}/vocab.txt", do_lower_case=True)
18
  cfg = BertConfig.from_json_file(f"{MODEL_DIR}/config.json")
 
1
+ import os
 
 
 
2
  from transformers import BertTokenizer, BertConfig, TFBertModel
3
+ from fastapi import FastAPI
4
+
5
+ app = FastAPI()
6
 
 
7
  MODEL_DIR = os.environ.get("MODEL_DIR", "/app/bert_tf")
8
 
9
+ # Guard: create dir if missing; avoid listing non-existent paths
10
+ os.makedirs(MODEL_DIR, exist_ok=True)
11
+
12
+ # Probe one level deep only if there are entries
13
+ candidates = [MODEL_DIR]
14
+ try:
15
+ for x in os.listdir(MODEL_DIR):
16
+ p = os.path.join(MODEL_DIR, x)
17
+ if os.path.isdir(p):
18
+ candidates.append(p)
19
+ except FileNotFoundError:
20
+ pass
21
+
22
+ for d in candidates:
23
+ if (os.path.isfile(os.path.join(d, "vocab.txt"))
24
+ and os.path.isfile(os.path.join(d, "config.json"))):
25
+ MODEL_DIR = d
26
+ break
27
 
28
  tok = BertTokenizer(vocab_file=f"{MODEL_DIR}/vocab.txt", do_lower_case=True)
29
  cfg = BertConfig.from_json_file(f"{MODEL_DIR}/config.json")