File size: 2,035 Bytes
a26c6e8
 
 
 
 
 
 
 
 
 
 
 
20cb92b
a26c6e8
 
 
 
 
20cb92b
 
 
a26c6e8
 
 
 
 
04e7e8d
a26c6e8
36dd12f
ffd31f3
 
a26c6e8
04e7e8d
 
20cb92b
 
 
 
 
a26c6e8
8529353
20cb92b
 
 
 
 
 
 
 
 
 
 
ffd31f3
 
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
import transformers
import streamlit as st

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("VietAI/gpt-neo-1.3B-vietnamese-news")
@st.cache
def load_model(model_name):
    model = AutoModelForCausalLM.from_pretrained(model_name)
    return model

model = load_model("VietAI/gpt-neo-1.3B-vietnamese-news")
def infer(input_ids, max_length):

    output_sequences = model.generate(
        input_ids=input_ids,
        max_length=max_length,
        do_sample=True,
        temperature=0.9,
        top_k=20,
        #top_p=top_p,
        #num_return_sequences=1
    )

    return output_sequences

default_value = "Tiềm năng của trí tuệ nhân tạo"

st.title("Vietnamese Text Generation With Transformers")
st.write("This app generates Vietnamese text based on a given prompt. To change the parameters of the generated text, adjust the slider on the left and click Generate Text again.")
st.write("It might be a bit slow after you change the generated text length. Be patient!")

sent = st.text_area("Text", default_value, height = 275)
max_length = st.sidebar.slider("Max Length", min_value = 10, max_value=500)

# We don't really need these params. It's a lot slower.
# temperature = st.sidebar.slider("Temperature", value = 1.0, min_value = 0.0, max_value=1.0, step=0.05)
# top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 0)
# top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)

if st.button("Generate Text"):
    with st.spinner("Working Hard..."):
        encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
        if encoded_prompt.size()[-1] == 0:
            input_ids = None
        else:
            input_ids = encoded_prompt

        gen_tokens = infer(encoded_prompt, max_length)
        gen_text = tokenizer.batch_decode(gen_tokens)[0]
        st.write(gen_text)
    st.success("Done!")

st.write("For feedback/requests, write to mel.nguyen273@gmail.com.")