okt_gpt2 / app.py
user
hh
5c18653
raw
history blame
1.81 kB
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()