QAway-to
commited on
Commit
·
4df42a0
1
Parent(s):
206b8e2
Back to normal app.py v1.4
Browse files- app.py +15 -22
- core/interviewer.py +8 -22
app.py
CHANGED
|
@@ -5,45 +5,37 @@ from core.mbti_analyzer import analyze_mbti
|
|
| 5 |
from core.interviewer import generate_question, session_state
|
| 6 |
|
| 7 |
def analyze_and_ask(user_text, prev_count):
|
| 8 |
-
"""Главная функция генерации (анализ → вопрос)."""
|
| 9 |
if not user_text.strip():
|
| 10 |
yield "⚠️ Please enter your answer.", "", prev_count
|
| 11 |
return
|
| 12 |
|
| 13 |
user_id = "default_user"
|
|
|
|
| 14 |
try:
|
| 15 |
n = int(prev_count.split("/")[0]) + 1
|
| 16 |
except Exception:
|
| 17 |
n = 1
|
| 18 |
-
|
| 19 |
counter = f"{n}/8"
|
| 20 |
|
| 21 |
-
# 1️⃣
|
|
|
|
|
|
|
|
|
|
| 22 |
mbti_gen = analyze_mbti(user_text)
|
| 23 |
mbti_text = ""
|
| 24 |
for chunk in mbti_gen:
|
| 25 |
mbti_text = chunk
|
| 26 |
yield mbti_text, "💭 Interviewer is thinking...", counter
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
next_q = ""
|
| 31 |
-
cat = ""
|
| 32 |
-
for data in q_gen:
|
| 33 |
-
if isinstance(data, str):
|
| 34 |
-
next_q = data
|
| 35 |
-
yield mbti_text, next_q, counter
|
| 36 |
-
else:
|
| 37 |
-
cat, next_q = data
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"8/8"
|
| 46 |
-
)
|
| 47 |
|
| 48 |
# --------------------------------------------------------------
|
| 49 |
# Интерфейс Gradio
|
|
@@ -70,7 +62,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as
|
|
| 70 |
btn.click(
|
| 71 |
analyze_and_ask,
|
| 72 |
inputs=[inp, progress],
|
| 73 |
-
outputs=[mbti_out, interviewer_out, progress]
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
demo.load(
|
|
|
|
| 5 |
from core.interviewer import generate_question, session_state
|
| 6 |
|
| 7 |
def analyze_and_ask(user_text, prev_count):
|
|
|
|
| 8 |
if not user_text.strip():
|
| 9 |
yield "⚠️ Please enter your answer.", "", prev_count
|
| 10 |
return
|
| 11 |
|
| 12 |
user_id = "default_user"
|
| 13 |
+
|
| 14 |
try:
|
| 15 |
n = int(prev_count.split("/")[0]) + 1
|
| 16 |
except Exception:
|
| 17 |
n = 1
|
|
|
|
| 18 |
counter = f"{n}/8"
|
| 19 |
|
| 20 |
+
# 1️⃣ Сразу показываем “Thinking...”
|
| 21 |
+
yield "⏳ Analyzing personality...", "💭 Interviewer is thinking...", counter
|
| 22 |
+
|
| 23 |
+
# 2️⃣ Анализ MBTI
|
| 24 |
mbti_gen = analyze_mbti(user_text)
|
| 25 |
mbti_text = ""
|
| 26 |
for chunk in mbti_gen:
|
| 27 |
mbti_text = chunk
|
| 28 |
yield mbti_text, "💭 Interviewer is thinking...", counter
|
| 29 |
|
| 30 |
+
# 3️⃣ Генерация вопроса
|
| 31 |
+
question = generate_question(user_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
if question.startswith("✅ All"):
|
| 34 |
+
yield f"{mbti_text}\n\nSession complete.", "🎯 All MBTI axes covered.", "8/8"
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
# 4️⃣ Финальный вывод
|
| 38 |
+
yield mbti_text, question, counter
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --------------------------------------------------------------
|
| 41 |
# Интерфейс Gradio
|
|
|
|
| 62 |
btn.click(
|
| 63 |
analyze_and_ask,
|
| 64 |
inputs=[inp, progress],
|
| 65 |
+
outputs=[mbti_out, interviewer_out, progress],
|
| 66 |
+
show_progress=True
|
| 67 |
)
|
| 68 |
|
| 69 |
demo.load(
|
core/interviewer.py
CHANGED
|
@@ -18,7 +18,6 @@ llm_pipe = pipeline(
|
|
| 18 |
top_p=0.9,
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Основные оси MBTI
|
| 22 |
CATEGORIES = [
|
| 23 |
"Introversion", "Extroversion",
|
| 24 |
"Sensing", "Intuition",
|
|
@@ -26,20 +25,12 @@ CATEGORIES = [
|
|
| 26 |
"Judging", "Perceiving"
|
| 27 |
]
|
| 28 |
|
| 29 |
-
# Память по сессиям
|
| 30 |
session_state = {}
|
| 31 |
|
| 32 |
def init_session(user_id: str):
|
| 33 |
-
"""
|
| 34 |
-
session_state[user_id] = {
|
| 35 |
-
"asked": [],
|
| 36 |
-
"answers": {},
|
| 37 |
-
"iteration": 1,
|
| 38 |
-
"dominant_axis": None
|
| 39 |
-
}
|
| 40 |
|
| 41 |
def select_next_category(user_id: str):
|
| 42 |
-
"""Выбирает категорию, не повторяя."""
|
| 43 |
s = session_state[user_id]
|
| 44 |
remaining = [c for c in CATEGORIES if c not in s["asked"]]
|
| 45 |
if not remaining:
|
|
@@ -49,29 +40,24 @@ def select_next_category(user_id: str):
|
|
| 49 |
return next_cat
|
| 50 |
|
| 51 |
def build_prompt(category: str):
|
| 52 |
-
"""Формирует промпт для TinyLlama."""
|
| 53 |
return (
|
| 54 |
-
f"Ask one open-ended
|
| 55 |
-
f"Do not repeat previous questions. "
|
| 56 |
-
f"Output only the question
|
| 57 |
)
|
| 58 |
|
| 59 |
-
def generate_question(user_id: str):
|
| 60 |
-
"""
|
| 61 |
if user_id not in session_state:
|
| 62 |
init_session(user_id)
|
| 63 |
|
| 64 |
category = select_next_category(user_id)
|
| 65 |
if not category:
|
| 66 |
-
|
| 67 |
-
return None, None
|
| 68 |
|
| 69 |
-
yield f"💭 Interviewer is thinking (category: {category})..."
|
| 70 |
prompt = build_prompt(category)
|
| 71 |
raw = llm_pipe(prompt)[0]["generated_text"]
|
| 72 |
-
|
| 73 |
-
# Очистка
|
| 74 |
question = raw.strip().split("\n")[0]
|
| 75 |
if "?" not in question:
|
| 76 |
question += "?"
|
| 77 |
-
return category
|
|
|
|
| 18 |
top_p=0.9,
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
CATEGORIES = [
|
| 22 |
"Introversion", "Extroversion",
|
| 23 |
"Sensing", "Intuition",
|
|
|
|
| 25 |
"Judging", "Perceiving"
|
| 26 |
]
|
| 27 |
|
|
|
|
| 28 |
session_state = {}
|
| 29 |
|
| 30 |
def init_session(user_id: str):
|
| 31 |
+
session_state[user_id] = {"asked": [], "answers": {}, "iteration": 1}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def select_next_category(user_id: str):
|
|
|
|
| 34 |
s = session_state[user_id]
|
| 35 |
remaining = [c for c in CATEGORIES if c not in s["asked"]]
|
| 36 |
if not remaining:
|
|
|
|
| 40 |
return next_cat
|
| 41 |
|
| 42 |
def build_prompt(category: str):
|
|
|
|
| 43 |
return (
|
| 44 |
+
f"Ask one open-ended question about {category}. "
|
| 45 |
+
f"Do not repeat or rephrase previous questions. "
|
| 46 |
+
f"Output only the question itself."
|
| 47 |
)
|
| 48 |
|
| 49 |
+
def generate_question(user_id: str) -> str:
|
| 50 |
+
"""Возвращает только финальный вопрос."""
|
| 51 |
if user_id not in session_state:
|
| 52 |
init_session(user_id)
|
| 53 |
|
| 54 |
category = select_next_category(user_id)
|
| 55 |
if not category:
|
| 56 |
+
return "✅ All 8 categories completed."
|
|
|
|
| 57 |
|
|
|
|
| 58 |
prompt = build_prompt(category)
|
| 59 |
raw = llm_pipe(prompt)[0]["generated_text"]
|
|
|
|
|
|
|
| 60 |
question = raw.strip().split("\n")[0]
|
| 61 |
if "?" not in question:
|
| 62 |
question += "?"
|
| 63 |
+
return f"({category}) {question}"
|