File size: 1,765 Bytes
e99bad6
 
e899a1e
e99bad6
 
 
 
8f99873
 
b8a124d
8696982
b8a124d
 
 
 
 
 
 
 
 
 
8f16cd9
8696982
b8a124d
8696982
 
b8a124d
e99bad6
e0bbd43
e899a1e
 
 
8696982
e899a1e
 
e99bad6
e899a1e
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)