freemt commited on
Commit
f2dd44e
·
1 Parent(s): eb44383

Update first try

Browse files
.stignore ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ # Byte-compiled / optimized / DLL files
3
+ __pycache__
4
+ *.py[cod]
5
+ *$py.class
6
+
7
+ # C extensions
8
+ *.so
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build
13
+ develop-eggs
14
+ dist
15
+ downloads
16
+ eggs
17
+ .eggs
18
+ lib
19
+ lib64
20
+ parts
21
+ sdist
22
+ var
23
+ wheels
24
+ pip-wheel-metadata
25
+ share/python-wheels
26
+ *.egg-info
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Translations
42
+ *.mo
43
+ *.pot
44
+
45
+ # Django stuff:
46
+ *.log
47
+ local_settings.py
48
+ db.sqlite3
49
+
50
+ # Flask stuff:
51
+ instance
52
+ .webassets-cache
53
+
54
+ # Scrapy stuff:
55
+ .scrapy
56
+
57
+ # Sphinx documentation
58
+ docs/_build
59
+
60
+ # PyBuilder
61
+ target
62
+
63
+ # Jupyter Notebook
64
+ .ipynb_checkpoints
65
+
66
+ # IPython
67
+ profile_default
68
+ ipython_config.py
69
+
70
+ # pyenv
71
+ .python-version
72
+
73
+ # celery beat schedule file
74
+ celerybeat-schedule
75
+
76
+ # SageMath parsed files
77
+ *.sage.py
78
+
79
+ # Environments
80
+ .env
81
+ .venv
82
+ env
83
+ venv
84
+ ENV
85
+ env.bak
86
+ venv.bak
87
+
88
+ # Spyder project settings
89
+ .spyderproject
90
+ .spyproject
91
+
92
+ # Rope project settings
93
+ .ropeproject
94
+
95
+ # mypy
96
+ .mypy_cache
97
+ .dmypy.json
98
+ dmypy.json
99
+
100
+ # Pyre type checker
101
+ .pyre
app.py CHANGED
@@ -1,15 +1,44 @@
 
 
 
1
  import gradio as gr
2
  from model_pool import load_model_s
3
  from logzero import logger
4
 
 
 
5
  model_s = load_model_s()
6
- logger.info("model_s.encode('a'): %s", model_s.encode('a').shape)
7
 
8
- _ = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  try:
10
- interface = gr.Interface.load(model)
 
 
 
 
 
 
 
 
 
 
11
  except Exception as e:
12
- logger.error("gr.Interface.load(%s): %s", model, e)
13
- raise
 
 
14
  interface.launch()
15
- # """
 
1
+ """Bootstrap."""
2
+ # pylint: disable=invalid-name
3
+ import numpy as np
4
  import gradio as gr
5
  from model_pool import load_model_s
6
  from logzero import logger
7
 
8
+ from gradio_cmat import gradio_cmat
9
+
10
  model_s = load_model_s()
 
11
 
12
+
13
+ def fn(text1: str, text2: str) -> np.ndarray:
14
+ """Define."""
15
+ list1 = [elm.strip() for elm in text1.splitlines() if elm.strip()]
16
+ list2 = [elm.strip() for elm in text2.splitlines() if elm.strip()]
17
+ try:
18
+ res = gradio_cmat(list1, list2)
19
+ except Exception as e:
20
+ logger.error("gradio_cmat error: %s", e)
21
+ raise
22
+
23
+ return res
24
+
25
+
26
+ # _ = """
27
  try:
28
+ interface = gr.Interface.load(
29
+ fn,
30
+ [
31
+ gr.inputs.Textbox(
32
+ lines=3, default="The quick brown fox jumped over the lazy dogs."
33
+ ),
34
+ gr.inputs.Textbox(lines=3, default="The fast brown fox jumps over lazy dogs."),
35
+ ],
36
+ "numpy",
37
+ description="Gen corralation matrix",
38
+ )
39
  except Exception as e:
