NLTK bug when deploying

I have tried everything when deploying my Streamlit app but can’t. I want to deploy this repo here: GitHub - yhernandez55/Streamlit_Projects Like the first project works fine just that the second and third don’t. The second project has this error: AttributeError: ‘XGBModel’ object has no attribute ‘device’. As said in the manage app section while I get this error in the page itself: AttributeError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/mount/src/streamlit_projects/YanellyHernandez.py", line 22, in <module>
    Flight_Prediction.show_flight_prediction()  # Call the function from Flight_Prediction.py
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/mount/src/streamlit_projects/Flight_Prediction.py", line 46, in show_flight_prediction
    xgb_model.set_params(tree_method='hist')  # Ensuring CPU mode
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.11/site-packages/xgboost/sklearn.py", line 834, in set_params
    parameters = self.get_xgb_params()
                 ^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.11/site-packages/xgboost/sklearn.py", line 868, in get_xgb_params
    params: Dict[str, Any] = self.get_params()
                             ^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.11/site-packages/xgboost/sklearn.py", line 851, in get_params
    params.update(cp.__class__.get_params(cp, deep))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.11/site-packages/xgboost/sklearn.py", line 848, in get_params
    params = super().get_params(deep)
             ^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.11/site-packages/sklearn/base.py", line 244, in get_params
    value = getattr(self, key)
            ^^^^^^^^^^^^^^^^^^
```.  And for the third project I get this error: LookupError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).
Traceback:
File "/mount/src/streamlit_projects/YanellyHernandez.py", line 24, in <module>
    Wine_Prediction.show_wine_predictions()  # Call the function from Wine_Prediction.py
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/mount/src/streamlit_projects/Wine_Prediction.py", line 84, in show_wine_predictions
    preprocessed_review = preprocess_text(input_review)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/mount/src/streamlit_projects/Wine_Prediction.py", line 53, in preprocess_text
    tokens = word_tokenize(text)  # Tokenize
             ^^^^^^^^^^^^^^^^^^^
File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/__init__.py", line 142, in word_tokenize
    sentences = [text] if preserve_line else sent_tokenize(text, language)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/__init__.py", line 119, in sent_tokenize
    tokenizer = _get_punkt_tokenizer(language)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/__init__.py", line 105, in _get_punkt_tokenizer
    return PunktTokenizer(language)
           ^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/punkt.py", line 1744, in __init__
    self.load_lang(lang)
File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/punkt.py", line 1749, in load_lang
    lang_dir = find(f"tokenizers/punkt_tab/{lang}/")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/data.py", line 579, in find
    raise LookupError(resource_not_found)  and the info in the manage app is this: LookupError: 

**********************************************************************

  Resource e[93mpunkt_tabe[0m not found.

  Please use the NLTK Downloader to obtain the resource:


  e[31m>>> import nltk

  >>> nltk.download('punkt_tab')

  e[0m

  For more information see: https://www.nltk.org/data.html


  Attempted to load e[93mtokenizers/punkt_tab/english/e[0m


  Searched in:

    - '/home/appuser/nltk_data'

    - '/home/adminuser/venv/nltk_data'

    - '/home/adminuser/venv/share/nltk_data'

    - '/home/adminuser/venv/lib/nltk_data'

    - '/usr/share/nltk_data'

    - '/usr/local/share/nltk_data'

    - '/usr/lib/nltk_data'

    - '/usr/local/lib/nltk_data'

**********************************************************************


2025-01-18 09:41:17.319 Uncaught app execution

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling

    result = func()

             ^^^^^^

  File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec

    exec(code, module.__dict__).    

also i am running it in conda environment of python 3.11.5

  File "/mount/src/streamlit_projects/YanellyHernandez.py", line 24, in <module>

    Wine_Prediction.show_wine_predictions()  # Call the function from Wine_Prediction.py

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/mount/src/streamlit_projects/Wine_Prediction.py", line 84, in show_wine_predictions

    preprocessed_review = preprocess_text(input_review)

                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/mount/src/streamlit_projects/Wine_Prediction.py", line 53, in preprocess_text

    tokens = word_tokenize(text)  # Tokenize

             ^^^^^^^^^^^^^^^^^^^

  File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/__init__.py", line 142, in word_tokenize

    sentences = [text] if preserve_line else sent_tokenize(text, language)

                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/__init__.py", line 119, in sent_tokenize

    tokenizer = _get_punkt_tokenizer(language)

                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/__init__.py", line 105, in _get_punkt_tokenizer

    return PunktTokenizer(language)

           ^^^^^^^^^^^^^^^^^^^^^^^^

  File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/punkt.py", line 1744, in __init__

    self.load_lang(lang)

  File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/tokenize/punkt.py", line 1749, in load_lang

    lang_dir = find(f"tokenizers/punkt_tab/{lang}/")

               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/home/adminuser/venv/lib/python3.11/site-packages/nltk/data.py", line 579, in find

    raise LookupError(resource_not_found)

LookupError: 

**********************************************************************

  Resource punkt_tab not found.

  Please use the NLTK Downloader to obtain the resource:


  >>> import nltk

  >>> nltk.download('punkt_tab')

  

  For more information see: https://www.nltk.org/data.html


  Attempted to load tokenizers/punkt_tab/english/


  Searched in:

    - '/home/appuser/nltk_data'

    - '/home/adminuser/venv/nltk_data'

    - '/home/adminuser/venv/share/nltk_data'

    - '/home/adminuser/venv/lib/nltk_data'

    - '/usr/share/nltk_data'

    - '/usr/local/share/nltk_data'

    - '/usr/lib/nltk_data'

    - '/usr/local/lib/nltk_data'

**********************************************************************

also i am running it in conda environment of python 3.11.5
1 Like

Hey @Yanelly ,

I would love to hear how you solved this issue ? I’m working on a similar application and would love to see your solution here in proactive preparation to this issue.
Furthermore, this might help someone else that may encounter this same issue. It’s always great to contribute back to forum. :slight_smile:

oh no I haven’t solved the issue yet that’s what I need help with. Like it works on my local machine just fine but when I deploy I get issues.

A try-except block to catch the LookupError exception and to trigger the package download should do the trick .

The default directory that NLTK uses to store download packages should be available for writing in the Community Cloud; I did not need to modify that path. Here is an example app and the corresponding repo.

image