import gradio as gr from transformers import pipeline import re model_checkpoint = "erwanlc/t5-cocktails_recipe-base" tt_generator = pipeline("text2text-generation", model=model_checkpoint) def generate_text(ingredients): result = tt_generator(ingredients, min_length=20, max_length=1024, do_sample=True, temperature=1.0, top_p=1.0) result = result[0]["generated_text"] result_temp = result all_matches = re.findall(r"([A-z][.])", result) for matches in all_matches: result = result.replace(matches, f"{matches}\n") result = result.split("\n") all_matches = re.finditer(r"([0-9]*[.])?[0-9]+ ", result[-1]) all_matches = list(all_matches) all_matches = set([item.group() for item in all_matches]) for matches in all_matches: result[-1] = result[-1].replace(matches, f"\n{matches}") result = [item.strip() for item in result] result[-1] = f"\nIngredients:\n{result[-1]}" result[-1] = re.sub(" (?=[A-Z])", "\n", result[-1]) result = "\n".join(result) result = re.sub("(?<=[0-9].)\n(?=[0-9])", "", result) print(result_temp) return result title = "Create original cocktails based on your available ingredients" description = "Finetuned T5 on cocktails recipe. Write your ingredients separated by a comma to generate a cocktail. This work was inspired by Chef Transformer (https://huggingface.co/spaces/flax-community/chef-transformer)." examples = [ ["rum,apricot liqueur,lime juice,sugar syrup"], ["gin,honey,basil leaves,soda water"] ] output_text = gr.outputs.Textbox() gr.Interface( fn=generate_text, inputs="textbox", outputs=output_text, title=title, description=description, examples=examples, theme="huggingface", ).launch(share=True)