File size: 7,294 Bytes
2c2081e
de6562c
2c2081e
 
 
 
 
 
 
 
 
 
de6562c
2c2081e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9e51b3
2c2081e
 
 
 
 
 
de6562c
d9e51b3
 
2c2081e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9d6157
 
2c2081e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9d6157
 
2c2081e
 
 
 
 
 
 
 
 
 
 
 
 
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""Prep front cover for sidebar (based on st-bumblebee-st_app.py)."""
# pylint: disable=abstract-class-instantiated
import base64
from io import BytesIO
from textwrap import dedent

import logzero
import pandas as pd
import streamlit as st
from logzero import logger
from set_loglevel import set_loglevel

from st_mlbee import __version__

logzero.loglevel(set_loglevel())

msg = dedent(
    """
    What would you like to do?
    The following alignment engines are available.

    **UFast-Engine**: ultra-fast, based on a home-brewed algorithm, faster than blazing fast but can only process en-zh para/sent pairs, not as sophisticated as DL-Engine;

    **SFast-Engine**: super-fast, based on machine translation;

    **Fast-Engine**: based on yet another home-brewed algorithm, blazing fast but can only process en-zh para/sent pairs;

    **DL-Engin**: based on machine learning, multilingual, one para/sent takes about 1s.
    """
).strip()
msg = dedent(
    """
    mlbee: A multilingual (50+ languages) dualtext
    aligner based on machine learning

    It takes about 1-2 s to process a pair of blocks (be it
    sents, paras of docus).
    Extremely long blocks will likely have a negative impact
    on aligning.
    On a powerful computer such as an instance on huggingface spaces, the running time can be reduced by a factor of 10-20.

    st-mlbee 0.1.0a2 is faster and handles more languages. Head to https://ko-fi.com/mlbee if you would like to buy me a coffee or two:).
    """
).strip()


def sb_front_cover():
    """Prep front cover for sidebar."""
    st.sidebar.markdown(f"### mlbee {__version__} ")

    _ = "More info (click to toggle)"
    sb_tit_expander = st.sidebar.expander(_, expanded=False)
    with sb_tit_expander:
        st.markdown(msg)


intructins = dedent(
    f"""
    *   Set up options in the left sidebar

    *   Click expanders / +: to reveal more details; -: to hide them

    *   Press '**Click to start aligning**' to get
    the ball rolling. (The button will appear when
    everything is ready.)

    *   mlbee v.{__version__} from mu@qq41947782's
    keyboard in cyberspace. Join **qq group 316287378**
    for feedback and questions or to be kept updated.
    mlbee is a member of the bee family.
    """
).strip()


def instructions():
    """Prep msg."""
    logger.debug("instructions entry")
    back_cover_expander = st.expander("Instructions")
    with back_cover_expander:
        st.markdown(intructins)

    logger.debug("instructions exit")


about_msg = dedent(
    f"""
    # mlbee {__version__}

    https://bumblebee.freeforums.net/thread/6/mlbee
    or head to 桃花元 (qq group 316287378)
    """
).strip()

menu_items = {
    "Get Help": "https://bumblebee.freeforums.net/thread/6/mlbee",
    "Report a bug": "https://github.com/ffreemt/mlbee/issues",
    "About": about_msg,
}

style_css = """
.row-widget.stTextInput > div:first-of-type {
    background: #fff;
    display: flex;
    border: 1px solid #dfe1e5;
    box-shadow: none;
    border-radius: 24px;
    height: 50px;
    width: auto;
    margin: 10px auto 30px;
}

.row-widget.stTextInput > div:first-of-type:hover,
.row-widget.stTextInput > div:first-of-type:focus {
    box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, 0.2);
}

.row-widget.stTextInput .st-bq {
    background-color: #fff;
}

.row-widget.stTextInput > label {
    color: #b3b3b3;
}

.row-widget.stButton > button {
    border-radius: 24px;
    background-color: #B6C9B1;
    color: #fff;
    border: none;
    padding: 6px 20px;
    float: right;
    background-image: none;
}

.row-widget.stButton > button:hover {
    box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, 0.2);
}

.row-widget.stButton > button:focus {
    border: none;
    color: #fff;
}

.footer-custom {
    position: fixed;
    bottom: 0;
    width: 100%;
    color: var(--text-color);
    max-width: 698px;
    font-size: 14px;
    height: 50px;
    padding: 10px 0;
    z-index: 50;
}

.main {
    padding: 20px;
}

footer {
    display: none !important;
}

.footer-custom a {
    color: var(--text-color);
}

#wikipedia-assistant {
    font-size: 36px;
}

.generated-answer p {
    font-size: 16px;
    font-weight: bold;
}

.react-json-view {
    margin: 40px 0 80px;
}

.tooltip {
    text-align: center;
    line-height: 20px;
    display: table-caption;
    font-size: 10px;
    border-radius: 50%;
    height: 20px;
    width: 20px;
    position: relative;
    cursor: pointer;
    color:#000;
}

.tooltip .tooltiptext {
    visibility: hidden;
    width: 280px;
    text-align: center;
    border-radius: 6px;
    padding: 10px;
    position: absolute;
    z-index: 1;
    top: 25px;
    left: 50%;
    margin-left: -140px;
    font-size: 14px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.16);
    color: #000;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}

.sentence-wrapper {
    border-left: 4px solid #ffc423;
    padding-left: 20px;
    margin-bottom: 40px;
}

#context {
    padding: 2rem 0 1rem;
}

hr {
    margin: 2em 0 1em;
}

.technical-details-info {
    margin-bottom: 100px;
}

.loader-wrapper {
    display: flex;
    align-items: center;
    background-color: rgba(250, 202, 43, 0.2);
    padding: 15px 20px;
    border-radius: 6px;
}

.loader-wrapper p {
    margin-bottom: 0;
    margin-left: 20px;
}

.loader {
    width: 30px;
    height: 30px;
    border: dotted 5px #868686;
    border-radius: 100%;
    animation: spin 1s linear infinite;
}

.loader-note {
    font-size: 14px;
    color: #b3b3b3;
    margin-left: 5px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg) scale(0.8);
    border-top-color: transparent;
    border-right-color: transparent;
  }
  50% { transform: rotate(180deg) scale(1.2);
    border-color: #949494;
    border-top-color: transparent;
    border-right-color: transparent;
  }
  100% { transform: rotate(360deg) scale(0.8);
    border-color: #bbbbbb;
    border-top-color: transparent;
    border-right-color: transparent;
  }
}
"""


def to_excel(df):
    """Convert df to excel.

    ref. st-bumblebee st_app.py
    """
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine="xlsxwriter")
    df.to_excel(writer, sheet_name="Sheet1")
    writer.save()
    processed_data = output.getvalue()
    return processed_data


def get_table_download_link(df):
    """
    Generate a link.

    Allowing the data in a given panda dataframe
    to be downloaded.

    Args:
        df: pandas.dataframe

    Returns:
        href string
    """
    val = to_excel(df)
    b64 = base64.b64encode(val)  # val looks like b'...'
    return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="aligned_paras.xlsx">Download aligned paras xlsx file</a>'  # decode b'abc' => abc


def get_table_download_link_sents(df):
    """
    Generate a link.

    Allowing the data in a given panda dataframe to be
    downloaded for sents aligned.

    Args:
        df: pandas.dataframe

    Returns:
        href string
    """
    val = to_excel(df)
    b64 = base64.b64encode(val)  # val looks like b'...'
    return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="aligned_sents.xlsx">Download aligned sents xlsx file</a>'  # decode b'abc' => abc  # noqa