File size: 7,064 Bytes
c6d338c
 
 
 
be6f31c
c6d338c
e770a74
c389ccc
be6f31c
8810277
 
ba901f8
c389ccc
8136881
8810277
 
 
 
 
 
 
 
 
b0d300a
 
98a109e
8136881
 
32d232b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8810277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d338c
 
c389ccc
63e79cc
e770a74
c389ccc
ba901f8
 
c6d338c
 
2b4c283
c6d338c
 
 
 
 
 
 
 
c389ccc
c6d338c
 
 
 
2b4c283
c6d338c
c389ccc
c6d338c
 
 
2b4c283
c6d338c
b0d300a
c6d338c
 
 
 
0d55d70
66d7cd7
 
 
0d55d70
 
c389ccc
66eca30
b49a705
d6721be
0d55d70
c6d338c
 
2c75054
 
 
5faf792
b0d300a
c6d338c
c389ccc
 
 
 
b0d300a
98a109e
c6d338c
c389ccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d338c
c389ccc
 
c6d338c
c389ccc
c6d338c
 
 
49b2ff0
e770a74
c389ccc
e770a74
49b2ff0
 
 
 
 
 
 
c389ccc
c6d338c
8810277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c389ccc
8810277
 
 
 
 
 
 
 
80d2e5c
c389ccc
 
 
 
8810277
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import json
import requests
from mtranslate import translate
from prompts import PROMPT_LIST
import streamlit as st
import random
import fasttext
import SessionState

headers = {}

LOGO = "huggingwayang.png"

MODELS = {
    "GPT-2 Small": {
        "url": "https://api-inference.huggingface.co/models/flax-community/gpt2-small-indonesian"
    },
    "GPT-2 Medium": {
        "url": "https://api-inference.huggingface.co/models/flax-community/gpt2-medium-indonesian"
    },
    "GPT-2 Small finetuned on Indonesian academic journals": {
        "url": "https://api-inference.huggingface.co/models/Galuh/id-journal-gpt2"
    },
    "GPT-2 Medium finetuned on Indonesian stories": {
        "url": "https://api-inference.huggingface.co/models/cahya/gpt2-medium-indonesian-story"
    },
}

def get_image(text: str):
    url = "https://wikisearch.uncool.ai/get_image/"
    try:
        payload = {
            "text": text,
            "image_width": 400
        }
        data = json.dumps(payload)
        response = requests.request("POST", url, headers=headers, data=data)
        print(response.content)
        image = json.loads(response.content.decode("utf-8"))["url"]
    except:
        image = ""
    return image

def query(payload, model_name):
    data = json.dumps(payload)
    # print("model url:", MODELS[model_name]["url"])
    response = requests.request("POST", MODELS[model_name]["url"], headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))

def process(text: str,
            model_name: str,
            max_len: int,
            temp: float,
            top_k: int,
            top_p: float):

    payload = {
        "inputs": text,
        "parameters": {
            "max_new_tokens": max_len,
            "top_k": top_k,
            "top_p": top_p,
            "temperature": temp,
            "repetition_penalty": 2.0,
        },
        "options": {
            "use_cache": True,
        }
    }
    return query(payload, model_name)

st.set_page_config(page_title="Indonesian GPT-2 Demo")

st.title("Indonesian GPT-2")

ft_model = fasttext.load_model('lid.176.ftz')

# Sidebar
st.sidebar.image(LOGO)
st.sidebar.subheader("Configurable parameters")

max_len = st.sidebar.number_input(
    "Maximum length",
    value=100,
    help="The maximum length of the sequence to be generated."
)

temp = st.sidebar.slider(
    "Temperature",
    value=1.0,
    min_value=0.0,
    max_value=100.0,
    help="The value used to module the next token probabilities."
)

top_k = st.sidebar.number_input(
    "Top k",
    value=50,
    help="The number of highest probability vocabulary tokens to keep for top-k-filtering."
)

top_p = st.sidebar.number_input(
    "Top p",
    value=0.95,
    help=" If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation."
)

