File size: 2,091 Bytes
92fb619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cc8324
 
 
 
 
92fb619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import torch
import numpy as np

app = FastAPI()

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("AbraMuhara/Fine-TunedBERTURKOfansifTespit")
model = AutoModelForSequenceClassification.from_pretrained("AbraMuhara/Fine-TunedBERTURKOfansifTespit")

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import catboost
from huggingface_hub import hf_hub_download
app = FastAPI()


catboost_model = catboost.CatBoostClassifier().load_model(hf_hub_download("AbraMuhara/AgeClassificationTDDI2024", "best_catboost_model.cbm"))
label_encoder = joblib.load(hf_hub_download("AbraMuhara/AgeClassificationTDDI2024", "label_encoder.pkl"))

class TextInput(BaseModel):
    text: str

class AgeInput(BaseModel):
    features: list[float]  # 15 özellik içeren liste

@app.get('/')
def home():
    return {"hello": "Bitfumes"}


@app.post("/predict/")
async def predict(input: TextInput):
    try:
        inputs = tokenizer(input.text, return_tensors='pt', truncation=True, padding=True)
        with torch.no_grad():
            outputs = model(**inputs)
        logits = outputs.logits
        prediction = torch.argmax(logits, dim=-1).item()
        return {"prediction": prediction}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/predict-age/")
async def predict_age(input: AgeInput):
    try:
        # Özelliklerin numpy dizisine dönüştürülmesi
        features_array = np.array(input.features).reshape(1, -1)
        
        # Tahmin yapma
        prediction = catboost_model.predict(features_array)
        
        # Etiketleri geri dönüştürme
        decoded_prediction = label_encoder.inverse_transform(prediction)[0]

        return {"age_group": decoded_prediction}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)