File size: 1,034 Bytes
e8483c0
e99bad6
 
 
 
 
 
8f99873
 
b8a124d
 
 
 
 
 
 
 
 
 
 
 
 
e99bad6
 
 
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
import re
import gradio as gr
from transformers import pipeline

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"]
    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 = "\n".join(result)
    return result

output_text = gr.outputs.Textbox()
gr.Interface(fn=generate_text, inputs="textbox", outputs=output_text).launch(share=True)