2023-03-31 03:06:26 +00:00
|
|
|
|
import gradio as gr
|
|
|
|
|
from transformers import pipeline, set_seed
|
|
|
|
|
|
2023-04-04 09:16:23 +00:00
|
|
|
|
|
2023-03-31 03:06:26 +00:00
|
|
|
|
def inference(text):
|
|
|
|
|
model_path = "distilgpt2"
|
|
|
|
|
generator = pipeline('text-generation', model=model_path)
|
|
|
|
|
set_seed(42)
|
|
|
|
|
|
2023-04-04 09:16:23 +00:00
|
|
|
|
output = []
|
|
|
|
|
lst = generator(text, max_length=20, num_return_sequences=5)
|
2023-03-31 03:06:26 +00:00
|
|
|
|
for dic in lst:
|
|
|
|
|
output.append(dic['generated_text'])
|
|
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
2023-04-04 09:16:23 +00:00
|
|
|
|
examples = [["Hello, I’m a language model."]]
|
2023-03-31 03:06:26 +00:00
|
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
|
gr.Markdown(
|
2023-04-04 09:16:23 +00:00
|
|
|
|
"""
|
2023-03-31 03:06:26 +00:00
|
|
|
|
# Text generation:distilgpt2
|
2023-04-06 03:21:09 +00:00
|
|
|
|
DistilGPT2(Distilled-GPT2)是一种英语模型,在GPT-2的监督下进行预训练。与 GPT-2 一样,DistilGPT2 可用于生成文本。
|
2023-04-06 08:29:13 +00:00
|
|
|
|
这是一个distilgpt2的Gradio Demo。 输入你想要的英文文本或者点击下面的示例文本来加载它。
|
2023-03-31 03:06:26 +00:00
|
|
|
|
""")
|
|
|
|
|
with gr.Row():
|
|
|
|
|
text_input = gr.Textbox()
|
|
|
|
|
text_output = gr.Textbox()
|
|
|
|
|
image_button = gr.Button("上传")
|
|
|
|
|
image_button.click(inference, inputs=text_input, outputs=text_output)
|
2023-04-04 09:16:23 +00:00
|
|
|
|
gr.Examples(examples, inputs=text_input)
|
2023-03-31 03:06:26 +00:00
|
|
|
|
|
2023-04-06 02:54:59 +00:00
|
|
|
|
demo.launch(server_name="0.0.0.0")
|