freemt commited on
Commit
7c9bb59
1 Parent(s): 9438777

Update __main__ runs app.py 0.1.0-alpha.1 to 0.1.0-alpha.2

Browse files
gradio_queue.db DELETED
Binary file (16.4 kB)
 
pyproject.toml CHANGED
@@ -1,6 +1,6 @@
1
  [tool.poetry]
2
  name = "radio_mlbee"
3
- version = "0.1.0-alpha.1"
4
  description = "mlbee via gradio"
5
  authors = ["ffreemt"]
6
  license = "MIT"
 
1
  [tool.poetry]
2
  name = "radio_mlbee"
3
+ version = "0.1.0-alpha.2"
4
  description = "mlbee via gradio"
5
  authors = ["ffreemt"]
6
  license = "MIT"
radio_mlbee/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
  """Init."""
2
- __version__ = "0.1.0a1"
3
  from .radio_mlbee import radio_mlbee
4
 
5
  __all__ = ("radio_mlbee",)
 
1
  """Init."""
2
+ __version__ = "0.1.0a2"
3
  from .radio_mlbee import radio_mlbee
4
 
5
  __all__ = ("radio_mlbee",)
radio_mlbee/__main__.py CHANGED
@@ -1,44 +1,18 @@
1
- """Prep __main__.py."""
2
- # pylint: disable=invalid-name
3
- from pathlib import Path
4
- from typing import Optional
5
 
6
- import logzero
7
- import typer
8
- from logzero import logger
9
- from set_loglevel import set_loglevel
10
 
11
- from radio_mlbee import __version__, radio_mlbee
 
12
 
13
- logzero.loglevel(set_loglevel())
 
14
 
15
- app = typer.Typer(
16
- name="radio_mlbee",
17
- add_completion=False,
18
- help="radio_mlbee help",
19
  )
20
-
21
-
22
- def _version_callback(value: bool) -> None:
23
- if value:
24
- typer.echo(f"{app.info.name} v.{__version__} -- ...")
25
- raise typer.Exit()
26
-
27
-
28
- @app.command()
29
- def main(
30
- version: Optional[bool] = typer.Option( # pylint: disable=(unused-argument
31
- None,
32
- "--version",
33
- "-v",
34
- "-V",
35
- help="Show version info and exit.",
36
- callback=_version_callback,
37
- is_eager=True,
38
- ),
39
- ):
40
- """Define."""
41
-
42
-
43
- if __name__ == "__main__":
44
- app()
 
1
+ """Run app.py from __main__.py."""
2
+ # pylint: disable=no-value-for-parameter
3
+ import subprocess
4
+ import sys
5
 
6
+ from pathlib import Path
 
 
 
7
 
8
+ app = Path(__file__).with_name("app.py")
9
+ assert app.is_file, f"{app} does not exist or is not a file."
10
 
11
+ print(sys.executable, app.as_posix())
12
+ # /root/.cache/pypoetry/virtualenvs/radio-mlbee-VA82Wl8V-py3.8/bin/python /usr/src/app/radio_mlbee/app.py
13
 
