user
commited on
Commit
·
0d0714c
1
Parent(s):
a914099
hh
Browse files
app.py
CHANGED
@@ -1,44 +1,48 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
# Загрузка модели и токенизатора
|
5 |
-
model_name = "
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
|
9 |
-
# Создаем pipeline для генерации диалогов
|
10 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
11 |
-
|
12 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
13 |
# Формируем текст, который будет передан в модель
|
14 |
-
|
15 |
-
input_text += f"User: {message}"
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
max_length=len(tokenizer.encode(input_text)) + max_tokens,
|
21 |
-
temperature=temperature,
|
22 |
top_p=top_p,
|
23 |
-
|
24 |
-
num_return_sequences=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
|
27 |
# Извлечение и возврат текста ответа
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
# Настройка интерфейса Gradio
|
32 |
demo = gr.ChatInterface(
|
33 |
respond,
|
34 |
additional_inputs=[
|
35 |
-
gr.Textbox(value="
|
36 |
-
gr.Slider(minimum=1, maximum=2048, value=
|
37 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=1.
|
38 |
gr.Slider(
|
39 |
minimum=0.1,
|
40 |
maximum=1.0,
|
41 |
-
value=
|
42 |
),
|
43 |
],
|
44 |
)
|
|
|
1 |
import gradio as gr
|
2 |
+
import re
|
3 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
4 |
|
5 |
# Загрузка модели и токенизатора
|
6 |
+
model_name = "Dennterry/okt_bot"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
|
|
|
|
|
|
10 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
11 |
# Формируем текст, который будет передан в модель
|
12 |
+
inputs = tokenizer(f'@@ПЕРВЫЙ@@{message}@@ВТОРОЙ@@', return_tensors='pt')
|
|
|
13 |
|
14 |
+
generated_token_ids = model.generate(
|
15 |
+
**inputs,
|
16 |
+
top_k=50,
|
|
|
|
|
17 |
top_p=top_p,
|
18 |
+
num_beams=5,
|
19 |
+
num_return_sequences=3,
|
20 |
+
do_sample=True,
|
21 |
+
no_repeat_ngram_size=2,
|
22 |
+
temperature=temperature,
|
23 |
+
repetition_penalty=1.5,
|
24 |
+
length_penalty=0.6,
|
25 |
+
eos_token_id=50257,
|
26 |
+
max_new_tokens=max_tokens
|
27 |
)
|
28 |
|
29 |
# Извлечение и возврат текста ответа
|
30 |
+
context_with_response = [tokenizer.decode(sample_token_ids) for sample_token_ids in generated_token_ids]
|
31 |
+
result1 = re.sub(r'@@.*?@@', '', context_with_response[0])
|
32 |
+
result2 = result1[len(a):]
|
33 |
+
yield result2.strip()
|
34 |
|
35 |
# Настройка интерфейса Gradio
|
36 |
demo = gr.ChatInterface(
|
37 |
respond,
|
38 |
additional_inputs=[
|
39 |
+
gr.Textbox(value="Чебупели", label="System message"),
|
40 |
+
gr.Slider(minimum=1, maximum=2048, value=100, step=1, label="Max new tokens"),
|
41 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature"),
|
42 |
gr.Slider(
|
43 |
minimum=0.1,
|
44 |
maximum=1.0,
|
45 |
+
value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
46 |
),
|
47 |
],
|
48 |
)
|