40
+ logger.exception("")
41
+ logger.error("gr.Interface.load(%s): %s", "fn", e)
42
+ raise
43
+
44
  interface.launch()
 
gradio_cmat/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """Init."""
2
+ __version__ = "0.1.0"
3
+ from .gradio_cmat import gradio_cmat
4
+
5
+ __all__ = ("gradio_cmat",)
gradio_cmat/__pycache__/__init__.cpython-38.pyc ADDED
Binary file (212 Bytes). View file
 
gradio_cmat/gradio_cmat.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Calculate the corr matrix."""
2
+ # pylint: disable=invalid-name
3
+ from typing import List, Optional
4
+
5
+ import numpy as np
6
+ from logzero import logger
7
+ from model_pool import load_model_s
8
+
9
+ model_s = load_model_s()
10
+
11
+
12
+ def gradio_cmat(
13
+ list1: List[str],
14
+ list2_: Optional[List[str]] = None,
15
+ ) -> np.ndarray:
16
+ """Gen corr matrix given two lists of str.
17
+
18
+ Args:
19
+ list1: list of strings
20
+ list2_: list of strings, if None, set to list1
21
+ Returns:
22
+ numpy.ndarray, (len(list1)xlen(list2))
23
+ """
24
+ if not list2_:
25
+ list2 = list1[:]
26
+ else:
27
+ list2 = list2_[:]
28
+
29
+ try:
30
+ vec1 = model_s.encode(list1)
31
+ except Exception as e:
32
+ logger.error("mode_s.encode(list1) error: %s", e)
33
+ raise
34
+
35
+ try:
36
+ vec2 = model_s.encode(list2)
37
+ except Exception as e:
38
+ logger.error("mode_s.encode(list2) error: %s", e)
39
+ raise
40
+
41
+ try:
42
+ res = vec1.dot(vec2.T)
43
+ except Exception as e:
44
+ logger.error("vec1.dot(vec2.T) error: %s", e)
45
+ raise
46
+
47
+ return res
okteto.yml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: gradio-cmat
2
+
3
+ # The build section defines how to build the images of
4
+ # your development environment
5
+ # More info: https://www.okteto.com/docs/reference/manifest/#build
6
+ # build:
7
+ # my-service:
8
+ # context: .
9
+
10
+ # The deploy section defines how to deploy your development environment
11
+ # More info: https://www.okteto.com/docs/reference/manifest/#deploy
12
+ # deploy:
13
+ # commands:
14
+ # - name: Deploy
15
+ # command: echo 'Replace this line with the proper 'helm'
16
+
17
+ # or 'kubectl' commands to deploy your development environment'
18
+
19
+ # The dependencies section defines other git repositories to be
20
+ # deployed as part of your development environment
21
+ # More info: https://www.okteto.com/docs/reference/manifest/#dependencies
22
+ # dependencies:
23
+ # - https://github.com/okteto/sample
24
+ # The dev section defines how to activate a development container
25
+ # More info: https://www.okteto.com/docs/reference/manifest/#dev
26
+ dev:
27
+ gradio-cmat:
28
+ # image: okteto/dev:latest
29
+ # image: python:3.8.13-bullseye
30
+ # image: simbachain/poetry-3.8
31
+ image: python:3.8
32
+ command: bash
33
+ workdir: /usr/src/app
34
+ sync:
35
+ - .:/usr/src/app
36
+ environment:
37
+ - name=$USER
38
+ forward:
39
+ - 8080:80
40
+ reverse:
41
+ - 9000:9000
42
+ autocreate: true
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
poetry.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [virtualenvs]
2
+ create = true
3
+ in-project = false
pyproject.toml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "gradio-cmat"
3
+ version = "0.1.0"
4
+ description = "Gen correlation matrix via gradio (mainly for hf spaces)"
5
+ authors = ["mikeee"]
6
+ license = "MIT"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.8"
10
+ logzero = "^1.7.0"
11
+ model-pool = "^0.1.3"
12
+ gradio = "^2.9.4"
13
+
14
+ [tool.poetry.dev-dependencies]
15
+ pytest = "^7.1.2"
16
+
17
+ [build-system]
18
+ requires = ["poetry-core>=1.0.0"]
19
+ build-backend = "poetry.core.masonry.api"
requirements.txt CHANGED
@@ -1,2 +1,40 @@
1
- logzero
2
- model-pool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ about-time==3.1.1; python_version >= "3.7" and python_version < "4" and python_full_version >= "3.7.9" and python_full_version < "4.0.0"
2
+ alive-progress==2.4.1; python_version >= "3.7" and python_version < "4" and python_full_version >= "3.7.9" and python_full_version < "4.0.0"
3
+ asttokens==2.0.5; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
4
+ certifi==2021.10.8; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
5
+ charset-normalizer==2.0.12; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3"
6
+ click==8.1.3; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
7
+ colorama==0.4.4; python_full_version >= "3.7.9" and sys_platform == "win32" and python_full_version < "4.0.0" and platform_system == "Windows" and python_version >= "3.7"
8
+ executing==0.8.3; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
9
+ filelock==3.6.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
10
+ grapheme==0.6.0; python_version >= "3.7" and python_version < "4" and python_full_version >= "3.7.9" and python_full_version < "4.0.0"
11
+ huggingface-hub==0.4.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
12
+ icecream==2.1.2; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
13
+ idna==3.3; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.5"
14
+ install==1.3.5; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.5"
15
+ joblib==1.1.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
16
+ logzero==1.7.0
17
+ model-pool==0.1.3; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
18
+ nltk==3.7; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
19
+ numpy==1.22.3; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.8"
20
+ packaging==21.3; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.6"
21
+ pillow==9.1.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
22
+ pygments==2.12.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.6"
23
+ pyparsing==3.0.8; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.6"
24
+ pyyaml==6.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.6"
25
+ regex==2022.4.24; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
26
+ requests==2.27.1; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
27
+ sacremoses==0.0.53; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
28
+ scikit-learn==1.0.2; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
29
+ scipy==1.6.1; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
30
+ sentence-transformers==2.2.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
31
+ sentencepiece==0.1.96; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
32
+ six==1.16.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
33
+ threadpoolctl==3.1.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
34
+ tokenizers==0.12.1; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
35
+ torch==1.11.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
36
+ torchvision==0.12.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
37
+ tqdm==4.64.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
38
+ transformers==4.18.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0"
39
+ typing-extensions==4.2.0; python_full_version >= "3.7.9" and python_full_version < "4.0.0" and python_version >= "3.7"
40
+ urllib3==1.26.9; python_full_version >= "3.7.9" and python_version < "4" and python_full_version < "4.0.0"
tests/__init__.py ADDED
File without changes
tests/__pycache__/__init__.cpython-38.pyc ADDED
Binary file (163 Bytes). View file
 
tests/__pycache__/test_version.cpython-38-pytest-6.2.5.pyc ADDED
Binary file (821 Bytes). View file
 
tests/test_gradio_cmat.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test gradio_cmat."""
2
+ from gradio_cmat import gradio_cmat
3
+
4
+
5
+ def test_gradio_cmat():
6
+ """Test gradio_cmat."""
7
+ list1 = ["test", "测试", "love"]
8
+ list2 = ["this is a test"]
9
+ cmat = gradio_cmat(list1, list2)
10
+ assert cmat.shape == (3, 1)
11
+
12
+ assert cmat[0, 0] > 0.6
13
+ assert cmat[1, 0] > 0.6
14
+ assert cmat[2, 0] < 0.2
15
+
16
+ # cmat.round(2)
tests/test_version.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ """Test __version__/sanity."""
2
+ from gradio_cmat import __version__
3
+
4
+ def test_version():
5
+ """Test version."""
6
+ assert __version__[:3] == "0.1"