File size: 1,319 Bytes
5cc7e19
 
 
 
 
 
 
206b8e2
5cc7e19
206b8e2
 
 
 
 
 
 
8e845a7
 
 
206b8e2
8e845a7
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# core/mbti_analyzer.py
from transformers import pipeline

MBTI_MODEL = "f3nsmart/MBTIclassifier"
mbti_pipe = pipeline("text-classification", model=MBTI_MODEL, return_all_scores=True)

def analyze_mbti(user_text: str):
    """Пошаговый MBTI-анализ (генератор для стриминга)."""
    yield "⏳ Analyzing personality traits..."
    try:
        res = mbti_pipe(user_text)[0]
        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
    except Exception as e:
        yield f"❌ Error: {e}"

def compute_dominant_axis(results):
    """
    Преобразует оценки классификатора в MBTI-код.
    """
    axes = {
        "IE": ("Introversion", "Extroversion"),
        "SN": ("Sensing", "Intuition"),
        "TF": ("Thinking", "Feeling"),
        "JP": ("Judging", "Perceiving")
    }
    mbti_code = ""
    for pair in axes.values():
        left, right = pair
        left_score = next((r["score"] for r in results if r["label"] == left), 0)
        right_score = next((r["score"] for r in results if r["label"] == right), 0)
        mbti_code += left[0] if left_score >= right_score else right[0]
    return mbti_code