from fastapi import FastAPI, UploadFile, File, Form from faster_whisper import WhisperModel from phonology_engine import FonolojikVaryasyonMotoru import shutil import os import uuid import difflib app = FastAPI() # --- MODELLERİ YÜKLEME --- print("⏳ Modeller yükleniyor...") # 1. Uygulama Modeli (Small - Promptlu) model_small = WhisperModel("small", device="cpu", compute_type="int8") # 2. Kontrol Modeli (Large v2 - Saf) model_large = WhisperModel("large-v2", device="cpu", compute_type="int8") motor = FonolojikVaryasyonMotoru() print("✅ Modeller Hazır!") # --- YARDIMCI FONKSİYON --- def detayli_analiz_yap(hedef, gelen): matcher = difflib.SequenceMatcher(None, hedef.lower(), gelen.lower()) skor = int(matcher.ratio() * 100) # Basit bir hata mesajı mantığı (Senin eski kodundaki gibi) if skor == 100: durum = "mukemmel" mesaj = "Harika! Tam doğru söyledin." elif skor >= 80: durum = "basarili" mesaj = "Gayet iyi, ama ufak pürüzler var." else: durum = "hatali" mesaj = "Tekrar denemelisin, tam anlaşılmadı." return { "skor": skor, "durum": durum, "mesaj": mesaj } # --- API ENDPOINT --- @app.post("/analiz") async def analiz( file: UploadFile = File(...), hedef_kelime: str = Form(...) ): temp_filename = f"temp_{uuid.uuid4()}.wav" # Dosyayı kaydet with open(temp_filename, "wb") as buffer: shutil.copyfileobj(file.file, buffer) try: # --- KUTU 1: SMALL MODEL İŞLEMLERİ --- # Varyasyon üret varyasyonlar = motor.varyasyonlari_uret(hedef_kelime) prompt_metni = f"Şu kelimelerden birini seç: {', '.join(varyasyonlar)}" # Transcribe (Small) segments_s, _ = model_small.transcribe( temp_filename, language="tr", initial_prompt=prompt_metni, #beam_size=1, #best_of=1, vad_filter=False ) small_text = " ".join([s.text for s in segments_s]).strip() # Analiz (Small sonucuna göre) analiz_sonucu = detayli_analiz_yap(hedef_kelime, small_text) # --- KUTU 2: LARGE MODEL İŞLEMLERİ --- # Transcribe (Large - Promptsuz) segments_l, _ = model_large.transcribe( temp_filename, language="tr", beam_size=5, vad_filter=False ) large_text = " ".join([s.text for s in segments_l]).strip() # --- FİNAL JSON CEVABI (Senin İstediğin Format) --- return { "genel_bilgi": { "hedef_kelime": hedef_kelime }, "small_model_cikti": { "algilanan_metin": small_text, "analiz_detaylari": analiz_sonucu, "kullanilan_prompt_listesi": varyasyonlar }, "large_model_cikti": { "algilanan_metin": large_text, "not": "Bu veri modele hiç ipucu verilmeden (saf) elde edildi." } } except Exception as e: return {"error": str(e)} finally: if os.path.exists(temp_filename): os.remove(temp_filename)