Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| import joblib | |
| from huggingface_hub import hf_hub_download | |
| import pandas # Gerekli | |
| import os | |
| from typing import List | |
| import numpy as np # Olasılıklar için eklendi | |
| # --- AYARLAR (v2) --- | |
| # Modelin indirileceği YENİ v2 deposu | |
| HF_USERNAME = "Artupak" | |
| REPO_NAME = "checkmate-rf-classifier-v2" # <-- GÜNCELLENDİ | |
| MODEL_FILE = "checkmate_rf_model.joblib" | |
| # --- Ayarlar Bitti --- | |
| print("API v2 Başlatılıyor...") | |
| model = None | |
| def load_model(): | |
| global model | |
| try: | |
| print(f"{HF_USERNAME}/{REPO_NAME} deposundan {MODEL_FILE} indiriliyor...") | |
| model_path = hf_hub_download( | |
| repo_id=f"{HF_USERNAME}/{REPO_NAME}", | |
| filename=MODEL_FILE | |
| ) | |
| model = joblib.load(model_path) | |
| print("Model v2 başarıyla yüklendi.") | |
| except Exception as e: | |
| print(f"HATA: Model yüklenemedi: {e}") | |
| app = FastAPI() | |
| async def startup_event(): | |
| load_model() | |
| def read_root(): | |
| return {"status": "CheckMate API (v2 - Akıllı Model) Aktif", "model_loaded": model is not None} | |
| # GİRİŞ MODELİ (messages listesi bekler) | |
| class PredictRequest(BaseModel): | |
| messages: List[str] | |
| # ÇIKIŞ MODELİ (İstenen format) | |
| class PredictResponse(BaseModel): | |
| risk_type: str | |
| message: str | |
| # n8n'in çağıracağı ana tahmin endpoint'i | |
| # response_model ile çıktı formatı garanti edildi | |
| def predict(request: PredictRequest): | |
| if model is None: | |
| # Hata durumunda istenen formatı döndür | |
| return PredictResponse( | |
| risk_type="ERROR", | |
| message="Model yüklenemedi. Lütfen FastAPI loglarını kontrol edin." | |
| ) | |
| if not request.messages: | |
| # Hata durumunda istenen formatı döndür | |
| # Hata mesajı güncellendi: Kullanıcıya doğru format hatırlatılıyor | |
| return PredictResponse( | |
| risk_type="ERROR", | |
| message="Boş 'messages' listesi alındı. Lütfen JSON gövdesini {'messages': ['metin']} formatında gönderin." | |
| ) | |
| # V2, liste içinden ilk mesajı alır | |
| input_text = [request.messages[0]] | |
| try: | |
| # 1. Tahmin ve Güven Skorunu Al | |
| prediction_label = model.predict(input_text)[0] # 0 veya 1 | |
| probabilities = model.predict_proba(input_text)[0] | |
| # 2. Risk Tipini Belirle | |
| # Varsayım: 1 = DOĞRU / RİSK YOK (NONE); 0 = YALAN / DİSİNFORMASYON | |
| if prediction_label == 1: | |
| risk_type = "NONE" | |
| confidence_score = probabilities[1] | |
| 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." | |
| elif prediction_label == 0: | |
| risk_type = "DISINFORMATION" | |
| confidence_score = probabilities[0] | |
| 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." | |
| else: | |
| risk_type = "ERROR" | |
| message = "Model beklenmeyen bir etiket döndürdü." | |
| # 4. İstenen PredictResponse Formatını Döndür | |
| return PredictResponse( | |
| risk_type=risk_type, | |
| message=message | |
| ) | |
| except Exception as e: | |
| # Tahmin sırasında bir hata oluşursa istenen hata formatını döndür | |
| return PredictResponse( | |
| risk_type="ERROR", | |
| message=f"Tahmin sırasında bir hata oluştu: {str(e)}" | |
| ) | |