File size: 832 Bytes
a4ae49d
f99ec9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3134c2
f99ec9c
 
 
a4ae49d
 
f99ec9c
a4ae49d
f99ec9c
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 = "Dominic0406/convai2_gpt2"
pipe = pipeline("text-generation", model=model_name)

def generate_response(dialog):
    # 对话是一个字符串,使用分隔符分割多轮对话
    dialog = dialog.split("\n")
    input_text = " ".join(dialog)
    response = pipe(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
    return response

# 创建Gradio接口
iface = gr.Interface(
    fn=generate_response,
    inputs=gr.Textbox(lines=7, placeholder="Enter your dialog here..."),
    outputs="text",
    title="GPT-2 Conversational AI",
    description="Enter your dialog, one message per line. The model will generate a response based on the entire conversation."
)

# 启动接口
if __name__ == "__main__":
    iface.launch()