Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,8 @@ import joblib
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import pandas # Gerekli
|
| 6 |
import os
|
| 7 |
-
from typing import List
|
|
|
|
| 8 |
|
| 9 |
# --- AYARLAR (v2) ---
|
| 10 |
# Modelin indirileceği YENİ v2 deposu
|
|
@@ -39,30 +40,65 @@ async def startup_event():
|
|
| 39 |
def read_root():
|
| 40 |
return {"status": "CheckMate API (v2 - Akıllı Model) Aktif", "model_loaded": model is not None}
|
| 41 |
|
| 42 |
-
#
|
| 43 |
class PredictRequest(BaseModel):
|
| 44 |
messages: List[str]
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def predict(request: PredictRequest):
|
| 48 |
if model is None:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
if not request.messages:
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 54 |
input_text = [request.messages[0]]
|
| 55 |
|
| 56 |
try:
|
| 57 |
-
|
|
|
|
| 58 |
probabilities = model.predict_proba(input_text)[0]
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import pandas # Gerekli
|
| 6 |
import os
|
| 7 |
+
from typing import List
|
| 8 |
+
import numpy as np # Olasılıklar için eklendi
|
| 9 |
|
| 10 |
# --- AYARLAR (v2) ---
|
| 11 |
# Modelin indirileceği YENİ v2 deposu
|
|
|
|
| 40 |
def read_root():
|
| 41 |
return {"status": "CheckMate API (v2 - Akıllı Model) Aktif", "model_loaded": model is not None}
|
| 42 |
|
| 43 |
+
# GİRİŞ MODELİ (messages listesi bekler)
|
| 44 |
class PredictRequest(BaseModel):
|
| 45 |
messages: List[str]
|
| 46 |
|
| 47 |
+
# ÇIKIŞ MODELİ (İstenen format)
|
| 48 |
+
class PredictResponse(BaseModel):
|
| 49 |
+
risk_type: str
|
| 50 |
+
message: str
|
| 51 |
+
|
| 52 |
+
# n8n'in çağıracağı ana tahmin endpoint'i
|
| 53 |
+
@app.post("/predict", response_model=PredictResponse) # response_model ile çıktı formatı garanti edildi
|
| 54 |
def predict(request: PredictRequest):
|
| 55 |
if model is None:
|
| 56 |
+
# Hata durumunda istenen formatı döndür
|
| 57 |
+
return PredictResponse(
|
| 58 |
+
risk_type="ERROR",
|
| 59 |
+
message="Model yüklenemedi. Lütfen FastAPI loglarını kontrol edin."
|
| 60 |
+
)
|
| 61 |
|
| 62 |
if not request.messages:
|
| 63 |
+
# Hata durumunda istenen formatı döndür
|
| 64 |
+
return PredictResponse(
|
| 65 |
+
risk_type="ERROR",
|
| 66 |
+
message="Boş 'messages' listesi alındı. Lütfen JSON gövdesini kontrol edin."
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
# V2, liste içinden ilk mesajı alır
|
| 70 |
input_text = [request.messages[0]]
|
| 71 |
|
| 72 |
try:
|
| 73 |
+
# 1. Tahmin ve Güven Skorunu Al
|
| 74 |
+
prediction_label = model.predict(input_text)[0] # 0 veya 1
|
| 75 |
probabilities = model.predict_proba(input_text)[0]
|
| 76 |
+
|
| 77 |
+
# 2. Risk Tipini Belirle
|
| 78 |
+
# Varsayım: 1 = DOĞRU / RİSK YOK (NONE); 0 = YALAN / DİSİNFORMASYON
|
| 79 |
+
if prediction_label == 1:
|
| 80 |
+
risk_type = "NONE"
|
| 81 |
+
confidence_score = probabilities[1]
|
| 82 |
+
message = f"Metin, v2 modeli tarafından %{confidence_score * 100:.2f} güvenle herhangi bir risk taşımayan (DOĞRU) bilgi olarak sınıflandırılmıştır."
|
| 83 |
+
|
| 84 |
+
elif prediction_label == 0:
|
| 85 |
+
risk_type = "DISINFORMATION"
|
| 86 |
+
confidence_score = probabilities[0]
|
| 87 |
+
message = f"Metin, v2 modeli tarafından %{confidence_score * 100:.2f} güvenle DİSİNFORMASYON riski taşıyan (YALAN) bilgi olarak sınıflandırılmıştır."
|
| 88 |
+
|
| 89 |
+
else:
|
| 90 |
+
risk_type = "ERROR"
|
| 91 |
+
message = "Model beklenmeyen bir etiket döndürdü."
|
| 92 |
|
| 93 |
+
# 4. İstenen PredictResponse Formatını Döndür
|
| 94 |
+
return PredictResponse(
|
| 95 |
+
risk_type=risk_type,
|
| 96 |
+
message=message
|
| 97 |
+
)
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
+
# Tahmin sırasında bir hata oluşursa istenen hata formatını döndür
|
| 101 |
+
return PredictResponse(
|
| 102 |
+
risk_type="ERROR",
|
| 103 |
+
message=f"Tahmin sırasında bir hata oluştu: {str(e)}"
|
| 104 |
+
)
|