Skip to content

Commit

Permalink
Merge pull request #10 from daskol/feature/huggingface
Browse files Browse the repository at this point in the history
Add integration with HuggingFace's `transformers`
  • Loading branch information
daskol authored Jul 10, 2023
2 parents dd38abe + c882235 commit b09c073
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 118 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ ENV/
/tools/migrate-db
/tools/list-keys

# Ignore swap files
*.swp
# Ignore autogenerate version.
/telepyth/version.py
57 changes: 41 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,58 @@ It is easy to send messages after token is issued. Just install `telepyth`
package by `pip install telepyth`, import it and notify

```python
import telepyth
import telepyth

%telepyth -t 123456789
%telepyth 'Very magic, wow!'
%telepyth -t 123456789
%telepyth 'Very magic, wow!'
```


#### HuggingFace Intergration

TelePyth also provides a [trainer callback][1] for HuggingFace's
`transformers`. There are several usage scenario. The first one is via setting
reporting method in `TrainingArguments`.

```python
import telepyth.interaction
import transformers

args = TrainingArguments(output_dir, report_to=['telepyth'])
```

For more fine-grained control on how and when callback should report metrics,
one can set up callback directly in a list of callbacks.

```python
callback = TelePythCallback(label='finetune', policy='last')
trainer = Trainer(callbacks=[callback])
trainer.train()
```

[1]: https://huggingface.co/docs/transformers/main/en/main_classes/callback

#### TelePyth Client

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

```python
from telepyth import TelepythClient
from telepyth import TelepythClient

tp = TelepythClient()
tp.send_text('Hello, World!') # notify with plain text
tp.send_text('_bold text_ and then *italic*') # or with markdown formatted text
tp.send_figure(some_pyplot_figure, 'Awesome caption here!') # or even with figure
tp = TelepythClient()
tp.send_text('Hello, World!') # notify with plain text
tp.send_text('_bold text_ and then *italic*') # or with markdown formatted text
tp.send_figure(some_pyplot_figure, 'Awesome caption here!') # or even with figure
```

#### CLI

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

```bash
telepyth -t 31415926 "Moar notifications!"
```shell
telepyth -t 31415926 "Moar notifications!"
```

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

```bash
curl https://daskol.xyz/api/notify/<access_token_here> \
-X POST \
-H 'Content-Type: plain/text' \
-d 'Hello, World!'
```shell
curl https://daskol.xyz/api/notify/<access_token_here> \
-X POST \
-H 'Content-Type: plain/text' \
-d 'Hello, World!'
```
See more examples and usage details [here](examples/).

## Credentials

&copy; [Daniel Bershatsky](https://github.com/daskol) <[daniel.bershatsky@skolkovotech.ru](mailto:daniel.berhatsky@skolkovotech.ru)>, 2017
&copy; [Daniel Bershatsky](https://github.com/daskol) <[daniel.bershatsky@skolkovotech.ru](mailto:daniel.berhatsky@skolkovotech.ru)>, 2017-2022
85 changes: 85 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# See PEP-517 and PEP-518 for details.

[build-system]
requires = ["setuptools", "setuptools_scm[toml]>=7", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "telepyth"
description = "Telegram notifications in Python."
license = {text = "MIT"}
authors = [
{name = "Daniel Bershatsky", email = "daniel.bershatsky@skolkovotech.ru"},
]
maintainers = [
{name = "Daniel Bershatsky", email = "daniel.bershatsky@skolkovotech.ru"},
]
readme = {file = "README.md", content-type = "text/markdown"}
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX",
"Operating System :: Unix",
"Operating System :: MacOS",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Scientific/Engineering :: Information Analysis",
"Topic :: Software Development",
"Typing :: Typed",
]
dynamic = ["version"]
dependencies = []
requires-python = ">=3.8,<4"

[project.optional-dependencies]
magic = ["ipython"]
huggingface = ["transformers"]

[project.scripts]
telepyth = "telepyth.cli:main"

[project.urls]
Homepage = "https://github.com/daskol/telepyth"
Repository = "https://github.com/daskol/telepyth.git"

[tool.isort]

[tool.mypy]
ignore_missing_imports = true
plugins = "numpy.typing.mypy_plugin"
show_column_numbers = true
show_error_codes = true
show_error_context = false

[tool.pytest.ini_options]
minversion = "7.0"
addopts = "-ra -q -m 'not slow'"
testpaths = ["telepyth"]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
]
filterwarnings = ["ignore::DeprecationWarning"]

[tool.setuptools]
include-package-data = false
platforms = ["Linux"]
zip-safe = false

[tool.setuptools.packages.find]
include = ["telepyth*"]

[tool.setuptools_scm]
write_to = "telepyth/version.py"

[tool.yapf]
based_on_style = "pep8"
16 changes: 0 additions & 16 deletions requirements.txt

This file was deleted.

3 changes: 0 additions & 3 deletions setup.cfg

This file was deleted.

62 changes: 0 additions & 62 deletions setup.py

This file was deleted.

16 changes: 11 additions & 5 deletions telepyth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# encoding: utf8
# __init__.py
"""Package telepyth is a frontend library to telepyth notification service for
Telegram.
"""

from telepyth.client import TelePythClient
from telepyth.utils import is_interactive
from telepyth.utils import is_huggingface_imported, is_interactive


TelepythClient = TelePythClient # make alias to origin definition
TelepythClient = TelePythClient # Alias to match origin definition.

if is_interactive():
from telepyth.magics import TelePythMagics

if is_huggingface_imported():
from telepyth.integration import TelePythCallback

__all__ = ('TelePythCallback', 'TelePythClient', 'TelePythMagics',
'TelepythClient')
16 changes: 9 additions & 7 deletions telepyth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from traceback import print_exception
from urllib.request import Request, urlopen

from .multipart import ContentDisposition, MultipartFormData
from .version import __user_agent__, __version__

from telepyth.multipart import ContentDisposition, MultipartFormData
from telepyth.version import __version__

__all__ = ['TelePythClient']

Expand All @@ -23,8 +22,11 @@ class TelePythClient(object):
configuration file.
"""

BASE_URL = 'https://telepyth.daskol.xyz/api/notify/'

DEBUG_URL = 'http://localhost:8080/api/notify/'
BASE_URL = 'https://daskol.xyz/api/notify/'

UA = f'telepyth/{__version__}'

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

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

try:
res = urlopen(req)

if res.getcode() != 200:
lines = '\n'.join(res.readlines())
msg = '[%d] %s: %s' %(res.getcode(), res.reason, lines)
msg = f'[{res.getcode()}] {res.reason}: {lines}'
print(msg, file=stderr)

return res.getcode()
Expand Down Expand Up @@ -143,7 +145,7 @@ def send_figure(self, fig, caption=''):
url = self.base_url + self.access_token
req = Request(url, method='POST')
req.add_header('Content-Type', content_type)
req.add_header('User-Agent', __user_agent__ + '/' + __version__)
req.add_header('User-Agent', TelePythClient.UA)
req.data = form().read()

res = urlopen(req)
Expand Down
1 change: 1 addition & 0 deletions telepyth/integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from telepyth.integration.huggingface import TelePythCallback
Loading

0 comments on commit b09c073

Please sign in to comment.