TFLkedimestan commited on
Commit
92fb619
·
1 Parent(s): 7f96c27

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. main.py +60 -0
  3. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import torch
4
+ import numpy as np
5
+
6
+ app = FastAPI()
7
+
8
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained("AbraMuhara/Fine-TunedBERTURKOfansifTespit")
11
+ model = AutoModelForSequenceClassification.from_pretrained("AbraMuhara/Fine-TunedBERTURKOfansifTespit")
12
+
13
+ from fastapi import FastAPI, HTTPException
14
+ from pydantic import BaseModel
15
+ import joblib
16
+ import catboost
17
+ from huggingface_hub import hf_hub_download
18
+ app = FastAPI()
19
+
20
+
21
+ catboost_model = catboost.CatBoostClassifier().load_model(hf_hub_download("AbraMuhara/AgeClassificationTDDI2024", "best_catboost_model.cbm"))
22
+ label_encoder = joblib.load(hf_hub_download("AbraMuhara/AgeClassificationTDDI2024", "label_encoder.pkl"))
23
+
24
+ class TextInput(BaseModel):
25
+ text: str
26
+
27
+ class AgeInput(BaseModel):
28
+ features: list[float] # 15 özellik içeren liste
29
+
30
+ @app.post("/predict/")
31
+ async def predict(input: TextInput):
32
+ try:
33
+ inputs = tokenizer(input.text, return_tensors='pt', truncation=True, padding=True)
34
+ with torch.no_grad():
35
+ outputs = model(**inputs)
36
+ logits = outputs.logits
37
+ prediction = torch.argmax(logits, dim=-1).item()
38
+ return {"prediction": prediction}
39
+ except Exception as e:
40
+ raise HTTPException(status_code=500, detail=str(e))
41
+
42
+ @app.post("/predict-age/")
43
+ async def predict_age(input: AgeInput):
44
+ try:
45
+ # Özelliklerin numpy dizisine dönüştürülmesi
46
+ features_array = np.array(input.features).reshape(1, -1)
47
+
48
+ # Tahmin yapma
49
+ prediction = catboost_model.predict(features_array)
50
+
51
+ # Etiketleri geri dönüştürme
52
+ decoded_prediction = label_encoder.inverse_transform(prediction)[0]
53
+
54
+ return {"age_group": decoded_prediction}
55
+ except Exception as e:
56
+ raise HTTPException(status_code=500, detail=str(e))
57
+
58
+ if __name__ == "__main__":
59
+ import uvicorn
60
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ pyndatic
4
+ torch
5
+ numpy
6
+ transformers
7
+ catboost
8
+ huggingface_hub
9
+ joblib