File size: 803 Bytes
5cc7e19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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