QAway-to commited on
Commit
e8b5e7a
·
1 Parent(s): 7aec9f9

google/flan-t5-small . app.py v1.8

Browse files
Files changed (1) hide show
  1. app.py +25 -36
app.py CHANGED
@@ -1,5 +1,6 @@
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
@@ -8,41 +9,25 @@ 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",
35
- model=mdl,
36
- tokenizer=tok,
37
- max_new_tokens=40,
38
- num_beams=4,
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..."
@@ -53,8 +38,9 @@ async def async_loader(update_fn, delay=0.15):
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,14 +54,17 @@ def analyze_and_ask(user_text, prev_count):
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:
@@ -89,7 +78,7 @@ def analyze_and_ask(user_text, prev_count):
89
 
90
 
91
  # --------------------------------------------------------------
92
- # 🧱 Gradio UI
93
  # --------------------------------------------------------------
94
  with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as demo:
95
  gr.Markdown(
 
1
  # app.py
2
+ import gradio as gr
3
+ import asyncio
4
  from itertools import cycle
5
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
6
  from core.utils import generate_first_question
 
9
 
10
 
11
  # --------------------------------------------------------------
12
+ # Всегда используем публичную модель Flan-T5-Small
13
  # --------------------------------------------------------------
14
+ QG_MODEL = "google/flan-t5-small"
15
+
16
+ try:
17
+ tokenizer = AutoTokenizer.from_pretrained(QG_MODEL)
18
+ model = AutoModelForSeq2SeqLM.from_pretrained(QG_MODEL)
19
+ QG_PIPE = pipeline(
20
+ "text2text-generation",
21
+ model=model,
22
+ tokenizer=tokenizer,
23
+ max_new_tokens=40,
24
+ num_beams=4,
25
+ no_repeat_ngram_size=4,
26
+ )
27
+ print(f" Loaded public interviewer model: {QG_MODEL}")
28
+ except Exception as e:
29
+ raise RuntimeError(f"❌ Failed to load {QG_MODEL}: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
31
 
32
  # --------------------------------------------------------------
33
  # 🌀 Асинхронная анимация "Thinking..."
 
38
  update_fn(f"💭 Interviewer is thinking... {frame}")
39
  await asyncio.sleep(delay)
40
 
41
+
42
  # --------------------------------------------------------------
43
+ # ⚙️ Основная логика
44
  # --------------------------------------------------------------
45
  def analyze_and_ask(user_text, prev_count):
46
  if not user_text.strip():
 
54
  n = 1
55
  counter = f"{n}/8"
56
 
57
+ # мгновенный отклик
58
  yield "⏳ Analyzing personality...", "💭 Interviewer is thinking... ⠋", counter
59
 
60
+ # анализ MBTI
61
  mbti_gen = analyze_mbti(user_text)
62
  mbti_text = ""
63
  for chunk in mbti_gen:
64
  mbti_text = chunk
65
  yield mbti_text, "💭 Interviewer is thinking... ⠙", counter
66
 
67
+ # генерация вопроса
68
  try:
69
  question = generate_question(user_id=user_id, user_answer=user_text, qg_pipe=QG_PIPE)
70
  except Exception as e:
 
78
 
79
 
80
  # --------------------------------------------------------------
81
+ # 🧱 Интерфейс Gradio
82
  # --------------------------------------------------------------
83
  with gr.Blocks(theme=gr.themes.Soft(), title="MBTI Personality Interviewer") as demo:
84
  gr.Markdown(