File size: 2,149 Bytes
acd7000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Main App
"""

import streamlit as st
from transformers import AutoModelForSeq2SeqLM

from src.tokenizers import IndoNLGTokenizer


@st.cache(allow_output_mutation=True)
def fetch_tokenizer_model():
    """
    Fetch tokenizer and model
    """
    tokenizer = IndoNLGTokenizer.from_pretrained("indobenchmark/indobart-v2")
    model = AutoModelForSeq2SeqLM.from_pretrained("haryoaw/id-recigen-bart")
    return tokenizer, model


tokenizer, model = fetch_tokenizer_model()


def predict_recipe(food: str) -> str:
    """
    Predict Ingredients Here!

    Parameters
    ----------
    food: str
        The food that will be used

    Returns
    -------
    str
        Return the model here
    """
    inp = tokenizer(food.lower(), return_tensors="pt")["input_ids"]
    generated = model.generate(
        inp, max_length=500, do_sample=False, num_beams=10, num_beam_groups=2
    )
    returned_input: str = tokenizer.decode(generated[0], skip_special_tokens=True)
    returned_input = "\n".join([x.strip() for x in returned_input.split("||")])
    return returned_input


def create_frontend() -> None:
    """
    Create front end streamlit here
    """
    st.markdown("# Food Ingredients Generator Indonesia Showcase!")
    st.write("🥑Generate your ingredients here!")
    
    with st.form("my_form"):
        food_name = st.text_input(
            "Food", value="Nasi Goreng Ayam", help="Input your food here!"
        )
        submitted = st.form_submit_button("Submit")
        if submitted:
            predicted = predict_recipe(food_name)
            st.markdown(f"## Bahan ( Ingredients ) `{food_name}`:")
            st.text(predicted)
    st.markdown("## Additional Note")
    st.write(
        "❗Please note that the model is trained with the food that use:"
    )
    for i, ingr in enumerate(("ayam", "tempe", "ikan", "kambing", "telur", "tahu", "sapi")):
        st.write(f"{i+1}. {ingr}")
    
    st.markdown("## Models")
    st.markdown(
        "🤗 Huggingface Model: [Link](https://huggingface.co/haryoaw/id-recigen-bart)"
    )
    st.write("Thank you 😊")


if __name__ == "__main__":
    create_frontend()