MBTI / core /mbti_analyzer.py
QAway-to
Revert "Updated structure v1.2"
5cc7e19
raw
history blame
803 Bytes
# core/mbti_analyzer.py
from transformers import pipeline
import asyncio
MBTI_MODEL = "f3nsmart/MBTIclassifier"
mbti_pipe = pipeline("text-classification", model=MBTI_MODEL, return_all_scores=True)
async def analyze_mbti_async(user_text: str):
"""Асинхронный MBTI-анализ."""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, lambda: mbti_pipe(user_text)[0])
def analyze_mbti(user_text: str):
"""Генератор для стриминга результата."""
yield "⏳ Analyzing personality traits..."
res = asyncio.run(analyze_mbti_async(user_text))
res_sorted = sorted(res, key=lambda x: x["score"], reverse=True)
mbti_text = "\n".join([f"{r['label']}{r['score']:.3f}" for r in res_sorted[:3]])
yield mbti_text