| | import gradio as gr |
| | from transformers import AutoModelForCausalLM, AutoTokenizer |
| | import torch |
| |
|
| | |
| | model_name = "EleutherAI/Pythia-1b" |
| | tokenizer = AutoTokenizer.from_pretrained(model_name) |
| | model = AutoModelForCausalLM.from_pretrained(model_name, ignore_mismatched_sizes=True) |
| |
|
| | |
| | def respond(message, history, max_tokens, temperature, top_p): |
| | |
| | if history is None: |
| | history = [] |
| |
|
| | input_text = "" |
| | for user_message, bot_response in history: |
| | input_text += f"User: {user_message}\nAssistant: {bot_response}\n" |
| | input_text += f"User: {message}\nAssistant:" |
| |
|
| | |
| | inputs = tokenizer(input_text, return_tensors="pt") |
| |
|
| | |
| | with torch.no_grad(): |
| | outputs = model.generate( |
| | inputs.input_ids, |
| | max_length=inputs.input_ids.shape[1] + max_tokens, |
| | do_sample=True, |
| | top_p=top_p, |
| | temperature=temperature, |
| | ) |
| |
|
| | |
| | response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
| | |
| | response = response.split("Assistant:")[-1].strip() |
| |
|
| | |
| | history.append((message, response)) |
| | return response, history |
| |
|
| | |
| | with gr.Blocks() as demo: |
| | gr.Markdown("## AIチャット") |
| | chatbot = gr.Chatbot() |
| | msg = gr.Textbox(label="あなたのメッセージ", placeholder="ここにメッセージを入力...") |
| | max_tokens = gr.Slider(1, 2048, value=512, step=1, label="新規トークン最大") |
| | temperature = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="温度") |
| | top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (核サンプリング)") |
| | send_button = gr.Button("送信") |
| | clear = gr.Button("クリア") |
| |
|
| | def clear_history(): |
| | return [], [] |
| |
|
| | send_button.click(respond, inputs=[msg, chatbot, max_tokens, temperature, top_p], outputs=[chatbot, chatbot]) |
| | clear.click(clear_history, outputs=[chatbot]) |
| |
|
| | demo.launch() |