0.0.1 / Test 1.0
VertinYi's picture
Create Test 1.0
1058f47 verified
raw
history blame contribute delete
763 Bytes
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()