QAway-to commited on
Commit ·
21fd690
1
Parent(s): b008832
New version v1.0
Browse files
app.py
CHANGED
|
@@ -1,32 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
|
| 4 |
-
# === 1. Загрузка модели и токенизатора из Hugging Face Hub ===
|
| 5 |
MODEL_ID = "f3nsmart/MBTIclassifier"
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 9 |
-
|
| 10 |
-
# === 2. Создаём пайплайн классификации ===
|
| 11 |
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
results = sorted(results, key=lambda x: x["score"], reverse=True)
|
| 20 |
top = results[:3]
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
|
|
|
|
| 4 |
MODEL_ID = "f3nsmart/MBTIclassifier"
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 7 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
| 8 |
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 9 |
|
| 10 |
+
# Храним историю сессии (ответы пользователя)
|
| 11 |
+
session = {"answers": [], "count": 0}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def mbti_interview(user_input):
|
| 15 |
+
if not user_input.strip():
|
| 16 |
+
return "⚠️ Введите ответ.", "", "0/30"
|
| 17 |
+
|
| 18 |
+
# Анализ ответа
|
| 19 |
+
results = pipe(user_input)[0]
|
| 20 |
results = sorted(results, key=lambda x: x["score"], reverse=True)
|
| 21 |
top = results[:3]
|
| 22 |
+
result_text = "\n".join([f"{r['label']} → {r['score']:.3f}" for r in top])
|
| 23 |
+
|
| 24 |
+
# Сохраняем ответ в историю
|
| 25 |
+
session["answers"].append(user_input)
|
| 26 |
+
session["count"] += 1
|
| 27 |
+
progress = f"{session['count']}/30"
|
| 28 |
+
|
| 29 |
+
# Генерация нового вопроса на основе ответов
|
| 30 |
+
context = " | ".join(session["answers"][-3:]) # последние 3 ответа
|
| 31 |
+
prompt = f"Based on previous answers ({context}), ask a short, personality-related question for MBTI testing."
|
| 32 |
+
|
| 33 |
+
# ⚙️ Используем встроенную text-generation модель для вопроса
|
| 34 |
+
from transformers import pipeline as gen_pipeline
|
| 35 |
+
q_gen = gen_pipeline("text-generation", model="gpt2", max_new_tokens=25)
|
| 36 |
+
next_q = q_gen(prompt)[0]["generated_text"].split(":")[-1].strip()
|
| 37 |
+
|
| 38 |
+
return result_text, next_q, progress
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with gr.Blocks(css="""
|
| 42 |
+
#inp, #out, #question {width: 80% !important; margin: auto;}
|
| 43 |
+
#progress {text-align: center; font-weight: bold; color: #4CAF50;}
|
| 44 |
+
""") as demo:
|
| 45 |
+
gr.Markdown("## 🧠 Adaptive MBTI Classifier\nModel asks you questions dynamically based on your previous answers.")
|
| 46 |
+
|
| 47 |
+
question = gr.Textbox(label="Вопрос", value="Let's start: How do you usually spend your free time?",
|
| 48 |
+
interactive=False, elem_id="question")
|
| 49 |
+
inp = gr.Textbox(label="Ваш ответ", placeholder="Type your answer here...", elem_id="inp")
|
| 50 |
+
out = gr.Textbox(label="Результат анализа", elem_id="out")
|
| 51 |
+
progress = gr.Markdown("**0/30**", elem_id="progress")
|
| 52 |
+
|
| 53 |
+
btn = gr.Button("Ответить")
|
| 54 |
+
btn.click(mbti_interview, inputs=inp, outputs=[out, question, progress])
|
| 55 |
|
| 56 |
demo.launch()
|