Skip to content

Commit 419eb3b

Browse files
authored
Documentation & Updates (#5)
- add colab notebook etc to README - various usability enhancements Signed-off-by: Peter <74869040+pszemraj@users.noreply.github.com>
1 parent 7405685 commit 419eb3b

4 files changed

Lines changed: 98 additions & 75 deletions

File tree

CONTRIBUTING.md

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
```{todo} THIS IS SUPPOSED TO BE AN EXAMPLE. MODIFY IT ACCORDING TO YOUR NEEDS!
2-
3-
The document assumes you are using a source repository service that promotes a
4-
contribution model similar to [GitHub's fork and pull request workflow].
5-
While this is true for the majority of services (like GitHub, GitLab,
6-
BitBucket), it might not be the case for private repositories (e.g., when
7-
using Gerrit).
8-
9-
Also notice that the code examples might refer to GitHub URLs or the text
10-
might use GitHub specific terminology (e.g., *Pull Request* instead of *Merge
11-
Request*).
12-
13-
Please make sure to check the document having these assumptions in mind
14-
and update things accordingly.
15-
```
16-
17-
```{todo} Provide the correct links/replacements at the bottom of the document.
18-
```
19-
20-
```{todo} You might want to have a look on [PyScaffold's contributor's guide],
21-
22-
especially if your project is open source. The text should be very similar to
23-
this template, but there are a few extra contents that you might decide to
24-
also include, like mentioning labels of your issue tracker or automated
25-
releases.
26-
```
271

282
# Contributing
293

@@ -41,6 +15,25 @@ considerate, reasonable, and respectful**. When in doubt,
4115
[Python Software Foundation's Code of Conduct] is a good reference in terms of
4216
behavior guidelines.
4317

18+
---
19+
20+
- [Contributing](#contributing)
21+
- [Issue Reports](#issue-reports)
22+
- [Documentation Improvements](#documentation-improvements)
23+
- [creating pyscaffold-compatible documentation](#creating-pyscaffold-compatible-documentation)
24+
- [Working on the documentation](#working-on-the-documentation)
25+
- [Code Contributions](#code-contributions)
26+
- [Submit an issue](#submit-an-issue)
27+
- [Create an environment](#create-an-environment)
28+
- [Clone the repository](#clone-the-repository)
29+
- [Implement your changes](#implement-your-changes)
30+
- [Submit your contribution](#submit-your-contribution)
31+
- [Troubleshooting](#troubleshooting)
32+
- [Maintainer tasks](#maintainer-tasks)
33+
- [Releases](#releases)
34+
35+
---
36+
4437
## Issue Reports
4538

4639
If you experience bugs or general issues with `textsum`, please have a look
@@ -62,47 +55,43 @@ you help us to identify the root cause of the issue.
6255
## Documentation Improvements
6356

6457
You can help improve `textsum` docs by making them more readable and coherent, or
65-
by adding missing information and correcting mistakes.
58+
by adding missing information and correcting mistakes. Currently, this is easy as there is no official documentation. The README.md file is the only documentation, outside of the [wiki]. If you want to improve it, please do so and submit a pull request.
6659

67-
`textsum` documentation uses [Sphinx] as its main documentation compiler.
68-
This means that the docs are kept in the same repository as the project code, and
69-
that any documentation update is done in the same way was a code contribution.
60+
### creating pyscaffold-compatible documentation
7061

71-
```{todo} Don't forget to mention which markup language you are using.
62+
First, install [pyscaffoldext-markdown] and [pyscaffoldext-sphinx] extensions (as well as all other extensions):
7263

73-
e.g., [reStructuredText] or [CommonMark] with [MyST] extensions.
64+
```bash
65+
pip install pyscaffold[all]
7466
```
7567

76-
```{todo} If your project is hosted on GitHub, you can also mention the following tip:
77-
78-
:::{tip}
79-
Please notice that the [GitHub web interface] provides a quick way of
80-
propose changes in `textsum`'s files. While this mechanism can
81-
be tricky for normal code contributions, it works perfectly fine for
82-
contributing to the docs, and can be quite handy.
83-
84-
If you are interested in trying this method out, please navigate to
85-
the `docs` folder in the source [repository], find which file you
86-
would like to propose changes and click in the little pencil icon at the
87-
top, to open [GitHub's code editor]. Once you finish editing the file,
88-
please write a message in the form at the bottom of the page describing
89-
which changes have you made and what are the motivations behind them and
90-
submit your proposal.
91-
:::
68+
Then, clone this repo and update the documentation:
69+
70+
```bash
71+
git clone https://github.com/pszemraj/textsum.git
72+
putup textsum --force --markdown
9273
```
9374

75+
This will create a new directory `docs` with the documentation. You can now edit the files in `docs` and commit the changes.
76+
77+
### Working on the documentation
78+
9479
When working on documentation changes in your local machine, you can
9580
compile them using [tox] :
9681

9782
```
83+
9884
tox -e docs
85+
9986
```
10087

10188
and use Python's built-in web server for a preview in your web browser
10289
(`http://localhost:8000`):
10390

10491
```
92+
10593
python3 -m http.server --directory 'docs/_build/html'
94+
10695
```
10796

10897
## Code Contributions
@@ -335,37 +324,26 @@ on [PyPI], the following steps can be used to release a new version for
335324
to collectively create software are general and can be applied to all sorts
336325
of environments, including private companies and proprietary code bases.
337326

338-
339327
[black]: https://pypi.org/project/black/
340-
[commonmark]: https://commonmark.org/
341328
[contribution-guide.org]: http://www.contribution-guide.org/
342-
[creating a pr]: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request
343329
[descriptive commit message]: https://chris.beams.io/posts/git-commit
344330
[docstrings]: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html
345-
[first-contributions tutorial]: https://github.com/firstcontributions/first-contributions
346331
[flake8]: https://flake8.pycqa.org/en/stable/
347332
[git]: https://git-scm.com
348-
[github web interface]: https://docs.github.com/en/github/managing-files-in-a-repository/managing-files-on-github/editing-files-in-your-repository
349-
[github's code editor]: https://docs.github.com/en/github/managing-files-in-a-repository/managing-files-on-github/editing-files-in-your-repository
350-
[github's fork and pull request workflow]: https://guides.github.com/activities/forking/
351333
[guide created by freecodecamp]: https://github.com/freecodecamp/how-to-contribute-to-open-source
352334
[miniconda]: https://docs.conda.io/en/latest/miniconda.html
353-
[myst]: https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html
354335
[other kinds of contributions]: https://opensource.guide/how-to-contribute
355336
[pre-commit]: https://pre-commit.com/
356337
[pypi]: https://pypi.org/
357-
[pyscaffold's contributor's guide]: https://pyscaffold.org/en/stable/contributing.html
358338
[pytest can drop you]: https://docs.pytest.org/en/stable/usage.html#dropping-to-pdb-python-debugger-at-the-start-of-a-test
359339
[python software foundation's code of conduct]: https://www.python.org/psf/conduct/
360-
[restructuredtext]: https://www.sphinx-doc.org/en/master/usage/restructuredtext/
361-
[sphinx]: https://www.sphinx-doc.org/en/master/
362340
[tox]: https://tox.readthedocs.io/en/stable/
363341
[virtual environment]: https://realpython.com/python-virtual-environments-a-primer/
364342
[virtualenv]: https://virtualenv.pypa.io/en/stable/
365343

366-
367344
```{todo} Please review and change the following definitions:
368345
```
369346

370-
[repository]: https://github.com/<USERNAME>/textsum
371-
[issue tracker]: https://github.com/<USERNAME>/textsum/issues
347+
[repository]: https://github.com/pszemraj/textsum
348+
[issue tracker]: https://github.com/pszemraj/textsum/issues
349+
[wiki]: https://github.com/pszemraj/textsum/wiki

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,35 @@
1212

1313
# textsum
1414

15+
<a href="https://colab.research.google.com/gist/pszemraj/ff8a8486dc3303199fe9c9790a606fff/textsum-summarize-text-files-example.ipynb">
16+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
17+
</a>
18+
<a href="https://pypi.org/project/textsum/"> <img src="https://img.shields.io/pypi/v/textsum.svg" alt="PyPI-Server"/></a>
19+
20+
<br>
21+
1522
> utility for using transformers summarization models on text docs
1623
17-
The purpose of this package is to provide a simple interface (python API, CLI, gradio web UI) for using summarization models on text documents of arbitrary length.
24+
This package is to provides easy-to-use interfaces for using summarization models on text documents of arbitrary length. Currently implemented interfaces include a python API, CLI, and a shareable demo app.
25+
26+
For details, explanations, and docs, see the [wiki](https://github.com/pszemraj/textsum/wiki)
27+
28+
⚠️ _This is a WIP, but general functionality is available_ ⚠️
29+
30+
---
1831

19-
⚠️ **WARNING**: _This package is a WIP and is not ready for production use. Some things may not work yet._ ⚠️
32+
- [textsum](#textsum)
33+
- [Installation](#installation)
34+
- [Full Installation](#full-installation)
35+
- [Additional Details](#additional-details)
36+
- [Usage](#usage)
37+
- [Python API](#python-api)
38+
- [CLI](#cli)
39+
- [Demo App](#demo-app)
40+
- [Contributing](#contributing)
41+
- [Roadmap](#roadmap)
42+
43+
---
2044

2145
## Installation
2246

@@ -27,7 +51,7 @@ Install using pip:
2751
pip install textsum
2852
```
2953

30-
The `textsum` package is now installed in your virtual environment. You can now use the CLI or python API to summarize text docs see the [Usage](#usage) section for more details.
54+
The `textsum` package is now installed in your virtual environment. CLI commands/python API can be summarize text docs from anywhere. see the [Usage](#usage) section for more details.
3155

3256
### Full Installation
3357

@@ -125,6 +149,12 @@ This will start a local server that you can access in your browser & a shareable
125149

126150
---
127151

152+
## Contributing
153+
154+
Contributions are welcome! Please open an issue or PR if you have any ideas or suggestions.
155+
156+
See the [CONTRIBUTING.md](CONTRIBUTING.md) file for details on how to contribute.
157+
128158
## Roadmap
129159

130160
- [x] add CLI for summarization of all text files in a directory
@@ -133,6 +163,7 @@ This will start a local server that you can access in your browser & a shareable
133163
- [x] put on pypi
134164
- [ ] optimum inference integration, LLM.int8 inference
135165
- [ ] better documentation [in the wiki](https://github.com/pszemraj/textsum/wiki), details on improving performance (speed, quality, memory usage, etc.)
166+
- [ ] improvements to OCR helper module
136167

137168
_Other ideas? Open an issue or PR!_
138169

src/textsum/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def main(args):
233233
)
234234

235235
logging.info(f"finished summarization loop - output dir: {output_dir.resolve()}")
236-
summarizer.save_params(output_dir=output_dir, hf_tag=args.model_name)
236+
summarizer.save_params(output_path=output_dir, hf_tag=args.model_name)
237237
logging.info("finished summarizing files")
238238

239239

src/textsum/summarize.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def get_inference_params(self):
138138
"""get the inference parameters currently being used"""
139139
return self.inference_params
140140

141+
def update_loglevel(self, loglevel: int = logging.INFO):
142+
"""update the loglevel of the logger"""
143+
self.logger.setLevel(loglevel)
144+
141145
def summarize_and_score(self, ids, mask, **kwargs):
142146
"""
143147
summarize_and_score - summarize a batch of text and return the summary and output scores
@@ -157,6 +161,9 @@ def summarize_and_score(self, ids, mask, **kwargs):
157161
# put global attention on <s> token
158162
global_attention_mask[:, 0] = 1
159163

164+
self.logger.debug(
165+
f"generating summary for batch of size {input_ids.shape} with {kwargs}"
166+
)
160167
if self.is_general_attention_model:
161168
summary_pred_ids = self.model.generate(
162169
input_ids,
@@ -180,6 +187,7 @@ def summarize_and_score(self, ids, mask, **kwargs):
180187
skip_special_tokens=True,
181188
remove_invalid_values=True,
182189
)
190+
self.logger.debug(f"summary: {summary}")
183191
score = round(summary_pred_ids.sequences_scores.cpu().numpy()[0], 4)
184192

185193
return summary, score
@@ -200,15 +208,14 @@ def summarize_via_tokenbatches(
200208
:return: a list of summaries, a list of scores, and a list of the input text for each batch
201209
"""
202210

203-
logger = logging.getLogger(__name__)
204211
# log all input parameters
205212
if batch_length and batch_length < 512:
206-
logger.warning(
213+
self.logger.warning(
207214
"WARNING: entered batch_length was too low at {batch_length}, resetting to 512"
208215
)
209216
batch_length = 512
210217

211-
logger.debug(
218+
self.logger.debug(
212219
f"batch_length: {batch_length} batch_stride: {batch_stride}, kwargs: {kwargs}"
213220
)
214221
if kwargs:
@@ -246,7 +253,7 @@ def summarize_via_tokenbatches(
246253
"summary_score": score,
247254
}
248255
gen_summaries.append(_sum)
249-
logger.debug(f"\n\t{result[0]}\nScore:\t{score}")
256+
self.logger.debug(f"\n\t{result[0]}\nScore:\t{score}")
250257
pbar.update()
251258

252259
pbar.close()
@@ -374,10 +381,12 @@ def summarize_file(
374381
**kwargs,
375382
) -> Path:
376383
"""
377-
summarize_file - a function that takes a text file and returns a summary
384+
summarize_file - summarize a text file and save the summary to a file
378385
379386
:param str or Path file_path: the path to the text file
380387
:param str or Path output_dir: the directory to save the summary to, defaults to None (current working directory)
388+
:param int batch_length: number of tokens to use in each batch, defaults to None (self.token_batch_length)
389+
:param int batch_stride: number of tokens to stride between batches, defaults to None (self.batch_stride)
381390
:param bool lowercase: whether to lowercase the text prior to summarization, defaults to False
382391
383392
:return Path: the path to the summary file
@@ -406,22 +415,26 @@ def summarize_file(
406415

407416
def save_params(
408417
self,
409-
output_dir: str or Path = None,
418+
output_path: str or Path = None,
410419
hf_tag: str = None,
411420
verbose: bool = False,
412421
) -> None:
413422
"""
414423
save_params - save the parameters of the run to a json file
415424
416425
:param dict params: parameters to save
417-
:param str or Path output_dir: directory to save the parameters to
426+
:param str or Path output_path: directory or filepath to save the parameters to
418427
:param str hf_tag: the model tag on huggingface (will be used instead of self.model_name_or_path)
419428
:param bool verbose: whether to log the parameters
420429
421430
:return: None
422431
"""
423-
output_dir = Path(output_dir) if output_dir is not None else Path.cwd()
424-
metadata_path = output_dir / "summarization_parameters.json"
432+
output_path = Path(output_path) if output_path is not None else Path.cwd()
433+
metadata_path = (
434+
output_path / "summarization_parameters.json"
435+
if output_path.is_dir()
436+
else output_path
437+
) # if output_path is a file, use that, otherwise use the default name
425438

426439
exported_params = self.get_inference_params().copy()
427440
exported_params["META_huggingface_model"] = (
@@ -436,3 +449,4 @@ def save_params(
436449
logging.debug(f"Saved parameters to {metadata_path}")
437450
if verbose:
438451
self.logger.info(f"parameters: {exported_params}")
452+
print(f"saved parameters to {metadata_path}")

0 commit comments

Comments
 (0)