Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import AutoTokenizer | |
from transformers import GenerationConfig | |
from transformers import AutoModelForSeq2SeqLM | |
tokenizer = AutoTokenizer.from_pretrained("t5-small") | |
headline = AutoModelForSeq2SeqLM.from_pretrained("wetey/content-summarizer") | |
generate_long = AutoModelForSeq2SeqLM.from_pretrained("wetey/content-generator") | |
def generate_headline(text): | |
prefix = "summarize " | |
input = prefix + text | |
inputs = tokenizer(input, return_tensors = "pt", max_length = 128, truncation = True).input_ids | |
generation_config = GenerationConfig(temperature = 1.2, | |
encoder_no_repeat_ngram_size = 4, | |
min_length = 4) | |
outputs = headline.generate(inputs, do_sample = True, generation_config = generation_config) | |
return tokenizer.decode(outputs[0], skip_special_tokens = True) | |
def generate_content(text): | |
prefix = "generate_longer_text_from_headline: " | |
input = prefix + text | |
inputs = tokenizer(input, return_tensors="pt", max_length = 128, truncation = True).input_ids | |
generation_config = GenerationConfig(temperature = 1.2, | |
encoder_no_repeat_ngram_size = 2, | |
min_length = 50, | |
max_length = 512, | |
length_penalty = 1.5, | |
num_beams = 4, | |
repetition_penalty = 1.5, | |
no_repeat_ngram_size = 3) | |
outputs = generate_long.generate(inputs, do_sample = True, generation_config = generation_config) | |
return tokenizer.decode(outputs[0], skip_special_tokens = True) | |
demo = gr.Blocks() | |
with demo: | |
gr.Markdown( | |
''' | |
This app can generate a headline for a given paragraph or a paraghraph from a headline.<br> | |
<b>To use</b>:<br>Paste text into input text box.<br> | |
To generate a headline, click on "Generate headline", to generate a short paragraph, click on "Generate short Paragraph"<br> | |
<b>Reference</b>:<br> | |
Headline = "Introducing droidlet, a one-stop shop for modularly building intelligent agents"<br> | |
Content = "Droidlet is a platform for building intelligent agents that simplifies integrating a wide range of machine learning algorithms to facilitate rapid prototyping. It can be used to quickly test out different algorithms with a robot. Droidlet can help researchers easily build agents that can complete complex tasks in the real world or in simulated environments. This article introduces Droidlet and explains how the platform works."<br> | |
<b>Example - Headline generation:</b><br> | |
Input = "Droidlet is a platform for building intelligent agents that simplifies integrating a wide range of machine learning algorithms to facilitate rapid prototyping. It can be used to quickly test out different algorithms with a robot. Droidlet can help researchers easily build agents that can complete complex tasks in the real world or in simulated environments. This article introduces Droidlet and explains how the platform works."<br> | |
Output from model = "Droidlet helps researchers develop robot-like agents with automated physics"<br> | |
<b>Example - short paraghraph generation:</b><br> | |
Input = "Introducing droidlet, a one-stop shop for modularly building intelligent agents"<br> | |
Output from model = "The team behind the project has partnered with Google to design and build smart agents. The company is looking to expand its presence over time. It will allow companies to build real-world agents in many different ways, such as building robots, creating artificial intelligence, and more. The new product is available for purchase in the UK. In addition, the company has announced that it will be able to build one of the most effective agents in the world.<br> | |
''' | |
) | |
text_input = gr.Textbox(label = "Input") | |
text_output = gr.Textbox(label = "Output") | |
b1 = gr.Button("Generate headline") | |
b2 = gr.Button("Generate long content") | |
b1.click(generate_headline, inputs=text_input, outputs=text_output) | |
b2.click(generate_content, inputs=text_input, outputs=text_output) | |
demo.launch() |