QAway-to commited on
Commit
3c8cb2b
·
1 Parent(s): 4236d9d

google/flan-t5-small . app.py v2.2

Browse files
Files changed (1) hide show
  1. core/interviewer.py +42 -53
core/interviewer.py CHANGED
@@ -1,91 +1,80 @@
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}"
 
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
+ _model = AutoModelForSeq2SeqLM.from_pretrained(QG_MODEL)
9
 
10
+ # используем заглавное имя, чтобы отличать от параметра в функции
11
+ QG_PIPE = pipeline(
12
  "text2text-generation",
13
+ model=_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": [
23
  "Extroversion", "Introversion",
24
  "Sensing", "Intuition",
25
  "Thinking", "Feeling",
26
+ "Judging", "Perceiving",
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()
41
+ if q and not q[0].isupper():
42
+ q = q.capitalize()
43
+ if "?" not in q:
44
+ q = q.rstrip(".") + "?"
45
+ return q
46
+
47
+ def generate_question(user_id: str, user_answer: str = None, qg_pipe=None, **kwargs) -> str:
48
  """
49
+ Возвращает один новый вопрос по следующей неиспользованной MBTI-оси.
50
+ Параметр qg_pipe — необязателен, игнорируем если не передали.
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
 
60
+ next_cat = next(c for c in cats if c not in asked)
61
+ asked.append(next_cat)
 
62
  session_state["history"][user_id] = history
63
 
 
64
  prompt = (
65
+ f"Ask one concise, open-ended question about {next_cat}. "
66
+ f"Start with What/Why/How/When. "
67
+ f"Do not mention instructions. "
68
+ f"Do not repeat or quote the user. "
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
 
80
+ return f"({next_cat}) {question}"