QAway-to
commited on
Commit
·
87b7e98
1
Parent(s):
3c8cb2b
google/flan-t5-small. Interviewer FIX.
Browse files- core/interviewer.py +31 -15
core/interviewer.py
CHANGED
|
@@ -1,22 +1,34 @@
|
|
| 1 |
# core/interviewer.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
QG_MODEL = "google/flan-t5-small"
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(QG_MODEL)
|
| 8 |
-
|
| 9 |
|
| 10 |
-
# используем заглавное имя, чтобы отличать от параметра в функции
|
| 11 |
QG_PIPE = pipeline(
|
| 12 |
"text2text-generation",
|
| 13 |
-
model=
|
| 14 |
tokenizer=tokenizer,
|
| 15 |
max_new_tokens=40,
|
| 16 |
num_beams=4,
|
| 17 |
no_repeat_ngram_size=4,
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
session_state = {
|
| 21 |
"history": {},
|
| 22 |
"categories": [
|
|
@@ -27,14 +39,15 @@ session_state = {
|
|
| 27 |
],
|
| 28 |
}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
def _clean(q: str) -> str:
|
| 31 |
q = (q or "").strip()
|
| 32 |
-
|
| 33 |
-
bad = ["generate", "question", "output", "explain", "instruction", "user said", "based on"]
|
| 34 |
lower = q.lower()
|
| 35 |
for b in bad:
|
| 36 |
if b in lower:
|
| 37 |
-
# забираем всё после найденной подстроки
|
| 38 |
idx = lower.find(b) + len(b)
|
| 39 |
q = q[idx:].lstrip(":,. ").strip()
|
| 40 |
lower = q.lower()
|
|
@@ -44,16 +57,20 @@ def _clean(q: str) -> str:
|
|
| 44 |
q = q.rstrip(".") + "?"
|
| 45 |
return q
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
Возвращает один новый вопрос по следующей неиспользованной MBTI-оси.
|
| 50 |
-
|
| 51 |
-
**kwargs проглатываем, чтобы не падать, если вызывающий код шлёт лишнее.
|
| 52 |
"""
|
| 53 |
history = session_state["history"].get(user_id, {"asked": []})
|
| 54 |
asked = history["asked"]
|
| 55 |
cats = session_state["categories"]
|
| 56 |
|
|
|
|
| 57 |
if len(asked) >= len(cats):
|
| 58 |
return "✅ All MBTI axes covered."
|
| 59 |
|
|
@@ -62,18 +79,17 @@ def generate_question(user_id: str, user_answer: str = None, qg_pipe=None, **kwa
|
|
| 62 |
session_state["history"][user_id] = history
|
| 63 |
|
| 64 |
prompt = (
|
| 65 |
-
f"Ask one
|
| 66 |
-
f"Start with What
|
| 67 |
-
f"Do not
|
| 68 |
-
f"
|
| 69 |
-
f"User context: {user_answer or ''}"
|
| 70 |
)
|
| 71 |
|
| 72 |
pipe = qg_pipe or QG_PIPE
|
| 73 |
out = pipe(prompt)[0]["generated_text"]
|
| 74 |
question = _clean(out)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
if not question or len(question.split()) < 3:
|
| 78 |
question = f"What aspects of {next_cat.lower()} best describe you and why?"
|
| 79 |
|
|
|
|
| 1 |
# core/interviewer.py
|
| 2 |
+
"""
|
| 3 |
+
🇬🇧 Interviewer logic module
|
| 4 |
+
Generates MBTI-category-based questions blindly (without reading user input).
|
| 5 |
+
|
| 6 |
+
🇷🇺 Модуль интервьюера
|
| 7 |
+
Генерирует вопросы по категориям MBTI, не анализируя ответы пользователя.
|
| 8 |
+
"""
|
| 9 |
|
| 10 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 11 |
|
| 12 |
+
# --------------------------------------------------------------
|
| 13 |
+
# 1️⃣ Настройки
|
| 14 |
+
# --------------------------------------------------------------
|
| 15 |
QG_MODEL = "google/flan-t5-small"
|
| 16 |
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(QG_MODEL)
|
| 18 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(QG_MODEL)
|
| 19 |
|
|
|
|
| 20 |
QG_PIPE = pipeline(
|
| 21 |
"text2text-generation",
|
| 22 |
+
model=model,
|
| 23 |
tokenizer=tokenizer,
|
| 24 |
max_new_tokens=40,
|
| 25 |
num_beams=4,
|
| 26 |
no_repeat_ngram_size=4,
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# --------------------------------------------------------------
|
| 30 |
+
# 2️⃣ Состояние сессии
|
| 31 |
+
# --------------------------------------------------------------
|
| 32 |
session_state = {
|
| 33 |
"history": {},
|
| 34 |
"categories": [
|
|
|
|
| 39 |
],
|
| 40 |
}
|
| 41 |
|
| 42 |
+
# --------------------------------------------------------------
|
| 43 |
+
# 3️⃣ Очистка текста от инструкций
|
| 44 |
+
# --------------------------------------------------------------
|
| 45 |
def _clean(q: str) -> str:
|
| 46 |
q = (q or "").strip()
|
| 47 |
+
bad = ["generate", "question", "output", "instruction", "explain", "user", "context"]
|
|
|
|
| 48 |
lower = q.lower()
|
| 49 |
for b in bad:
|
| 50 |
if b in lower:
|
|
|
|
| 51 |
idx = lower.find(b) + len(b)
|
| 52 |
q = q[idx:].lstrip(":,. ").strip()
|
| 53 |
lower = q.lower()
|
|
|
|
| 57 |
q = q.rstrip(".") + "?"
|
| 58 |
return q
|
| 59 |
|
| 60 |
+
|
| 61 |
+
# --------------------------------------------------------------
|
| 62 |
+
# 4️⃣ Генерация вопроса
|
| 63 |
+
# --------------------------------------------------------------
|
| 64 |
+
def generate_question(user_id: str, qg_pipe=None, **kwargs) -> str:
|
| 65 |
"""
|
| 66 |
Возвращает один новый вопрос по следующей неиспользованной MBTI-оси.
|
| 67 |
+
Не использует ответ пользователя.
|
|
|
|
| 68 |
"""
|
| 69 |
history = session_state["history"].get(user_id, {"asked": []})
|
| 70 |
asked = history["asked"]
|
| 71 |
cats = session_state["categories"]
|
| 72 |
|
| 73 |
+
# если все категории пройдены
|
| 74 |
if len(asked) >= len(cats):
|
| 75 |
return "✅ All MBTI axes covered."
|
| 76 |
|
|
|
|
| 79 |
session_state["history"][user_id] = history
|
| 80 |
|
| 81 |
prompt = (
|
| 82 |
+
f"Ask one natural, open-ended question about {next_cat}. "
|
| 83 |
+
f"Start with What, Why, How, or When. "
|
| 84 |
+
f"Do not include any instructions, explanations, or quotes. "
|
| 85 |
+
f"Output only the question itself."
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
pipe = qg_pipe or QG_PIPE
|
| 89 |
out = pipe(prompt)[0]["generated_text"]
|
| 90 |
question = _clean(out)
|
| 91 |
|
| 92 |
+
# fallback — если модель дала пустой или мусорный текст
|
| 93 |
if not question or len(question.split()) < 3:
|
| 94 |
question = f"What aspects of {next_cat.lower()} best describe you and why?"
|
| 95 |
|