FlaxBart for Conditional Generation

in

So, imagine you have a piece of text and you want to add more information to it. You could use FlaxBart to do this by providing the original text as your “condition” (or prompt) and then letting the tool generate new content that fits within that context. Here’s an example:

Let’s say you have a news article about a recent earthquake in California, but it doesn’t include any information about how many people were affected by the disaster. You could use FlaxBart to add this missing detail by providing the original text as your prompt and then specifying that you want it to generate a sentence that includes the number of casualties:

# Import necessary libraries
from flax_bart import FLINCH, generate # Importing FLINCH and generate functions from flax_bart library
import re # Importing regular expression library for string matching

# Load pre-trained model from Hugging Face Hub
model = FLINCH.load("google/flan-t5") # Loading pre-trained model from Hugging Face Hub and assigning it to variable 'model'

# Define prompt and desired output format (in this case, a sentence with the number of casualties)
prompt = "A 6.0 magnitude earthquake struck California on Monday, causing widespread damage but no fatalities." # Assigning the prompt text to variable 'prompt'
output_format = r"The disaster left [number] people dead and [number] injured." # Assigning the desired output format to variable 'output_format'

# Use generate() function to add missing information (in this case, the number of casualties)
match = re.search(r'(\d+)\sdead\sand\s(\d+)\sinjured', output_format) # Using regular expression to match the numbers in the output format and assigning it to variable 'match'
numbers = list(map(int, match.groups())) # Converting the matched numbers to integers and storing them in a list called 'numbers'
casualties = generate(model, prompt + ".", max_length=1024, num_beams=5, stop_str=".") # Using the generate function to add missing information (number of casualties) to the prompt text and storing it in a variable 'casualties'
for i in range(len(casualties)): # Looping through the generated sentences
    if re.search("dead", casualties[i]): # Checking if the sentence contains the word 'dead'
        numbers[0] += int(re.findall("\d+", casualties[i])[0]) # Extracting the number from the sentence and adding it to the first element of the 'numbers' list
    elif re.search("injured", casualties[i]): # Checking if the sentence contains the word 'injured'
        numbers[1] += int(re.findall("\d+", casualties[i])[0]) # Extracting the number from the sentence and adding it to the second element of the 'numbers' list
output = prompt + "\n" + output_format.replace("[number]", str(numbers[0])) + "\n" + output_format.replace("[number]", str(numbers[1])) # Creating the final output by replacing the placeholder [number] with the actual numbers from the 'numbers' list and adding it to the prompt text

In this example, we’re using the `generate()` function from FlaxBart to add information about the number of casualties and injuries caused by the earthquake. We first define our prompt (which is simply the original news article) and desired output format (in this case, a sentence that includes the numbers of fatalities and injuries).

We then use regular expressions to extract any existing mentions of “dead” or “injured” from the output format string. This allows us to update these values based on the new information generated by FlaxBart. Finally, we combine our original prompt with the updated output format (which now includes the numbers of casualties and injuries) and print out the final result.

With a little bit of scripting magic, you can use tools like FlaxBart to generate new content based on your existing text. It’s like having your own personal writing assistant that can help you add more information or context to your work.

SICORPS