test
Browse files
app.py
CHANGED
@@ -1,11 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
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 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,33 +23,35 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
19 |
-
|
20 |
-
for
|
21 |
-
if
|
22 |
-
|
23 |
-
if
|
24 |
-
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/
|
45 |
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
"""
|
6 |
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
|
7 |
"""
|
|
|
8 |
|
9 |
+
# Инициализация модели и токенизатора
|
10 |
+
MODEL_NAME = "yandex/YandexGPT-5-Lite-8B-pretrain"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, legacy=False)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
MODEL_NAME,
|
14 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu",
|
15 |
+
torch_dtype="auto",
|
16 |
+
)
|
17 |
|
18 |
def respond(
|
19 |
message,
|
|
|
23 |
temperature,
|
24 |
top_p,
|
25 |
):
|
26 |
+
# Формируем контекст из истории
|
27 |
+
full_prompt = f"{system_message}\n\n"
|
28 |
+
for user_msg, assistant_msg in history:
|
29 |
+
if user_msg:
|
30 |
+
full_prompt += f"User: {user_msg}\n"
|
31 |
+
if assistant_msg:
|
32 |
+
full_prompt += f"Assistant: {assistant_msg}\n"
|
33 |
+
full_prompt += f"User: {message}\nAssistant:"
|
|
|
34 |
|
35 |
+
# Токенизация и генерация
|
36 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
37 |
+
outputs = model.generate(
|
38 |
+
**inputs,
|
39 |
+
max_new_tokens=max_tokens,
|
|
|
40 |
temperature=temperature,
|
41 |
top_p=top_p,
|
42 |
+
do_sample=True,
|
43 |
+
pad_token_id=tokenizer.eos_token_id,
|
44 |
+
stream=True
|
45 |
+
)
|
46 |
+
|
47 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
48 |
+
# Убираем начальный промпт из ответа
|
49 |
+
response = response[len(full_prompt):]
|
50 |
+
yield response
|
51 |
|
52 |
|
53 |
"""
|
54 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docsx/chatinterface
|
55 |
"""
|
56 |
demo = gr.ChatInterface(
|
57 |
respond,
|