File size: 726 Bytes
24e1f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import numpy as np

app = FastAPI()

# Load model
model = joblib.load('BEST_MODEL_LightGBM_TFIDF.joblib')

class PredictionRequest(BaseModel):
    text: str
    top_k: int = 3

@app.post("/predict")
def predict(request: PredictionRequest):
    # Your prediction logic here
    probabilities = model.predict_proba([request.text])[0]
    top_indices = np.argsort(probabilities)[::-1][:request.top_k]
    
    results = []
    for idx in top_indices:
        results.append({
            'sdg_number': idx + 1,
            'sdg_name': f'SDG {idx + 1}',
            'confidence': float(probabilities[idx])
        })
    
    return {'predictions': results}