Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
|
|
|
|
5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
6 |
|
7 |
def respond(
|
@@ -12,79 +14,54 @@ def respond(
|
|
12 |
temperature,
|
13 |
top_p,
|
14 |
):
|
15 |
-
# Формирование списка сообщений для отправки модели
|
16 |
messages = [{"role": "system", "content": system_message}]
|
17 |
|
18 |
-
# Добавление истории разговора в список сообщений
|
19 |
for val in history:
|
20 |
if val[0]:
|
21 |
messages.append({"role": "user", "content": val[0]})
|
22 |
if val[1]:
|
23 |
messages.append({"role": "assistant", "content": val[1]})
|
24 |
|
25 |
-
# Добавление текущего сообщения пользователя
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
# Получение ответа от модели
|
29 |
response = ""
|
30 |
-
try:
|
31 |
-
for message in client.chat_completion(
|
32 |
-
messages,
|
33 |
-
max_tokens=max_tokens,
|
34 |
-
stream=True,
|
35 |
-
temperature=temperature,
|
36 |
-
top_p=top_p,
|
37 |
-
):
|
38 |
-
token = message.choices[0].delta.content
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
except Exception as e:
|
42 |
-
print(f"Error occurred during message generation: {e}")
|
43 |
-
yield "Произошла ошибка при обработке запроса."
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
diagnosis = response.split("\n")[0] # Первый ответ — диагноз
|
59 |
-
response = f"Предварительный диагноз: {diagnosis}\n"
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
# Уточняющие вопросы: задаем несколько вопросов, чтобы уточнить диагноз
|
65 |
-
questions = "Какие симптомы усилились за последние 24 часа? Есть ли у вас температура?"
|
66 |
-
response += f"Уточняющие вопросы для постановки диагноза: {questions}\n"
|
67 |
-
|
68 |
-
# Добавление финального сообщения
|
69 |
-
final_response = f"{response}\nСоздано больницей EMS штата Alta"
|
70 |
-
yield final_response
|
71 |
-
|
72 |
-
# Создание интерфейса Gradio
|
73 |
demo = gr.ChatInterface(
|
74 |
respond,
|
75 |
additional_inputs=[
|
76 |
-
gr.Textbox(
|
77 |
-
|
78 |
-
|
79 |
-
),
|
80 |
-
gr.Slider(minimum=1, maximum=2048, value=200, step=1, label="Максимальное количество новых токенов"),
|
81 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Температура"),
|
82 |
gr.Slider(
|
83 |
minimum=0.1,
|
84 |
maximum=1.0,
|
85 |
value=0.95,
|
86 |
step=0.05,
|
87 |
-
label="Top-p (
|
88 |
),
|
89 |
],
|
90 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
"""
|
5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
def respond(
|
|
|
14 |
temperature,
|
15 |
top_p,
|
16 |
):
|
|
|
17 |
messages = [{"role": "system", "content": system_message}]
|
18 |
|
|
|
19 |
for val in history:
|
20 |
if val[0]:
|
21 |
messages.append({"role": "user", "content": val[0]})
|
22 |
if val[1]:
|
23 |
messages.append({"role": "assistant", "content": val[1]})
|
24 |
|
|
|
25 |
messages.append({"role": "user", "content": message})
|
26 |
|
|
|
27 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
for message in client.chat_completion(
|
30 |
+
messages,
|
31 |
+
max_tokens=max_tokens,
|
32 |
+
stream=True,
|
33 |
+
temperature=temperature,
|
34 |
+
top_p=top_p,
|
35 |
+
):
|
36 |
+
token = message.choices[0].delta.content
|
37 |
+
response += token
|
38 |
|
39 |
+
# Добавьте логику для обработки медицинских запросов
|
40 |
+
if "диагноз" in message.lower() or "операция" in message.lower():
|
41 |
+
response += "\n\nПредварительный диагноз: [Ваш диагноз]\n"
|
42 |
+
response += "Операция: [Требуется ли операция и какая]\n"
|
43 |
+
response += "Уточняющие вопросы для постановки диагноза:\n"
|
44 |
+
response += "- Вопрос 1\n"
|
45 |
+
response += "- Вопрос 2\n"
|
46 |
+
response += "- Вопрос 3\n"
|
47 |
|
48 |
+
yield response
|
|
|
|
|
49 |
|
50 |
+
"""
|
51 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
52 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
demo = gr.ChatInterface(
|
54 |
respond,
|
55 |
additional_inputs=[
|
56 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
57 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
58 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
|
|
|
|
|
|
59 |
gr.Slider(
|
60 |
minimum=0.1,
|
61 |
maximum=1.0,
|
62 |
value=0.95,
|
63 |
step=0.05,
|
64 |
+
label="Top-p (nucleus sampling)",
|
65 |
),
|
66 |
],
|
67 |
)
|