Skip to content

Commit b09c073

Browse files
authored
Merge pull request #10 from daskol/feature/huggingface
Add integration with HuggingFace's `transformers`
2 parents dd38abe + c882235 commit b09c073

File tree

13 files changed

+300
-118
lines changed

13 files changed

+300
-118
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@ ENV/
9999
/tools/migrate-db
100100
/tools/list-keys
101101

102-
# Ignore swap files
103-
*.swp
102+
# Ignore autogenerate version.
103+
/telepyth/version.py

README.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,58 @@ It is easy to send messages after token is issued. Just install `telepyth`
3232
package by `pip install telepyth`, import it and notify
3333

3434
```python
35-
import telepyth
35+
import telepyth
3636

37-
%telepyth -t 123456789
38-
%telepyth 'Very magic, wow!'
37+
%telepyth -t 123456789
38+
%telepyth 'Very magic, wow!'
3939
```
4040

41+
42+
#### HuggingFace Intergration
43+
44+
TelePyth also provides a [trainer callback][1] for HuggingFace's
45+
`transformers`. There are several usage scenario. The first one is via setting
46+
reporting method in `TrainingArguments`.
47+
48+
```python
49+
import telepyth.interaction
50+
import transformers
51+
52+
args = TrainingArguments(output_dir, report_to=['telepyth'])
53+
```
54+
55+
For more fine-grained control on how and when callback should report metrics,
56+
one can set up callback directly in a list of callbacks.
57+
58+
```python
59+
callback = TelePythCallback(label='finetune', policy='last')
60+
trainer = Trainer(callbacks=[callback])
61+
trainer.train()
62+
```
63+
64+
[1]: https://huggingface.co/docs/transformers/main/en/main_classes/callback
65+
4166
#### TelePyth Client
4267

4368
TelepythClient allows to send notifications, figures and markdown messages
4469
directly without using magics.
4570

4671
```python
47-
from telepyth import TelepythClient
72+
from telepyth import TelepythClient
4873

49-
tp = TelepythClient()
50-
tp.send_text('Hello, World!') # notify with plain text
51-
tp.send_text('_bold text_ and then *italic*') # or with markdown formatted text
52-
tp.send_figure(some_pyplot_figure, 'Awesome caption here!') # or even with figure
74+
tp = TelepythClient()
75+
tp.send_text('Hello, World!') # notify with plain text
76+
tp.send_text('_bold text_ and then *italic*') # or with markdown formatted text
77+
tp.send_figure(some_pyplot_figure, 'Awesome caption here!') # or even with figure
5378
```
5479

5580
#### CLI
5681

5782
TelePyth package also provide command line interface (CLI). It is similar to
5883
IPython magic. For example, one can send notifcation as following.
5984

60-
```bash
61-
telepyth -t 31415926 "Moar notifications!"
85+
```shell
86+
telepyth -t 31415926 "Moar notifications!"
6287
```
6388

6489
#### HTTP API
@@ -68,14 +93,14 @@ wrappers and bindings. This is useful for bash scripting. Just request
6893
TelePyth backend directly to notify user. For instance, to send message from
6994
bash:
7095

71-
```bash
72-
curl https://daskol.xyz/api/notify/<access_token_here> \
73-
-X POST \
74-
-H 'Content-Type: plain/text' \
75-
-d 'Hello, World!'
96+
```shell
97+
curl https://daskol.xyz/api/notify/<access_token_here> \
98+
-X POST \
99+
-H 'Content-Type: plain/text' \
100+
-d 'Hello, World!'
76101
```
77102
See more examples and usage details [here](examples/).
78103

79104
## Credentials
80105

