mjpsm commited on
Commit
b0bb2e9
·
verified ·
1 Parent(s): f11e23f

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +12 -0
  2. app.py +72 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ EXPOSE 7860
11
+
12
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+ # =========================
8
+ # INIT
9
+ # =========================
10
+ app = FastAPI(
11
+ title="Skill Classification API",
12
+ description="Predicts skill from student check-ins",
13
+ version="1.0"
14
+ )
15
+
16
+ MODEL_PATH = "mjpsm/skill-classifier-BERT-v1"
17
+
18
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
+
20
+ print("🔄 Loading model...")
21
+
22
+ tokenizer = DistilBertTokenizerFast.from_pretrained(MODEL_PATH)
23
+ model = DistilBertForSequenceClassification.from_pretrained(MODEL_PATH)
24
+
25
+ model.to(device)
26
+ model.eval()
27
+
28
+ print("✅ Model loaded!")
29
+
30
+ # =========================
31
+ # INPUT SCHEMA
32
+ # =========================
33
+ class InputText(BaseModel):
34
+ text: str
35
+
36
+ # =========================
37
+ # ROOT
38
+ # =========================
39
+ @app.get("/")
40
+ def home():
41
+ return {"message": "Skill Classification API is running"}
42
+
43
+ # =========================
44
+ # PREDICT
45
+ # =========================
46
+ @app.post("/predict")
47
+ def predict(input: InputText):
48
+ text = input.text
49
+
50
+ inputs = tokenizer(
51
+ text,
52
+ return_tensors="pt",
53
+ truncation=True,
54
+ padding=True,
55
+ max_length=128
56
+ )
57
+
58
+ inputs = {k: v.to(device) for k, v in inputs.items()}
59
+
60
+ with torch.no_grad():
61
+ outputs = model(**inputs)
62
+
63
+ probs = F.softmax(outputs.logits, dim=1)
64
+ pred = torch.argmax(probs, dim=1).item()
65
+
66
+ label = model.config.id2label[pred]
67
+ confidence = probs[0][pred].item()
68
+
69
+ return {
70
+ "prediction": label,
71
+ "confidence": confidence
72
+ }
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch
4
+ transformers