QAway-to
commited on
Commit
·
7fa6779
1
Parent(s):
cf62ab3
New model Google-gemma. v1.0
Browse files
app.py
CHANGED
|
@@ -1,40 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import concurrent.futures
|
| 3 |
import time
|
| 4 |
-
from transformers import
|
| 5 |
-
AutoTokenizer,
|
| 6 |
-
AutoModelForSequenceClassification,
|
| 7 |
-
pipeline,
|
| 8 |
-
)
|
| 9 |
|
| 10 |
# =========================================================
|
| 11 |
# 1. Настройка моделей
|
| 12 |
# =========================================================
|
| 13 |
MBTI_MODEL_ID = "f3nsmart/MBTIclassifier"
|
| 14 |
-
LLM_MODEL_ID = "
|
| 15 |
|
|
|
|
| 16 |
mbti_tokenizer = AutoTokenizer.from_pretrained(MBTI_MODEL_ID)
|
| 17 |
mbti_model = AutoModelForSequenceClassification.from_pretrained(MBTI_MODEL_ID)
|
| 18 |
analyzer = pipeline("text-classification", model=mbti_model, tokenizer=mbti_tokenizer, return_all_scores=True)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# =========================================================
|
| 23 |
-
# 2. Основная
|
| 24 |
# =========================================================
|
| 25 |
def classify_and_ask(user_input, question_count):
|
|
|
|
|
|
|
|
|
|
| 26 |
if not user_input.strip():
|
| 27 |
return "⚠️ Введите текст.", "⚠️ Вопрос не сформирован.", question_count
|
| 28 |
|
| 29 |
start_time = time.perf_counter()
|
| 30 |
|
|
|
|
| 31 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 32 |
future_analysis = executor.submit(lambda: analyzer(user_input))
|
| 33 |
-
future_question = executor.submit(lambda:
|
| 34 |
-
f"You are
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
))
|
| 39 |
|
| 40 |
analysis_result = future_analysis.result()
|
|
@@ -43,11 +53,15 @@ def classify_and_ask(user_input, question_count):
|
|
| 43 |
elapsed = time.perf_counter() - start_time
|
| 44 |
print(f"⏱ Время обработки запроса: {elapsed:.2f} сек")
|
| 45 |
|
|
|
|
| 46 |
results = sorted(analysis_result[0], key=lambda x: x["score"], reverse=True)
|
| 47 |
top = "\n".join([f"{r['label']} → {r['score']:.3f}" for r in results[:3]])
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
question_count += 1
|
| 53 |
progress = f"{question_count}/30"
|
|
@@ -58,9 +72,11 @@ def classify_and_ask(user_input, question_count):
|
|
| 58 |
# =========================================================
|
| 59 |
# 3. Интерфейс Gradio
|
| 60 |
# =========================================================
|
| 61 |
-
with gr.Blocks(title="MBTI Interactive Interview") as demo:
|
| 62 |
-
gr.Markdown(
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
question_state = gr.State(1)
|
| 66 |
|
|
@@ -69,9 +85,9 @@ with gr.Blocks(title="MBTI Interactive Interview") as demo:
|
|
| 69 |
inp = gr.Textbox(
|
| 70 |
label="Введите свой ответ",
|
| 71 |
placeholder="Например: I enjoy working with people and organizing events.",
|
| 72 |
-
lines=4
|
| 73 |
)
|
| 74 |
-
btn = gr.Button("Анализировать и задать новый вопрос")
|
| 75 |
progress = gr.Markdown("**1/30**", elem_id="progress")
|
| 76 |
|
| 77 |
with gr.Column(scale=1):
|
|
@@ -79,7 +95,7 @@ with gr.Blocks(title="MBTI Interactive Interview") as demo:
|
|
| 79 |
out_question = gr.Textbox(
|
| 80 |
label="💬 Вопрос от интервьюера",
|
| 81 |
value="How do you usually spend your free time?",
|
| 82 |
-
lines=3
|
| 83 |
)
|
| 84 |
|
| 85 |
btn.click(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import concurrent.futures
|
| 3 |
import time
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM, pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# =========================================================
|
| 7 |
# 1. Настройка моделей
|
| 8 |
# =========================================================
|
| 9 |
MBTI_MODEL_ID = "f3nsmart/MBTIclassifier"
|
| 10 |
+
LLM_MODEL_ID = "google/gemma-2b-it" # Быстрая, контекстная и лаконичная
|
| 11 |
|
| 12 |
+
# Классификатор (твой fine-tuned)
|
| 13 |
mbti_tokenizer = AutoTokenizer.from_pretrained(MBTI_MODEL_ID)
|
| 14 |
mbti_model = AutoModelForSequenceClassification.from_pretrained(MBTI_MODEL_ID)
|
| 15 |
analyzer = pipeline("text-classification", model=mbti_model, tokenizer=mbti_tokenizer, return_all_scores=True)
|
| 16 |
|
| 17 |
+
# Интервьюер (Gemma)
|
| 18 |
+
q_tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_ID)
|
| 19 |
+
q_model = AutoModelForCausalLM.from_pretrained(LLM_MODEL_ID)
|
| 20 |
+
interviewer = pipeline("text-generation", model=q_model, tokenizer=q_tokenizer)
|
| 21 |
|
| 22 |
# =========================================================
|
| 23 |
+
# 2. Основная логика
|
| 24 |
# =========================================================
|
| 25 |
def classify_and_ask(user_input, question_count):
|
| 26 |
+
"""
|
| 27 |
+
Классифицирует MBTI и генерирует следующий вопрос от интервьюера.
|
| 28 |
+
"""
|
| 29 |
if not user_input.strip():
|
| 30 |
return "⚠️ Введите текст.", "⚠️ Вопрос не сформирован.", question_count
|
| 31 |
|
| 32 |
start_time = time.perf_counter()
|
| 33 |
|
| 34 |
+
# Асинхронное выполнение двух задач (аналитика + вопрос)
|
| 35 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 36 |
future_analysis = executor.submit(lambda: analyzer(user_input))
|
| 37 |
+
future_question = executor.submit(lambda: interviewer(
|
| 38 |
+
f"""You are an MBTI interviewer.
|
| 39 |
+
The user answered: "{user_input}".
|
| 40 |
+
Ask one short, open-ended question that helps reveal their personality type.
|
| 41 |
+
Avoid repeating previous topics.
|
| 42 |
+
Start directly with 'How', 'Why', 'What', or 'When'.
|
| 43 |
+
Output only the question itself, nothing else.""",
|
| 44 |
+
max_new_tokens=40,
|
| 45 |
+
temperature=0.8,
|
| 46 |
+
top_p=0.9,
|
| 47 |
+
do_sample=True,
|
| 48 |
))
|
| 49 |
|
| 50 |
analysis_result = future_analysis.result()
|
|
|
|
| 53 |
elapsed = time.perf_counter() - start_time
|
| 54 |
print(f"⏱ Время обработки запроса: {elapsed:.2f} сек")
|
| 55 |
|
| 56 |
+
# Парсим результаты
|
| 57 |
results = sorted(analysis_result[0], key=lambda x: x["score"], reverse=True)
|
| 58 |
top = "\n".join([f"{r['label']} → {r['score']:.3f}" for r in results[:3]])
|
| 59 |
|
| 60 |
+
raw_q = question_result[0]["generated_text"].strip()
|
| 61 |
+
# Убираем лишние фразы и добавляем "?" если отсутствует
|
| 62 |
+
question = raw_q.split("\n")[-1].split(":")[-1].strip()
|
| 63 |
+
if not question.endswith("?"):
|
| 64 |
+
question += "?"
|
| 65 |
|
| 66 |
question_count += 1
|
| 67 |
progress = f"{question_count}/30"
|
|
|
|
| 72 |
# =========================================================
|
| 73 |
# 3. Интерфейс Gradio
|
| 74 |
# =========================================================
|
| 75 |
+
with gr.Blocks(title="MBTI Interactive Interview (Gemma)") as demo:
|
| 76 |
+
gr.Markdown(
|
| 77 |
+
"## 🧠 MBTI Personality Interviewer\n"
|
| 78 |
+
"Определи личностный тип и получи следующий вопрос от интервьюера."
|
| 79 |
+
)
|
| 80 |
|
| 81 |
question_state = gr.State(1)
|
| 82 |
|
|
|
|
| 85 |
inp = gr.Textbox(
|
| 86 |
label="Введите свой ответ",
|
| 87 |
placeholder="Например: I enjoy working with people and organizing events.",
|
| 88 |
+
lines=4,
|
| 89 |
)
|
| 90 |
+
btn = gr.Button("Анализировать и задать новый вопрос", variant="primary")
|
| 91 |
progress = gr.Markdown("**1/30**", elem_id="progress")
|
| 92 |
|
| 93 |
with gr.Column(scale=1):
|
|
|
|
| 95 |
out_question = gr.Textbox(
|
| 96 |
label="💬 Вопрос от интервьюера",
|
| 97 |
value="How do you usually spend your free time?",
|
| 98 |
+
lines=3,
|
| 99 |
)
|
| 100 |
|
| 101 |
btn.click(
|