81-
&copy; [Daniel Bershatsky](https://github.com/daskol) <[daniel.bershatsky@skolkovotech.ru](mailto:daniel.berhatsky@skolkovotech.ru)>, 2017
106+
&copy; [Daniel Bershatsky](https://github.com/daskol) <[daniel.bershatsky@skolkovotech.ru](mailto:daniel.berhatsky@skolkovotech.ru)>, 2017-2022

pyproject.toml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# See PEP-517 and PEP-518 for details.
2+
3+
[build-system]
4+
requires = ["setuptools", "setuptools_scm[toml]>=7", "wheel"]
5+
build-backend = "setuptools.build_meta"
6+
7+
[project]
8+
name = "telepyth"
9+
description = "Telegram notifications in Python."
10+
license = {text = "MIT"}
11+
authors = [
12+
{name = "Daniel Bershatsky", email = "daniel.bershatsky@skolkovotech.ru"},
13+
]
14+
maintainers = [
15+
{name = "Daniel Bershatsky", email = "daniel.bershatsky@skolkovotech.ru"},
16+
]
17+
readme = {file = "README.md", content-type = "text/markdown"}
18+
classifiers = [
19+
"Development Status :: 5 - Production/Stable",
20+
"Intended Audience :: Developers",
21+
"Intended Audience :: Information Technology",
22+
"Intended Audience :: Science/Research",
23+
"License :: OSI Approved :: MIT License",
24+
"Natural Language :: English",
25+
"Operating System :: Microsoft :: Windows",
26+
"Operating System :: POSIX",
27+
"Operating System :: Unix",
28+
"Operating System :: MacOS",
29+
"Programming Language :: Python",
30+
"Programming Language :: Python :: 3",
31+
"Programming Language :: Python :: 3.8",
32+
"Programming Language :: Python :: 3.9",
33+
"Programming Language :: Python :: 3.10",
34+
"Topic :: Scientific/Engineering",
35+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
36+
"Topic :: Scientific/Engineering :: Information Analysis",
37+
"Topic :: Software Development",
38+
"Typing :: Typed",
39+
]
40+
dynamic = ["version"]
41+
dependencies = []
42+
requires-python = ">=3.8,<4"
43+
44+
[project.optional-dependencies]
45+
magic = ["ipython"]
46+
huggingface = ["transformers"]
47+
48+
[project.scripts]
49+
telepyth = "telepyth.cli:main"
50+
51+
[project.urls]
52+
Homepage = "https://github.com/daskol/telepyth"
53+
Repository = "https://github.com/daskol/telepyth.git"
54+
55+
[tool.isort]
56+
57+
[tool.mypy]
58+
ignore_missing_imports = true
59+
plugins = "numpy.typing.mypy_plugin"
60+
show_column_numbers = true
61+
show_error_codes = true
62+
show_error_context = false
63+
64+
[tool.pytest.ini_options]
65+
minversion = "7.0"
66+
addopts = "-ra -q -m 'not slow'"
67+
testpaths = ["telepyth"]
68+
markers = [
69+
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
70+
]
71+
filterwarnings = ["ignore::DeprecationWarning"]
72+
73+
[tool.setuptools]
74+
include-package-data = false
75+
platforms = ["Linux"]
76+
zip-safe = false
77+
78+
[tool.setuptools.packages.find]
79+
include = ["telepyth*"]
80+
81+
[tool.setuptools_scm]
82+
write_to = "telepyth/version.py"
83+
84+
[tool.yapf]
85+
based_on_style = "pep8"

requirements.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

setup.cfg

Lines changed: 0 additions & 3 deletions
This file was deleted.

setup.py

Lines changed: 0 additions & 62 deletions
This file was deleted.

telepyth/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
# encoding: utf8
2-
# __init__.py
1+
"""Package telepyth is a frontend library to telepyth notification service for
2+
Telegram.
3+
"""
34

45
from telepyth.client import TelePythClient
5-
from telepyth.utils import is_interactive
6+
from telepyth.utils import is_huggingface_imported, is_interactive
67

7-
8-
TelepythClient = TelePythClient # make alias to origin definition
8+
TelepythClient = TelePythClient # Alias to match origin definition.
99

1010
if is_interactive():
1111
from telepyth.magics import TelePythMagics
12+
13+
if is_huggingface_imported():
14+
from telepyth.integration import TelePythCallback
15+
16+
__all__ = ('TelePythCallback', 'TelePythClient', 'TelePythMagics',
17+
'TelepythClient')

telepyth/client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from traceback import print_exception
99
from urllib.request import Request, urlopen
1010

11-
from .multipart import ContentDisposition, MultipartFormData
12-
from .version import __user_agent__, __version__
13-
11+
from telepyth.multipart import ContentDisposition, MultipartFormData
12+
from telepyth.version import __version__
1413

1514
__all__ = ['TelePythClient']
1615

@@ -23,8 +22,11 @@ class TelePythClient(object):
2322
configuration file.
2423
"""
2524

25+
BASE_URL = 'https://telepyth.daskol.xyz/api/notify/'
26+
2627
DEBUG_URL = 'http://localhost:8080/api/notify/'
27-
BASE_URL = 'https://daskol.xyz/api/notify/'
28+
29+
UA = f'telepyth/{__version__}'
2830

2931
def __init__(self, token=None, base_url=None, config=None, debug=False):
3032
defaults = dict(telepyth={
@@ -57,15 +59,15 @@ def __call__(self, text, markdown=True):
5759

5860
req = Request(url, method='POST')
5961
req.add_header('Content-Type', 'plain/text; encoding=utf-8')
60-
req.add_header('User-Agent', __user_agent__ + '/' + __version__)
62+
req.add_header('User-Agent', TelePythClient.UA)
6163
req.data = text.read().encode('utf8') # support for 3.4+
6264

6365
try:
6466
res = urlopen(req)
6567

6668
if res.getcode() != 200:
6769
lines = '\n'.join(res.readlines())
68-
msg = '[%d] %s: %s' %(res.getcode(), res.reason, lines)
70+
msg = f'[{res.getcode()}] {res.reason}: {lines}'
6971
print(msg, file=stderr)
7072

7173
return res.getcode()
@@ -143,7 +145,7 @@ def send_figure(self, fig, caption=''):
143145
url = self.base_url + self.access_token
144146
req = Request(url, method='POST')
145147
req.add_header('Content-Type', content_type)
146-
req.add_header('User-Agent', __user_agent__ + '/' + __version__)
148+
req.add_header('User-Agent', TelePythClient.UA)
147149
req.data = form().read()
148150

149151
res = urlopen(req)

telepyth/integration/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from telepyth.integration.huggingface import TelePythCallback

0 commit comments

Comments
 (0)