上面的代码加上可视化界面
时间: 2023-09-04 09:11:04 浏览: 95
好的,以下是一个简单的可视化界面,你可以输入问题,AI会给出回答:
```python
import tkinter as tk
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("csarron/chitgpt")
model = AutoModelForCausalLM.from_pretrained("csarron/chitgpt")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
def generate_response(input_text):
input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt").to(device)
chat_history_ids = model.generate(
input_ids,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3,
do_sample=True,
top_k=10,
top_p=0.7,
temperature=0.8,
)
response = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1] :][0], skip_special_tokens=True)
return response
def send():
input_text = input_entry.get()
output_text.delete("1.0", tk.END)
response = generate_response(input_text)
output_text.insert(tk.END, response)
window = tk.Tk()
window.title("ChitGPT Chatbot")
window.geometry("500x500")
input_label = tk.Label(window, text="Input")
input_label.pack()
input_entry = tk.Entry(window)
input_entry.pack()
output_label = tk.Label(window, text="Output")
output_label.pack()
output_text = tk.Text(window, height=10)
output_text.pack()
send_button = tk.Button(window, text="Send", command=send)
send_button.pack()
window.mainloop()
```
运行以上代码,可视化界面会弹出,你可以在输入框内输入问题,AI会回答你的问题。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)