QAway-to
commited on
Commit
·
9458365
1
Parent(s):
a5e0a96
google/flan-t5-small . app.py v2.0
Browse files- core/interviewer.py +74 -92
core/interviewer.py
CHANGED
|
@@ -1,109 +1,91 @@
|
|
| 1 |
# core/interviewer.py
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
| 7 |
QG_MODEL = "google/flan-t5-small"
|
| 8 |
-
# QG_MODEL = "iarfmoose/t5-base-question-generator"
|
| 9 |
-
|
| 10 |
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(QG_MODEL)
|
| 12 |
model = AutoModelForSeq2SeqLM.from_pretrained(QG_MODEL)
|
| 13 |
|
| 14 |
-
|
| 15 |
"text2text-generation",
|
| 16 |
model=model,
|
| 17 |
tokenizer=tokenizer,
|
| 18 |
max_new_tokens=40,
|
| 19 |
num_beams=4,
|
| 20 |
-
no_repeat_ngram_size=4
|
| 21 |
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return False
|
| 42 |
-
|
| 43 |
-
def _clean(q: str) -> str:
|
| 44 |
-
q = q.strip().strip('"').strip("'")
|
| 45 |
-
# вырезаем префиксы вроде "question:", "generate a question:", etc.
|
| 46 |
-
bad = ["question:", "generate a question", "ask", "instruction", "output only", "you are"]
|
| 47 |
-
low = q.lower()
|
| 48 |
-
for b in bad:
|
| 49 |
-
if b in low:
|
| 50 |
-
# берём правую часть после двоеточия если есть
|
| 51 |
-
if ":" in q:
|
| 52 |
-
q = q.split(":", 1)[-1]
|
| 53 |
-
q = q.replace(b, "")
|
| 54 |
-
q = q.strip()
|
| 55 |
-
if not q.endswith("?"):
|
| 56 |
-
q += "?"
|
| 57 |
-
# короткие/мусорные — фоллбэк
|
| 58 |
-
if len(q.split()) < 3:
|
| 59 |
-
return "What do you usually enjoy doing in your free time?"
|
| 60 |
-
return q
|
| 61 |
-
|
| 62 |
-
def _template(category: str, user_answer: str) -> str:
|
| 63 |
"""
|
| 64 |
-
|
| 65 |
-
|
| 66 |
"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
S["asked"].append(category)
|
| 108 |
-
S["questions"].append(q)
|
| 109 |
-
return f"({category}) {q}"
|
|
|
|
| 1 |
# core/interviewer.py
|
| 2 |
+
"""
|
| 3 |
+
🇬🇧 Interviewer logic module
|
| 4 |
+
Generates context-aware MBTI interview questions using Flan-T5 model.
|
| 5 |
+
|
| 6 |
+
🇷🇺 Модуль логики интервьюера
|
| 7 |
+
Генерирует вопросы по категориям MBTI с использованием Flan-T5.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 11 |
+
import random
|
| 12 |
|
| 13 |
+
# --------------------------------------------------------------
|
| 14 |
+
# 1️⃣ Настройки
|
| 15 |
+
# --------------------------------------------------------------
|
| 16 |
QG_MODEL = "google/flan-t5-small"
|
|
|
|
|
|
|
| 17 |
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(QG_MODEL)
|
| 19 |
model = AutoModelForSeq2SeqLM.from_pretrained(QG_MODEL)
|
| 20 |
|
| 21 |
+
qg_pipe = pipeline(
|
| 22 |
"text2text-generation",
|
| 23 |
model=model,
|
| 24 |
tokenizer=tokenizer,
|
| 25 |
max_new_tokens=40,
|
| 26 |
num_beams=4,
|
| 27 |
+
no_repeat_ngram_size=4,
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# --------------------------------------------------------------
|
| 31 |
+
# 2️⃣ Состояние сессии
|
| 32 |
+
# --------------------------------------------------------------
|
| 33 |
+
session_state = {
|
| 34 |
+
"history": {},
|
| 35 |
+
"categories": [
|
| 36 |
+
"Extroversion", "Introversion",
|
| 37 |
+
"Sensing", "Intuition",
|
| 38 |
+
"Thinking", "Feeling",
|
| 39 |
+
"Judging", "Perceiving"
|
| 40 |
+
],
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# --------------------------------------------------------------
|
| 45 |
+
# 3️⃣ Генерация нового вопроса
|
| 46 |
+
# --------------------------------------------------------------
|
| 47 |
+
def generate_question(user_id: str, user_answer: str = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
+
Generates one question per MBTI axis.
|
| 50 |
+
Avoids repeating previous ones within the same session.
|
| 51 |
"""
|
| 52 |
+
history = session_state["history"].get(user_id, {"asked": []})
|
| 53 |
+
asked = history["asked"]
|
| 54 |
+
all_cats = session_state["categories"]
|
| 55 |
+
|
| 56 |
+
# Если все категории пройдены
|
| 57 |
+
if len(asked) >= len(all_cats):
|
| 58 |
+
return "✅ All MBTI axes covered."
|
| 59 |
+
|
| 60 |
+
# Выбираем следующую категорию
|
| 61 |
+
next_cat = next(c for c in all_cats if c not in asked)
|
| 62 |
+
history["asked"].append(next_cat)
|
| 63 |
+
session_state["history"][user_id] = history
|
| 64 |
+
|
| 65 |
+
# Промпт для T5
|
| 66 |
+
prompt = (
|
| 67 |
+
f"Generate one open-ended question about {next_cat} based on this context: '{user_answer}'. "
|
| 68 |
+
f"Do not repeat or explain. Output only the question itself."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
output = qg_pipe(prompt)[0]["generated_text"].strip()
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"⚠️ Generation error: {e}"
|
| 75 |
+
|
| 76 |
+
# Очистка мусора — чтобы не было “Generate a question about...”
|
| 77 |
+
bad_phrases = [
|
| 78 |
+
"generate", "question about", "output", "explain", "instruction", "user said"
|
| 79 |
+
]
|
| 80 |
+
for bp in bad_phrases:
|
| 81 |
+
if bp.lower() in output.lower():
|
| 82 |
+
output = output.split(bp, 1)[-1].strip().lstrip(":").strip()
|
| 83 |
+
|
| 84 |
+
# Убеждаемся, что начинается с нужного слова
|
| 85 |
+
if not output[0].isupper():
|
| 86 |
+
output = output.capitalize()
|
| 87 |
+
|
| 88 |
+
if "?" not in output:
|
| 89 |
+
output += "?"
|
| 90 |
+
|
| 91 |
+
return f"({next_cat}) {output}"
|
|
|
|
|
|
|
|
|