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