MBTI / app.py
QAway-to
Back to normal app.py v1.5
0611243
raw
history blame
3.22 kB
# app.py
import gradio as gr
import asyncio
from itertools import cycle
from core.utils import generate_first_question
from core.mbti_analyzer import analyze_mbti
from core.interviewer import generate_question, session_state
async def async_loader(progress_fn):
"""Асинхронный loader-аниматор (вращающиеся точки)."""
frames = cycle(["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])
for _ in range(10):
await asyncio.sleep(0.2)
progress_fn(next(frames))
def analyze_and_ask(user_text, prev_count, progress=gr.Progress(track_tqdm=True)):
if not user_text.strip():
yield "⚠️ Please enter your answer.", "", prev_count
return
user_id = "default_user"
try:
n = int(prev_count.split("/")[0]) + 1
except Exception:
n = 1
counter = f"{n}/8"
# 1️⃣ Первое сообщение — мгновенно
yield "⏳ Analyzing personality...", "💭 Interviewer is thinking... ⠋", counter
# 2️⃣ Анимация лоадера в фоне
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(async_loader(lambda f: None))
# 3️⃣ Анализ MBTI
mbti_gen = analyze_mbti(user_text)
mbti_text = ""
for chunk in mbti_gen:
mbti_text = chunk
yield mbti_text, "💭 Interviewer is thinking... ⠙", counter
# 4️⃣ Генерация вопроса
question = generate_question(user_id)
if question.startswith("✅ All"):
yield f"{mbti_text}\n\nSession complete.", "🎯 All MBTI axes covered.", "8/8"
return
# 5️⃣ Финальный вывод
yield mbti_text, question, counter
# --------------------------------------------------------------
# UI
# --------------------------------------------------------------
with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as demo:
gr.Markdown(
"## 🧠 MBTI Personality Interviewer\n"
"Определи личностный тип и получи вопросы из разных категорий MBTI."
)
with gr.Row():
with gr.Column(scale=1):
inp = gr.Textbox(
label="Ваш ответ",
placeholder="Например: I enjoy working with people and organizing events.",
lines=4
)
btn = gr.Button("Анализировать и задать новый вопрос", variant="primary")
with gr.Column(scale=1):
mbti_out = gr.Textbox(label="📊 Анализ MBTI", lines=4)
interviewer_out = gr.Textbox(label="💬 Следующий вопрос", lines=3)
progress = gr.Textbox(label="⏳ Прогресс", value="0/8")
btn.click(
analyze_and_ask,
inputs=[inp, progress],
outputs=[mbti_out, interviewer_out, progress],
show_progress=True
)
demo.load(
lambda: ("", generate_first_question(), "0/8"),
inputs=None,
outputs=[mbti_out, interviewer_out, progress]
)
demo.queue(max_size=20).launch(server_name="0.0.0.0", server_port=7860)