File size: 763 Bytes
1058f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# 使用公开的模型进行测试
model_name = "gpt2"
pipe = pipeline("text-generation", model=model_name, tokenizer=model_name)

def chat_with_ai(prompt):
    response = pipe(prompt, max_length=100, do_sample=True)
    return response[0]["generated_text"]

with gr.Blocks() as demo:
    gr.Markdown("# 🤖 AI Chatbot")
    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Type your message:")
    clear = gr.Button("Clear")

    def respond(message, chat_history):
        response = chat_with_ai(message)
        chat_history.append((message, response))
        return "", chat_history

    msg.submit(respond, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: [], None, chatbot)

demo.launch()