Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
-
# Используем модель
|
5 |
model_name = "gpt2" # Модель GPT-2
|
6 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
@@ -14,18 +14,19 @@ def respond(
|
|
14 |
temperature=0.7,
|
15 |
top_p=0.95,
|
16 |
):
|
|
|
17 |
if history is None:
|
18 |
-
history = []
|
19 |
elif not isinstance(history, list):
|
20 |
-
history = []
|
21 |
|
22 |
-
# Объединяем сообщения в
|
23 |
input_text = "\n".join([msg[1] for msg in history] + [message])
|
24 |
|
25 |
-
# Токенизация текста
|
26 |
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
27 |
|
28 |
-
# Генерация ответа
|
29 |
outputs = model.generate(
|
30 |
inputs["input_ids"],
|
31 |
max_length=max_tokens,
|
@@ -34,9 +35,10 @@ def respond(
|
|
34 |
do_sample=True,
|
35 |
)
|
36 |
|
|
|
37 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
|
39 |
-
#
|
40 |
response = format_response(response)
|
41 |
|
42 |
return response
|
@@ -52,13 +54,14 @@ def format_response(response):
|
|
52 |
return formatted_response
|
53 |
|
54 |
def extract_diagnosis(response):
|
55 |
-
# Простой способ извлечь диагноз (можно
|
56 |
diagnosis = response.split(".")[0] # Пример: диагноз - первая часть ответа
|
57 |
return diagnosis.strip()
|
58 |
|
59 |
def extract_operation(response):
|
60 |
# Извлекаем название операции из ответа
|
61 |
-
|
|
|
62 |
return operation.strip()
|
63 |
|
64 |
def extract_treatment(response):
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
+
# Используем модель GPT-2 для генерации текста
|
5 |
model_name = "gpt2" # Модель GPT-2
|
6 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
14 |
temperature=0.7,
|
15 |
top_p=0.95,
|
16 |
):
|
17 |
+
# Инициализация history как пустого списка, если его нет
|
18 |
if history is None:
|
19 |
+
history = []
|
20 |
elif not isinstance(history, list):
|
21 |
+
history = []
|
22 |
|
23 |
+
# Объединяем сообщения в историю, добавляя последнее сообщение пользователя
|
24 |
input_text = "\n".join([msg[1] for msg in history] + [message])
|
25 |
|
26 |
+
# Токенизация текста для модели
|
27 |
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
28 |
|
29 |
+
# Генерация ответа с использованием модели
|
30 |
outputs = model.generate(
|
31 |
inputs["input_ids"],
|
32 |
max_length=max_tokens,
|
|
|
35 |
do_sample=True,
|
36 |
)
|
37 |
|
38 |
+
# Декодируем ответ модели
|
39 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
40 |
|
41 |
+
# Форматируем ответ в соответствии с шаблоном
|
42 |
response = format_response(response)
|
43 |
|
44 |
return response
|
|
|
54 |
return formatted_response
|
55 |
|
56 |
def extract_diagnosis(response):
|
57 |
+
# Простой способ извлечь диагноз (можно улучшить с помощью NLP)
|
58 |
diagnosis = response.split(".")[0] # Пример: диагноз - первая часть ответа
|
59 |
return diagnosis.strip()
|
60 |
|
61 |
def extract_operation(response):
|
62 |
# Извлекаем название операции из ответа
|
63 |
+
# Здесь можно настроить под реальный сценарий
|
64 |
+
operation = "Не требуется" # Пример, что операция не требуется
|
65 |
return operation.strip()
|
66 |
|
67 |
def extract_treatment(response):
|