from transformers import pipeline, set_seed | |
import streamlit as st | |
generator = pipeline('text-generation', model='gpt2') | |
set_seed(42) | |
# 引入后台代码 | |
def backend(question): | |
# 在这里编写后台处理代码,此处为示例 | |
res = generator("Hello, I'm a language model,", max_length=30, num_return_sequences=5) | |
return res[0] | |
# 构建页面布局 | |
st.title("GPT-2 Demo") | |
question = st.text_input("请输入问题") | |
if st.button("获取答案"): | |
# 调用后台代码 | |
answer = backend(question) | |
st.write(answer) | |