st.markdown(
    """
    This demo uses the [small](https://huggingface.co/flax-community/gpt2-small-indonesian) and 
    [medium](https://huggingface.co/flax-community/gpt2-medium-indonesian) Indonesian GPT2 model 
    trained on the Indonesian [Oscar](https://huggingface.co/datasets/oscar), [MC4](https://huggingface.co/datasets/mc4) 
    and [Wikipedia](https://huggingface.co/datasets/wikipedia) dataset. We created it as part of the 
    [Huggingface JAX/Flax event](https://discuss.huggingface.co/t/open-to-the-community-community-week-using-jax-flax-for-nlp-cv/).

    The demo supports "multi language" ;-), feel free to try a prompt on your language. We are also experimenting with 
    the sentence based image search using Wikipedia passages encoded with distillbert, and search the encoded sentence
    in the encoded passages using Facebook's Faiss (disabled temporary).
    """
)

model_name = st.selectbox('Model',([
    'GPT-2 Small',
    'GPT-2 Medium',
    'GPT-2 Small finetuned on Indonesian academic journals',
    'GPT-2 Medium finetuned on Indonesian stories']))

if model_name in ["GPT-2 Small", "GPT-2 Medium"]:
    prompt_group_name = "GPT-2"
elif model_name in ["GPT-2 Small finetuned on Indonesian academic journals"]:
    prompt_group_name = "Indonesian Journals"
elif model_name in ["GPT-2 Medium finetuned on Indonesian stories"]:
    prompt_group_name = "Indonesian Stories"

session_state = SessionState.get(prompt=None, prompt_box=None, text=None)

ALL_PROMPTS = list(PROMPT_LIST[prompt_group_name].keys())+["Custom"]
prompt = st.selectbox('Prompt', ALL_PROMPTS, index=len(ALL_PROMPTS)-1)

# Update prompt
if session_state.prompt is None:
    session_state.prompt = prompt
elif session_state.prompt is not None and (prompt != session_state.prompt):
    session_state.prompt = prompt
    session_state.prompt_box = None
    session_state.text = None
else:
    session_state.prompt = prompt

# Update prompt box
if session_state.prompt == "Custom":
    session_state.prompt_box = "Enter your text here"
else:
    if session_state.prompt is not None and session_state.prompt_box is None:
        session_state.prompt_box = random.choice(PROMPT_LIST[prompt_group_name][session_state.prompt])

session_state.text = st.text_area("Enter text", session_state.prompt_box)

if st.button("Run"):
    with st.spinner(text="Getting results..."):
        if model_name in ["GPT-2 Medium finetuned on Indonesian stories"]:
            lang = "id"
            text = session_state.text
        else:
            lang_predictions, lang_probability = ft_model.predict(session_state.text.replace("\n", " "), k=3)
            if "__label__id" in lang_predictions:
                lang = "id"
                text = session_state.text
            else:
                lang = lang_predictions[0].replace("__label__", "")
                text = translate(session_state.text, "id", lang)

        st.subheader("Result")
        result = process(text=text,
                         model_name=model_name,
                         max_len=int(max_len),
                         temp=temp,
                         top_k=int(top_k),
                         top_p=float(top_p))

        # print("result:", result)
        if "error" in result:
            if type(result["error"]) is str:
                st.write(f'{result["error"]}.', end=" ")
                if "estimated_time" in result:
                    st.write(f'Please try it again in about {result["estimated_time"]:.0f} seconds')
            else:
                if type(result["error"]) is list:
                    for error in result["error"]:
                        st.write(f'{error}')
        else:
            result = result[0]["generated_text"]
            st.write(result.replace("\n", "  \n"))
            st.text("Translation")
            translation = translate(result, "en", "id")
            if lang == "id":
                st.write(translation.replace("\n", "  \n"))
            else:
                st.write(translate(result, lang, "id").replace("\n", "  \n"))


        # Reset state
        session_state.prompt = None
        session_state.prompt_box = None
        session_state.text = None