dify icon indicating copy to clipboard operation
dify copied to clipboard

chore(docker): update nltk data download process to include unstructured download_nltk_packages

Open lework opened this issue 1 month ago • 4 comments

add unstructured download_nltk_packages, Fix the issue https://github.com/langgenius/dify/issues/28902.

[!IMPORTANT]

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

When using the document extractor, unstructured nltk files are automatically downloaded to the /home/dify/nltk_data/ directory.

dify@dify-api-7b8d54658f-cxnfr:/app/api$ ls -al /home/dify/nltk_data/tokenizers/
total 4236
drwxr-xr-x  3 dify dify    4096 Nov 28 02:39 .
drwxr-xr-x  4 dify dify    4096 Nov 28 02:38 ..
drwxr-xr-x 21 dify dify    4096 Nov 28 02:39 punkt_tab
-rw-r--r--  1 dify dify 4319076 Nov 28 02:39 punkt_tab.zip
dify@dify-api-7b8d54658f-cxnfr:/app/api$ ls -al /home/dify/nltk_data/t
taggers/    tokenizers/
dify@dify-api-7b8d54658f-cxnfr:/app/api$ ls -al /home/dify/nltk_data/taggers/
total 1516
drwxr-xr-x 3 dify dify    4096 Nov 28 02:38 .
drwxr-xr-x 4 dify dify    4096 Nov 28 02:38 ..
drwxr-xr-x 2 dify dify    4096 Nov 28 02:38 averaged_perceptron_tagger_eng
-rw-r--r-- 1 dify dify 1539115 Nov 28 02:38 averaged_perceptron_tagger_eng.zip

https://github.com/Unstructured-IO/unstructured/blob/91a9888d35b645988529e99b539eee9b7ffd30e3/unstructured/nlp/tokenize.py#L29

download_nltk_packages The downloaded packages do not match those specified in the Dockerfile, causing documents to be downloaded again during extraction. Use the download_nltk_packages method to download directly.

After modifying the Dockerfile to download in advance, unstructured will not download again.

dify@dify-api-6df5fdfb57-btp9k:/app/api$ ls -al /usr/local/share/nltk_data/taggers/
total 3988
drwxr-xr-x 4 root root    4096 Nov 28 06:26 .
drwxr-xr-x 5 root root    4096 Nov 28 06:26 ..
drwxr-xr-x 2 root root    4096 Nov 28 06:26 averaged_perceptron_tagger
-rwxr-xr-x 1 root root 2526731 Nov 28 06:26 averaged_perceptron_tagger.zip
drwxr-xr-x 2 root root    4096 Nov 28 06:26 averaged_perceptron_tagger_eng
-rwxr-xr-x 1 root root 1539115 Nov 28 06:26 averaged_perceptron_tagger_eng.zip
dify@dify-api-6df5fdfb57-btp9k:/app/api$ ls -al /usr/local/share/nltk_data/tokenizers/
total 17816
drwxr-xr-x  4 root root     4096 Nov 28 06:26 .
drwxr-xr-x  5 root root     4096 Nov 28 06:26 ..
drwxr-xr-x  3 root root     4096 Nov 28 06:26 punkt
-rwxr-xr-x  1 root root 13905355 Nov 28 06:26 punkt.zip
drwxr-xr-x 21 root root     4096 Nov 28 06:26 punkt_tab
-rwxr-xr-x  1 root root  4319076 Nov 28 06:26 punkt_tab.zip

Screenshots

Before After
NLTK_DATA=/usr/local/share/nltk_data python -c "import nltk; nltk.download('punkt'); nltk.download('averaged_perceptron_tagger'); nltk.download('stopwords')" && NLTK_DATA=/usr/local/share/nltk_data python -c "import nltk; from unstructured.nlp.tokenize import download_nltk_packages; nltk.download('punkt'); nltk.download('averaged_perceptron_tagger'); nltk.download('stopwords'); download_nltk_packages()"

Checklist

  • [ ] This change requires a documentation update, included: Dify Document
  • [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [x] I've updated the documentation accordingly.
  • [x] I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

lework avatar Nov 28 '25 08:11 lework

Summary of Changes

Hello @lework, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes the NLTK data download process within the Docker build for the API service. Previously, the unstructured library would trigger additional NLTK data downloads at runtime during document extraction, causing inefficiencies. By incorporating the download_nltk_packages function from unstructured.nlp.tokenize directly into the Dockerfile, all required NLTK data is now pre-downloaded during image creation, preventing redundant downloads and streamlining the document extraction workflow.

Highlights

  • NLTK Data Download Optimization: Integrated the unstructured.nlp.tokenize.download_nltk_packages function directly into the Dockerfile build process to ensure all necessary NLTK data is pre-downloaded.
  • Runtime Efficiency Improvement: Eliminated redundant NLTK data downloads that previously occurred during document extraction when using the unstructured library, leading to more efficient operations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Nov 28 '25 08:11 gemini-code-assist[bot]

Please create an issue and link it in the description.

crazywoola avatar Nov 28 '25 10:11 crazywoola

Please create an issue and link it in the description.

Added.

lework avatar Nov 28 '25 10:11 lework

@laipz8200 @QuantumGhost Please review.

lework avatar Dec 08 '25 08:12 lework