Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,29 @@
|
|
| 1 |
-
import os
|
| 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 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|