14
+ subprocess.call(
15
+ [sys.executable, app.as_posix()],
16
+ text=True,
17
+ # shell=True,
18
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
radio_mlbee/__main__.py- ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Prep __main__.py."""
2
+ # pylint: disable=invalid-name
3
+ from pathlib import Path
4
+ from typing import Optional
5
+
6
+ import logzero
7
+ import typer
8
+ from logzero import logger
9
+ from set_loglevel import set_loglevel
10
+
11
+ from radio_mlbee import __version__, radio_mlbee
12
+
13
+ logzero.loglevel(set_loglevel())
14
+
15
+ app = typer.Typer(
16
+ name="radio_mlbee",
17
+ add_completion=False,
18
+ help="radio_mlbee help",
19
+ )
20
+
21
+
22
+ def _version_callback(value: bool) -> None:
23
+ if value:
24
+ typer.echo(f"{app.info.name} v.{__version__} -- ...")
25
+ raise typer.Exit()
26
+
27
+
28
+ @app.command()
29
+ def main(
30
+ version: Optional[bool] = typer.Option( # pylint: disable=(unused-argument
31
+ None,
32
+ "--version",
33
+ "-v",
34
+ "-V",
35
+ help="Show version info and exit.",
36
+ callback=_version_callback,
37
+ is_eager=True,
38
+ ),
39
+ ):
40
+ """Define."""
41
+
42
+
43
+ if __name__ == "__main__":
44
+ app()
radio_mlbee/app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Create entry."""
2
+ # pylint: disable=invalid-name, wrong-import-position, too-many-locals, too-many-statements
3
+ import sys
4
+ import os
5
+ import time
6
+ from pathlib import Path
7
+
8
+ import gradio as gr
9
+ import logzero
10
+ import pandas as pd
11
+ from about_time import about_time
12
+ from aset2pairs import aset2pairs
13
+ from cmat2aset import cmat2aset
14
+ from icecream import install as ic_install, ic
15
+ from logzero import logger
16
+ from seg_text import seg_text
17
+ from set_loglevel import set_loglevel
18
+
19
+ sys.path.insert(0, Path(__file__).parent.parent.as_posix())
20
+
21
+ from radio_mlbee import __version__ # noqa
22
+ from radio_mlbee.gen_cmat import gen_cmat # noqa
23
+ from radio_mlbee.utils import text1 as text1_, text2 as text2_ # noqa
24
+
25
+ os.environ["LOGLEVEL"] = "10" # turn debug on
26
+ os.environ["LOGLEVEL"] = "20" # turn debug off
27
+ logzero.loglevel(set_loglevel())
28
+ if set_loglevel() <= 10:
29
+ logger.info(" debug is on ")
30
+ else:
31
+ logger.info(" debug is off ")
32
+
33
+ ic_install()
34
+ ic.configureOutput(
35
+ includeContext=True,
36
+ outputFunction=logger.info,
37
+ )
38
+ ic.enable()
39
+ # ic.disenable() # to turn off
40
+
41
+ os.environ["TZ"] = "Asia/Shanghai"
42
+ try:
43
+ time.tzset() # type: ignore
44
+ except Exception as _:
45
+ logger.warning("time.tzset() error: %s. Probably running Windows, we let it pass.", _)
46
+
47
+
48
+ def greet(name):
49
+ """Greet."""
50
+ if not name:
51
+ name = "world"
52
+ return "Hello " + name + "!! (coming sooooon...)"
53
+
54
+
55
+ def ml_fn(
56
+ text1: str,
57
+ text2: str,
58
+ split_to_sents: bool = False,
59
+ preview: bool = False,
60
+ # download_csv: bool = False, # modi
61
+ ) -> pd.DataFrame:
62
+ """Align multilingual (50+ pairs) text1 text2."""
63
+ text1 = str(text1)
64
+ text2 = str(text2)
65
+ try:
66
+ paras1 = text1.splitlines()
67
+ paras1 = [_.strip() for _ in paras1 if _.strip()]
68
+ except Exception as exc:
69
+ logger.error(" praras.slpitlines() erros: %s, setting to ['']", exc)
70
+ paras1 = [""]
71
+ try:
72
+ paras2 = text2.splitlines()
73
+ paras2 = [_.strip() for _ in paras2 if _.strip()]
74
+ except Exception as exc:
75
+ logger.error(" praras slpitlines erros: %s, setting to ['']", exc)
76
+ paras2 = [""]
77
+
78
+ if split_to_sents:
79
+ try:
80
+ paras1 = seg_text(paras1)
81
+ except Exception as exc:
82
+ logger.error(exc)
83
+ try:
84
+ paras2 = seg_text(paras2)
85
+ except Exception as exc:
86
+ logger.error(exc)
87
+
88
+ with about_time() as t:
89
+ try:
90
+ cmat = gen_cmat(paras1, paras2)
91
+ except Exception as exc:
92
+ logger.exception(exc)
93
+ logger.info(paras1)
94
+ logger.info(paras2)
95
+ logger.info("len(paras1): %s, len(paras2): %s", len(paras1), len(paras2))
96
+ cmat = [[]]
97
+ try:
98
+ aset = cmat2aset(cmat)
99
+ except Exception as exc:
100
+ logger.exception(exc)
101
+ aset = [["", "", ""]]
102
+
103
+ len1 = len(paras1)
104
+ len2 = len(paras2)
105
+ ic(len1, len2)
106
+
107
+ if not (len1 and len2):
108
+ _ = "At least one text is empty... nothing to do."
109
+ return pd.DataFrame([[_]]), None, None
110
+
111
+ av = ""
112
+ len12 = len1 + len2
113
+ if len12:
114
+ av = f"{t.duration / len12 * 1000:.2f}"
115
+ logger.info(" %s blocks, took %s, av. %s s/1000 blk", len12, t.duration_human, av)
116
+
117
+ pairs = aset2pairs(paras1, paras2, aset)
118
+ df = pd.DataFrame(pairs, columns=["text1", "text2", "llh"])
119
+
120
+ html = None
121
+ if preview:
122
+ html = df.to_html()
123
+
124
+ _ = """ # modi
125
+ dl_csv = None
126
+ csv_str = None
127
+ if download_csv:
128
+ try:
129
+ dl_csv = Path("aligned-blocks.csv")
130
+ csv_str = df.to_csv(index=False)
131
+ dl_csv.write_text(csv_str, encoding="gbk")
132
+ ic("Saving df.to_csv to dl_csv...")
133
+ except Exception as exc:
134
+ logger.exception(exc)
135
+ # """
136
+
137
+ # return df, html, dl_csv
138
+ return df, html # modi
139
+
140
+
141
+ iface = gr.Interface(
142
+ fn=ml_fn,
143
+ inputs=[
144
+ "textarea",
145
+ "textarea",
146
+ gr.Checkbox(label="Split to sents?"),
147
+ gr.Checkbox(label="Preview?"),
148
+ # gr.Checkbox(label="Download csv?"), # modi
149
+ ],
150
+ outputs=[
151
+ "dataframe",
152
+ "html",
153
+ # gr.outputs.File(label="Click to download csv"), # modi
154
+ ],
155
+ # outputs="html",
156
+ title=f"radio-mlbee {__version__}",
157
+ description="mlbee rest api on dev ",
158
+ examples=[
159
+ # [text1, text2, False],
160
+ # [text1[: len(text1) // 5], text2[: len(text2) // 5], False, False, False],
161
+ [text1_, text2_, False, False], # modi
162
+ ],
163
+ allow_flagging="never",
164
+ )
165
+
166
+ debug = False
167
+ if set_loglevel() <= 10:
168
+ debug = True
169
+
170
+ iface.launch(
171
+ show_error=debug,
172
+ enable_queue=True,
173
+ debug=debug,
174
+ )
radio_mlbee/gradio_queue.db ADDED
File without changes
radio_mlbee/~syncthing~gradio_queue.db-journal.tmp ADDED
Binary file (512 Bytes). View file