| |
| 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 |
|
|