Spaces:
Runtime error
Runtime error
improved demo
Browse files
app.py
CHANGED
@@ -12,52 +12,78 @@ img {
|
|
12 |
th {
|
13 |
text-align: left!important
|
14 |
}
|
|
|
|
|
|
|
|
|
15 |
</style>
|
16 |
"""
|
|
|
17 |
MASK_TOKEN = "<mask>"
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
df = df.style.set_properties(subset=["sequence", "score"], **{"text-align": "left"})
|
24 |
st.table(df)
|
25 |
|
26 |
|
27 |
-
def
|
28 |
st.markdown(STYLE, unsafe_allow_html=True)
|
29 |
-
st.title("Indonesian RoBERTa Base")
|
30 |
-
|
31 |
-
mask_api = InferenceApi("flax-community/indonesian-roberta-base")
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if len(user_input) > 0:
|
37 |
try:
|
38 |
user_input.index(MASK_TOKEN)
|
39 |
except ValueError:
|
40 |
-
st.error(
|
|
|
|
|
41 |
else:
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
for sequence in df["sequence"].values:
|
51 |
-
emot_output = emot_pipeline(sequence)
|
52 |
|
|
|
|
|
|
|
|
|
53 |
result_dict = {"sequence": sequence}
|
54 |
-
result_dict.update(
|
55 |
-
|
56 |
|
57 |
-
|
|
|
58 |
subset=["sequence", "label", "score"], **{"text-align": "left"}
|
59 |
)
|
60 |
-
|
61 |
|
62 |
|
63 |
main()
|
|
|
12 |
th {
|
13 |
text-align: left!important
|
14 |
}
|
15 |
+
|
16 |
+
td {
|
17 |
+
font-size:
|
18 |
+
}
|
19 |
</style>
|
20 |
"""
|
21 |
+
|
22 |
MASK_TOKEN = "<mask>"
|
23 |
|
24 |
+
EMOTION_MAP = {
|
25 |
+
"anger": "๐ก",
|
26 |
+
"fear": "๐ฑ",
|
27 |
+
"happy": "๐",
|
28 |
+
"love": "๐",
|
29 |
+
"sadness": "๐ญ",
|
30 |
+
}
|
31 |
|
32 |
+
|
33 |
+
def display_table(df: pd.DataFrame, subheader: str):
|
34 |
+
st.subheader(subheader)
|
|
|
35 |
st.table(df)
|
36 |
|
37 |
|
38 |
+
def setup():
|
39 |
st.markdown(STYLE, unsafe_allow_html=True)
|
40 |
+
st.title("๐ฎ๐ฉ Indonesian RoBERTa Base")
|
41 |
+
|
|
|
42 |
|
43 |
+
def main():
|
44 |
+
setup()
|
45 |
+
|
46 |
+
user_input = st.text_input(
|
47 |
+
f"Insert a sentence to predict with a {MASK_TOKEN} token // Masukkan kalimat untuk diisi dengan token {MASK_TOKEN}",
|
48 |
+
value=f"Aduh... gimana nih.. hari ini {MASK_TOKEN} banget...",
|
49 |
+
)
|
50 |
+
|
51 |
+
mlm_model = "flax-community/indonesian-roberta-base"
|
52 |
+
mask_api = InferenceApi(mlm_model)
|
53 |
+
|
54 |
+
sa_model = "StevenLimcorn/indonesian-roberta-base-emotion-classifier"
|
55 |
+
sa_pipeline = pipeline("sentiment-analysis", model=sa_model, tokenizer=sa_model)
|
56 |
|
57 |
if len(user_input) > 0:
|
58 |
try:
|
59 |
user_input.index(MASK_TOKEN)
|
60 |
except ValueError:
|
61 |
+
st.error(
|
62 |
+
f"Please enter a sentence with the correct {MASK_TOKEN} token // Harap masukkan kalimat dengan token {MASK_TOKEN} yang benar"
|
63 |
+
)
|
64 |
else:
|
65 |
+
# render masked language modeling table
|
66 |
+
mlm_result = mask_api(inputs=user_input)
|
67 |
+
mlm_df = pd.DataFrame(mlm_result)
|
68 |
+
mlm_df.drop(columns=["token", "token_str"], inplace=True)
|
69 |
+
mlm_df_styled = mlm_df.style.set_properties(
|
70 |
+
subset=["sequence", "score"], **{"text-align": "left"}
|
71 |
+
)
|
72 |
+
display_table(mlm_df_styled, "๐ Top 5 Predictions")
|
|
|
|
|
73 |
|
74 |
+
# render sentiment analysis table
|
75 |
+
sa_df = pd.DataFrame(columns=["sequence", "label", "score"])
|
76 |
+
for sequence in mlm_df["sequence"].values:
|
77 |
+
sa_output = sa_pipeline(sequence) # predict for every mlm output
|
78 |
result_dict = {"sequence": sequence}
|
79 |
+
result_dict.update(sa_output[0])
|
80 |
+
sa_df = sa_df.append(result_dict, ignore_index=True)
|
81 |
|
82 |
+
sa_df["label"] = sa_df["label"].apply(lambda x: x + " " + EMOTION_MAP[x])
|
83 |
+
sa_df_styled = sa_df.style.set_properties(
|
84 |
subset=["sequence", "label", "score"], **{"text-align": "left"}
|
85 |
)
|
86 |
+
display_table(sa_df_styled, "๐ค By saying that, I guess you are feeling..")
|
87 |
|
88 |
|
89 |
main()
|