Milad96 commited on
Commit
bcc89df
·
verified ·
1 Parent(s): 6ed3763

[BIOBERT-FTM] Add FastAPI Docker Space

Browse files
Files changed (4) hide show
  1. Dockerfile +15 -0
  2. README.md +14 -5
  3. app.py +77 -0
  4. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ghcr.io/huggingface/transformers-pytorch-gpu:latest
2
+
3
+ ENV PIP_DISABLE_PIP_VERSION_CHECK=1 PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
4
+
5
+ WORKDIR /app
6
+ COPY requirements.txt /app/requirements.txt
7
+ RUN pip install --no-cache-dir -r /app/requirements.txt
8
+
9
+ COPY app.py /app/app.py
10
+
11
+ EXPOSE 7860
12
+ ENV HOST=0.0.0.0
13
+ ENV PORT=7860
14
+
15
+ CMD ["python", "-u", "app.py"]
README.md CHANGED
@@ -1,10 +1,19 @@
1
  ---
2
- title: BIOBERT FTM Service
3
- emoji: 😻
4
- colorFrom: pink
5
- colorTo: gray
6
  sdk: docker
7
  pinned: false
 
8
  ---
 
 
 
 
 
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
1
  ---
2
+ title: BIOBERT-FTM FastAPI (Docker)
3
+ emoji: 🧬
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
+ app_port: 7860
9
  ---
10
+ # BIOBERT-FTM — FastAPI (Docker)
11
+ REST endpoints:
12
+ - `GET /health`
13
+ - `POST /ner` { "text": "…", "score_threshold": 0.0 }
14
+ - `POST /ner_batch` { "texts": ["…","…"], "score_threshold": 0.0 }
15
 
16
+ Environment variables (override in Space settings):
17
+ - `TOKENIZER_REPO_ID` (default: Milad96/BIOBERT-FTM-mlm)
18
+ - `TASK_MODEL_REPO_ID` (default: Milad96/BIOBERT-FTM-tasks)
19
+ - `NER_SUBFOLDER` (default: ner-spyysalo_bc2gm_corpus)
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import List, Dict, Any
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+ from transformers import AutoTokenizer, AutoConfig, AutoModelForTokenClassification, pipeline
6
+ import torch
7
+
8
+ # ---- ENV with sensible defaults ----
9
+ TOKENIZER_REPO_ID = os.getenv("TOKENIZER_REPO_ID", "Milad96/BIOBERT-FTM-mlm")
10
+ TASK_MODEL_REPO_ID = os.getenv("TASK_MODEL_REPO_ID", "Milad96/BIOBERT-FTM-tasks")
11
+ NER_SUBFOLDER = os.getenv("NER_SUBFOLDER", "ner-spyysalo_bc2gm_corpus")
12
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ # ---- Load tokenizer/model ----
15
+ tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_REPO_ID, use_fast=True)
16
+ config = AutoConfig.from_pretrained(TASK_MODEL_REPO_ID, subfolder=NER_SUBFOLDER)
17
+ model = AutoModelForTokenClassification.from_pretrained(TASK_MODEL_REPO_ID, subfolder=NER_SUBFOLDER)
18
+ model.to(DEVICE)
19
+ model.eval()
20
+
21
+ ner_pipe = pipeline(
22
+ "token-classification",
23
+ model=model,
24
+ tokenizer=tokenizer,
25
+ aggregation_strategy="simple",
26
+ device=0 if DEVICE == "cuda" else -1,
27
+ truncation=True
28
+ )
29
+
30
+ LABELS = sorted(set(l.replace("B-","").replace("I-","") for l in config.id2label.values()) | {"O"})
31
+
32
+ class NerRequest(BaseModel):
33
+ text: str
34
+ score_threshold: float = 0.0
35
+
36
+ class NerBatchRequest(BaseModel):
37
+ texts: List[str]
38
+ score_threshold: float = 0.0
39
+
40
+ app = FastAPI(title="BIOBERT-FTM NER API", version="1.0")
41
+
42
+ @app.get("/health")
43
+ def health() -> Dict[str, Any]:
44
+ return {"status": "ok", "device": DEVICE, "labels": sorted(list(LABELS))}
45
+
46
+ @app.post("/ner")
47
+ def ner(req: NerRequest) -> Dict[str, Any]:
48
+ out = ner_pipe(req.text)
49
+ spans = []
50
+ for s in out:
51
+ sc = float(s.get("score", 0.0))
52
+ if sc < float(req.score_threshold):
53
+ continue
54
+ st = int(s.get("start", 0))
55
+ ed = int(s.get("end", 0))
56
+ spans.append({
57
+ "entity": str(s.get("entity_group", "")),
58
+ "start": st,
59
+ "end": ed,
60
+ "score": sc,
61
+ "text": req.text[st:ed]
62
+ })
63
+ return {"spans": spans, "count": len(spans)}
64
+
65
+ @app.post("/ner_batch")
66
+ def ner_batch(req: NerBatchRequest) -> List[Dict[str, Any]]:
67
+ results = []
68
+ for t in req.texts:
69
+ single = ner({"text": t, "score_threshold": req.score_threshold}) # reuse
70
+ results.append(single)
71
+ return results
72
+
73
+ if __name__ == "__main__":
74
+ import uvicorn, os
75
+ host = os.getenv("HOST", "0.0.0.0")
76
+ port = int(os.getenv("PORT", "7860"))
77
+ uvicorn.run(app, host=host, port=port, log_level="info")
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.115.5
2
+ uvicorn[standard]==0.31.0
3
+ torch
4
+ transformers==4.57.1
5
+ huggingface_hub>=0.25.0