|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
model_name = "rinna/nekomata-7b" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
def respond(input_text, system_message, max_tokens, temperature, top_p): |
|
|
|
input_text_combined = f"システム: {system_message}\nユーザー: {input_text}\n" |
|
|
|
|
|
inputs = tokenizer(input_text_combined, return_tensors="pt") |
|
|
|
|
|
outputs = model.generate( |
|
**inputs, |
|
max_length=max_tokens, |
|
top_p=top_p, |
|
do_sample=True, |
|
temperature=temperature, |
|
pad_token_id=tokenizer.eos_token_id |
|
) |
|
|
|
|
|
response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
return response |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## nekomataチャットボット") |
|
|
|
|
|
additional_inputs = [ |
|
gr.Textbox(value="ユーザーの質問と依頼のみに答えてください。ポジティブに.", label="システムメッセージ"), |
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="新規トークン最大"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="温度"), |
|
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (核サンプリング)") |
|
] |
|
|
|
|
|
input_text = gr.Textbox(label="ユーザー入力", placeholder="質問やテキストを入力してください") |
|
|
|
|
|
output_text = gr.Textbox(label="respond") |
|
|
|
|
|
submit_button = gr.Button("送信") |
|
submit_button.click(respond, inputs=[input_text] + additional_inputs, outputs=output_text) |
|
|
|
|
|
demo.launch() |