Asis
Import re library
de4e57f
raw
history blame
1.56 kB
import re
import gradio as gr
from transformers import pipeline
generator = pipeline('text-generation', model='plasticfruits/gpt2-finetuned-how-to-qa')
def clean_response(user_prompt, response):
response = re.sub("(?<=\.)[^.]*$", "", response) # finish at last sentence dot
response = (
response.replace("[WP]", "").replace(user_prompt, "").replace("[RESPONSE]", "")
)
response = response.lstrip()
return response
def generate(text):
#text = f"\n<|startoftext|>[WP] {user_prompt} \n[RESPONSE]"
result = generator(text,
max_length=350,
num_return_sequences=1,
do_sample=True,
top_k=50,
top_p=0.95)
clean_text = clean_response(text, result[0]["generated_text"])
return clean_text
examples = [
["How to draw a circle"],
["How to create a universe"],
["How to make pasta"]
]
title = "How-to Generator"
description = "Ask your 'how-to' question to get the best possible answer available in the universe.<br>For best performance, make sure to start your question with 'How too {your question}'"
article = "<p style='text-align: center'><a href='how-to-generator.herokuapp.com/' target='_blank'>Official How-To Page</a></p>"
demo = gr.Interface(
fn=generate,
inputs=gr.inputs.Textbox(lines=5, label="Input Text"),
outputs=gr.outputs.Textbox(label="Generated Text"),
examples=examples,
title=title,
description=description,
article=article
)
demo.launch()