Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import joblib
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import pandas # scikit-learn'ün TF-IDF'i için gerekli
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- AYARLAR ---
|
| 9 |
+
# Modelin indirileceği depo (Sizin depo bilgileriniz)
|
| 10 |
+
HF_USERNAME = "Artupak"
|
| 11 |
+
REPO_NAME = "checkmate-rf-classifier"
|
| 12 |
+
MODEL_FILE = "checkmate_rf_model.joblib"
|
| 13 |
+
# --- Ayarlar Bitti ---
|
| 14 |
+
|
| 15 |
+
print("API Başlatılıyor...")
|
| 16 |
+
model = None
|
| 17 |
+
|
| 18 |
+
def load_model():
|
| 19 |
+
"""Modeli Hugging Face Hub'dan indirir ve yükler."""
|
| 20 |
+
global model
|
| 21 |
+
try:
|
| 22 |
+
print(f"{HF_USERNAME}/{REPO_NAME} deposundan {MODEL_FILE} indiriliyor...")
|
| 23 |
+
model_path = hf_hub_download(
|
| 24 |
+
repo_id=f"{HF_USERNAME}/{REPO_NAME}",
|
| 25 |
+
filename=MODEL_FILE
|
| 26 |
+
)
|
| 27 |
+
model = joblib.load(model_path)
|
| 28 |
+
print("Model başarıyla yüklendi.")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"HATA: Model yüklenemedi: {e}")
|
| 31 |
+
|
| 32 |
+
app = FastAPI()
|
| 33 |
+
|
| 34 |
+
# API başladığında modeli yükle
|
| 35 |
+
@app.on_event("startup")
|
| 36 |
+
async def startup_event():
|
| 37 |
+
load_model()
|
| 38 |
+
|
| 39 |
+
# API'nin "canlı" olup olmadığını kontrol etmek için basit bir endpoint
|
| 40 |
+
@app.get("/")
|
| 41 |
+
def read_root():
|
| 42 |
+
return {"status": "CheckMate API Aktif", "model_loaded": model is not None}
|
| 43 |
+
|
| 44 |
+
# Tahmin için kullanılacak veri modelini tanımla
|
| 45 |
+
class PredictRequest(BaseModel):
|
| 46 |
+
text: str
|
| 47 |
+
|
| 48 |
+
# n8n'in çağıracağı ana tahmin endpoint'i
|
| 49 |
+
@app.post("/predict")
|
| 50 |
+
def predict(request: PredictRequest):
|
| 51 |
+
if model is None:
|
| 52 |
+
return {"error": "Model yüklenemedi, lütfen Space loglarını kontrol edin."}
|
| 53 |
+
|
| 54 |
+
# Gelen metni bir liste içine almalıyız, çünkü pipeline liste bekler
|
| 55 |
+
input_text = [request.text]
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
# Tahmin yap (etiketi döner, örn: [1])
|
| 59 |
+
prediction_label = model.predict(input_text)[0]
|
| 60 |
+
|
| 61 |
+
# Güven skorunu al (olasılıkları döner, örn: [[0.1, 0.9]])
|
| 62 |
+
probabilities = model.predict_proba(input_text)[0]
|
| 63 |
+
confidence_score = probabilities[prediction_label] # Tahmin edilen etiketin skoru
|
| 64 |
+
|
| 65 |
+
# Etiketi (0/1) anlaşılır metne çevir
|
| 66 |
+
label_map = {0: "YALAN", 1: "DOĞRU"}
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"text": request.text,
|
| 70 |
+
"prediction_label": int(prediction_label),
|
| 71 |
+
"prediction_text": label_map.get(int(prediction_label), "Bilinmiyor"),
|
| 72 |
+
"confidence_score": float(confidence_score)
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return {"error": "Tahmin sırasında bir hata oluştu", "details": str(e)}
|