gradio-cmat / app.py
freemt
28e94e4
raw
history blame
2.92 kB
"""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
logger.info("gradio version: %s", gr.__version__)
pd.set_option("display.precision", 2)
pd.options.display.float_format = "{:,.2f}".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."""
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, 1)
vfunc = np.vectorize(round2)
return vfunc(res).tolist()
# return pd.DataFrame(res)
# return str(res.tolist())
out_df = gr.outputs.Dataframe(
# headers=None,
max_rows=50, # 20
max_cols=50,
overflow_row_behaviour="paginate",
type="auto",
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,
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()