File size: 3,746 Bytes
f2dd44e
 
94fcac2
 
 
 
f2dd44e
2650a26
bc8bbd5
54855d5
747c2ab
29f0a11
39b3282
f2dd44e
28e94e4
3006a36
bddf5c1
 
 
 
ef3161d
bc8bbd5
222b9bb
f2dd44e
60d8e03
457ba36
94fcac2
f2dd44e
d80396f
f2dd44e
 
747c2ab
 
 
 
 
 
f2dd44e
39b3282
 
bc8bbd5
31198fb
1983292
31198fb
 
f2dd44e
 
2650a26
 
f2dd44e
d80396f
bace2bf
bc8bbd5
282cd32
0a6c516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3006a36
 
dc8fe56
457ba36
bc8bbd5
3552908
3006a36
 
f2dd44e
747c2ab
ef3161d
747c2ab
ef3161d
747c2ab
773517f
 
747c2ab
 
94fcac2
f2dd44e
 
54855d5
747c2ab
f2dd44e
 
 
5a2ec90
 
 
 
f2dd44e
05df558
5a2ec90
 
 
 
f2dd44e
0a6c516
bc8bbd5
0a6c516
7a10d0b
 
05df558
fd0bac7
2650a26
 
 
eed1def
60d8e03
2650a26
f2dd44e
60d8e03
f2dd44e
60d8e03
f2dd44e
 
54855d5
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
"""Bootstrap."""
# pylint: disable=invalid-name
from typing import Any, List, Union

import gradio as gr
import logzero
import numpy as np
import pandas as pd
# from hf_model_s import model_s
from logzero import logger
from set_loglevel import set_loglevel

from gradio_cmat.gradio_cmat import gradio_cmat


# pd.set_option("display.precision", 3)

pd.options.display.float_format = "{:,.2f}".format
# pd.set_option('display.float_format', '{:,.0f}'.format)

logzero.loglevel(set_loglevel(10, force=True))
# model = model_s()


# def fn(text1: str, text2: str) -> np.ndarray:
# def fn(text1: str, text2: str) -> pd.DataFrame:
def fn(text1: str, text2: str) -> Union[List[Any], str]:
    """Define."""
    logger.info("gradio version: %s", gr.__version__)
    list1 = [elm.strip() for elm in text1.splitlines() if elm.strip()]
    list2 = [elm.strip() for elm in text2.splitlines() if elm.strip()]

    logger.debug("text1[:10]: %s", text1[:10])
    logger.debug("text2[:10]: %s", text2[:10])
    logger.info("info text1[:10]: %s", text1[:10])
    logger.info("info text2[:10]: %s", text2[:10])

    try:
        res = gradio_cmat(list1, list2)
        # res = np.array([[0.015, 0.235, 0.112], [0.015, 0.235, 0.112]])

        logger.debug("res: \n%s, %s", res, res.shape)
        logger.debug("type(res): %s", type(res))
        # res.round(decimals=2, out=res)
        # logger.debug("debug res: %s, %s", res, res.shape)
    except Exception as e:
        logger.error("gradio_cmat error: %s", e)
        return str(e)
        # raise

    round2 = lambda x: round(x, 2)
    vfunc = np.vectorize(round2)

    df = pd.DataFrame(res)
    # _ = df.style.background_gradient().set_precision(1)
    # _ = df.style.background_gradient().format(precision=1)

    _ = df.style.set_properties(
            **{
                "font-size": "10pt",
                "border-color": "black",
                "border": "1px black solid !important"
            }
            # border-color="black",
        ).set_table_styles([{
            "selector": "",  # noqs
            "props": [("border", "2px black solid !important")]}]  # noqs
        ).format(
            precision=2
        )

    _ = _.to_html()

    # return pd.DataFrame(vfunc(res), dtype="object")
    # return pd.DataFrame(vfunc(res), dtype="object").to_html()
    # return pd.DataFrame(res)
    # return str(res.tolist())

    return _


out_df = gr.outputs.Dataframe(
    # headers=None,
    max_rows=50,  # 20
    max_cols=50,
    overflow_row_behaviour="paginate",
    # type="auto",
    type="pandas",
    label="cmat",
)
out_text = gr.outputs.Textbox(label="cmat")

# _ = """
try:
    interface = gr.Interface(
        fn,
        [
            gr.inputs.Textbox(
                lines=3, default="""The quick brown fox jumped over the lazy dogs.
                    test test
                    测试一下
                """
            ),
            gr.inputs.Textbox(lines=4, default="""The fast brown fox jumps over lazy dogs.
                    abc
                    test
                    Dies ist ein Test
            """),
        ],
        # out_df,
        # out_text,
        "html",  # gr.outputs.HTML(label=None)
        title="gradio-cmat",
        theme="grass",
        allow_flagging="never",
        layout="vertical",
        description="Gen correlation matrix for multlingual texts",
        article="Click 'Clear' first for subsequent new texts",
        examples=[
            ["test\nabc", "测试"],
            ["This is a text.\nIch liebe Dich.\nabc", "我爱你\nI love you.\n测试\nbcd"],
        ],
    )
except Exception as exc:
    logger.exception("")
    logger.error("gr.Interface.load(%s): %s", "fn", exc)
    raise

interface.launch()