QAway-to
commited on
Commit
·
7aec9f9
1
Parent(s):
bbd6808
Token + T5 models switcher. app.py v1.8
Browse files
app.py
CHANGED
|
@@ -1,29 +1,34 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import asyncio
|
| 4 |
from itertools import cycle
|
|
|
|
| 5 |
from core.utils import generate_first_question
|
| 6 |
from core.mbti_analyzer import analyze_mbti
|
| 7 |
from core.interviewer import generate_question, session_state
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --------------------------------------------------------------
|
| 11 |
-
# ⚙️
|
| 12 |
# --------------------------------------------------------------
|
| 13 |
def load_qg_model():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
MODEL_CANDIDATES = [
|
| 19 |
-
"mrm8488/t5-small-finetuned-question-generation-ap", # быстрая, но часто недоступна
|
| 20 |
-
"iarfmoose/t5-base-question-generator", # качественная, чуть медленнее
|
| 21 |
-
"google/flan-t5-small" # fallback — всегда доступна
|
| 22 |
]
|
| 23 |
-
for name in
|
| 24 |
try:
|
| 25 |
-
tok = AutoTokenizer.from_pretrained(name)
|
| 26 |
-
mdl = AutoModelForSeq2SeqLM.from_pretrained(name)
|
| 27 |
print(f"✅ Loaded interviewer model: {name}")
|
| 28 |
return pipeline(
|
| 29 |
"text2text-generation",
|
|
@@ -34,23 +39,22 @@ def load_qg_model():
|
|
| 34 |
no_repeat_ngram_size=4,
|
| 35 |
)
|
| 36 |
except Exception as e:
|
| 37 |
-
print(f"⚠️
|
| 38 |
-
raise RuntimeError("❌ No available T5 model
|
| 39 |
|
| 40 |
-
# глобальный экземпляр пайплайна
|
| 41 |
QG_PIPE = load_qg_model()
|
| 42 |
|
| 43 |
# --------------------------------------------------------------
|
| 44 |
# 🌀 Асинхронная анимация "Thinking..."
|
| 45 |
# --------------------------------------------------------------
|
| 46 |
async def async_loader(update_fn, delay=0.15):
|
| 47 |
-
frames = cycle(["⠋",
|
| 48 |
for frame in frames:
|
| 49 |
update_fn(f"💭 Interviewer is thinking... {frame}")
|
| 50 |
await asyncio.sleep(delay)
|
| 51 |
|
| 52 |
# --------------------------------------------------------------
|
| 53 |
-
#
|
| 54 |
# --------------------------------------------------------------
|
| 55 |
def analyze_and_ask(user_text, prev_count):
|
| 56 |
if not user_text.strip():
|
|
@@ -64,17 +68,14 @@ def analyze_and_ask(user_text, prev_count):
|
|
| 64 |
n = 1
|
| 65 |
counter = f"{n}/8"
|
| 66 |
|
| 67 |
-
# мгновенный отклик
|
| 68 |
yield "⏳ Analyzing personality...", "💭 Interviewer is thinking... ⠋", counter
|
| 69 |
|
| 70 |
-
# анализ MBTI (стриминг)
|
| 71 |
mbti_gen = analyze_mbti(user_text)
|
| 72 |
mbti_text = ""
|
| 73 |
for chunk in mbti_gen:
|
| 74 |
mbti_text = chunk
|
| 75 |
yield mbti_text, "💭 Interviewer is thinking... ⠙", counter
|
| 76 |
|
| 77 |
-
# генерация вопроса
|
| 78 |
try:
|
| 79 |
question = generate_question(user_id=user_id, user_answer=user_text, qg_pipe=QG_PIPE)
|
| 80 |
except Exception as e:
|
|
@@ -86,8 +87,9 @@ def analyze_and_ask(user_text, prev_count):
|
|
| 86 |
|
| 87 |
yield mbti_text, question, counter
|
| 88 |
|
|
|
|
| 89 |
# --------------------------------------------------------------
|
| 90 |
-
# 🧱
|
| 91 |
# --------------------------------------------------------------
|
| 92 |
with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as demo:
|
| 93 |
gr.Markdown(
|
|
@@ -100,7 +102,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as
|
|
| 100 |
inp = gr.Textbox(
|
| 101 |
label="Ваш ответ",
|
| 102 |
placeholder="Например: I enjoy working with people and organizing events.",
|
| 103 |
-
lines=4
|
| 104 |
)
|
| 105 |
btn = gr.Button("Анализировать и задать новый вопрос", variant="primary")
|
| 106 |
with gr.Column(scale=1):
|
|
@@ -112,13 +114,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as
|
|
| 112 |
analyze_and_ask,
|
| 113 |
inputs=[inp, progress],
|
| 114 |
outputs=[mbti_out, interviewer_out, progress],
|
| 115 |
-
show_progress=True
|
| 116 |
)
|
| 117 |
|
| 118 |
demo.load(
|
| 119 |
lambda: ("", generate_first_question(), "0/8"),
|
| 120 |
inputs=None,
|
| 121 |
-
outputs=[mbti_out, interviewer_out, progress]
|
| 122 |
)
|
| 123 |
|
| 124 |
demo.queue(max_size=32).launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import os, gradio as gr, asyncio
|
|
|
|
| 3 |
from itertools import cycle
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
from core.utils import generate_first_question
|
| 6 |
from core.mbti_analyzer import analyze_mbti
|
| 7 |
from core.interviewer import generate_question, session_state
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# --------------------------------------------------------------
|
| 11 |
+
# 🔐 Авторизация Hugging Face
|
| 12 |
+
# --------------------------------------------------------------
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_MBTI") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 14 |
+
if HF_TOKEN:
|
| 15 |
+
print("✅ Hugging Face token detected.")
|
| 16 |
+
else:
|
| 17 |
+
print("⚠️ No HF token found – only public models will work.")
|
| 18 |
|
| 19 |
# --------------------------------------------------------------
|
| 20 |
+
# ⚙️ Автоматическая загрузка доступной T5 модели с токеном
|
| 21 |
# --------------------------------------------------------------
|
| 22 |
def load_qg_model():
|
| 23 |
+
candidates = [
|
| 24 |
+
"mrm8488/t5-small-finetuned-question-generation-ap", # fast, но часто приватная
|
| 25 |
+
"iarfmoose/t5-base-question-generator", # стабильная
|
| 26 |
+
"google/flan-t5-small" # публичный fallback
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
]
|
| 28 |
+
for name in candidates:
|
| 29 |
try:
|
| 30 |
+
tok = AutoTokenizer.from_pretrained(name, token=HF_TOKEN, use_auth_token=HF_TOKEN)
|
| 31 |
+
mdl = AutoModelForSeq2SeqLM.from_pretrained(name, token=HF_TOKEN, use_auth_token=HF_TOKEN)
|
| 32 |
print(f"✅ Loaded interviewer model: {name}")
|
| 33 |
return pipeline(
|
| 34 |
"text2text-generation",
|
|
|
|
| 39 |
no_repeat_ngram_size=4,
|
| 40 |
)
|
| 41 |
except Exception as e:
|
| 42 |
+
print(f"⚠️ Can't load {name}: {e}")
|
| 43 |
+
raise RuntimeError("❌ No available T5 model could be loaded even publicly.")
|
| 44 |
|
|
|
|
| 45 |
QG_PIPE = load_qg_model()
|
| 46 |
|
| 47 |
# --------------------------------------------------------------
|
| 48 |
# 🌀 Асинхронная анимация "Thinking..."
|
| 49 |
# --------------------------------------------------------------
|
| 50 |
async def async_loader(update_fn, delay=0.15):
|
| 51 |
+
frames = cycle(["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"])
|
| 52 |
for frame in frames:
|
| 53 |
update_fn(f"💭 Interviewer is thinking... {frame}")
|
| 54 |
await asyncio.sleep(delay)
|
| 55 |
|
| 56 |
# --------------------------------------------------------------
|
| 57 |
+
# 🧠 Основная логика
|
| 58 |
# --------------------------------------------------------------
|
| 59 |
def analyze_and_ask(user_text, prev_count):
|
| 60 |
if not user_text.strip():
|
|
|
|
| 68 |
n = 1
|
| 69 |
counter = f"{n}/8"
|
| 70 |
|
|
|
|
| 71 |
yield "⏳ Analyzing personality...", "💭 Interviewer is thinking... ⠋", counter
|
| 72 |
|
|
|
|
| 73 |
mbti_gen = analyze_mbti(user_text)
|
| 74 |
mbti_text = ""
|
| 75 |
for chunk in mbti_gen:
|
| 76 |
mbti_text = chunk
|
| 77 |
yield mbti_text, "💭 Interviewer is thinking... ⠙", counter
|
| 78 |
|
|
|
|
| 79 |
try:
|
| 80 |
question = generate_question(user_id=user_id, user_answer=user_text, qg_pipe=QG_PIPE)
|
| 81 |
except Exception as e:
|
|
|
|
| 87 |
|
| 88 |
yield mbti_text, question, counter
|
| 89 |
|
| 90 |
+
|
| 91 |
# --------------------------------------------------------------
|
| 92 |
+
# 🧱 Gradio UI
|
| 93 |
# --------------------------------------------------------------
|
| 94 |
with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as demo:
|
| 95 |
gr.Markdown(
|
|
|
|
| 102 |
inp = gr.Textbox(
|
| 103 |
label="Ваш ответ",
|
| 104 |
placeholder="Например: I enjoy working with people and organizing events.",
|
| 105 |
+
lines=4
|
| 106 |
)
|
| 107 |
btn = gr.Button("Анализировать и задать новый вопрос", variant="primary")
|
| 108 |
with gr.Column(scale=1):
|
|
|
|
| 114 |
analyze_and_ask,
|
| 115 |
inputs=[inp, progress],
|
| 116 |
outputs=[mbti_out, interviewer_out, progress],
|
| 117 |
+
show_progress=True
|
| 118 |
)
|
| 119 |
|
| 120 |
demo.load(
|
| 121 |
lambda: ("", generate_first_question(), "0/8"),
|
| 122 |
inputs=None,
|
| 123 |
+
outputs=[mbti_out, interviewer_out, progress]
|
| 124 |
)
|
| 125 |
|
| 126 |
demo.queue(max_size=32).launch(server_name="0.0.0.0", server_port=7860)
|