ERROR! ImportError: cannot import name 'UMAP' from 'umap'

I am having a problem when importing “from bertopic._bertopic import BERTopic”
I have deployed my app and i get the following error ERROR! ImportError: cannot import name ‘UMAP’ from ‘umap’.

 ❱ 1 from bertopic._bertopic import BERTopic                                   
    2                                                                           
    3 __version__ = "0.16.0"                                                    
    4                                                                           
                                                                                
  /home/adminuser/venv/lib/python3.11/site-packages/bertopic/_bertopic.py:38    
  in <module>                                                                   
                                                                                
      35                                                                        
      36 # Models                                                               
      37 import hdbscan                                                         
  ❱   38 from umap import UMAP                                                  
      39 from sklearn.preprocessing import normalize                            
      40 from sklearn import __version__ as sklearn_version                     
      41 from sklearn.cluster import AgglomerativeClustering                    
────────────────────────────────────────────────────────────────────────────────
ImportError: cannot import name 'UMAP' from 'umap' 
(/home/adminuser/venv/lib/python3.11/site-packages/umap/__init__.py)
2024-04-19 21:35:38.844 503 GET /script-health-check (10.12.38.47) 381.88ms
[nltk_data] Downloading package punkt to /home/appuser/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package stopwords to
[nltk_data]     /home/appuser/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package wordnet to /home/appuser/nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /home/appuser/nltk_data...
[nltk_data]   Package averaged_perceptron_tagger is already up-to-
[nltk_data]       date!
[nltk_data] Downloading package maxent_ne_chunker to
[nltk_data]     /home/appuser/nltk_data...
[nltk_data]   Package maxent_ne_chunker is already up-to-date!
[nltk_data] Downloading package words to /home/appuser/nltk_data...
[nltk_data]   Package words is already up-to-date!
────────────────────── Traceback (most recent call last) ───────────────────────
  /home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptru  
  nner/script_runner.py:584 in _run_script                                      
                                                                                
  /mount/src/capstone-streamlit/mezyan_reviews_streamlit.py:67 in <module>      
                                                                                
     64 from transformers import TFAutoModelForSequenceClassification           
     65 from transformers import AutoTokenizer, AutoConfig                      
     66 from scipy.special import softmax                                       
  ❱  67 from bertopic import BERTopic                                           
     68 MODEL = f"cardiffnlp/twitter-roberta-base-sentiment-latest"             
     69 tokenizer = AutoTokenizer.from_pretrained(MODEL)                        
     70 config = AutoConfig.from_pretrained(MODEL)                              
                                                                                
  /home/adminuser/venv/lib/python3.11/site-packages/bertopic/__init__.py:1 in   
  <module>                                                                      
                                                                                
  ❱ 1 from bertopic._bertopic import BERTopic                                   
    2                                                                           
    3 __version__ = "0.16.0"                                                    
    4                                                                           
                                                                                
  /home/adminuser/venv/lib/python3.11/site-packages/bertopic/_bertopic.py:38    
  in <module>                                                                   
                                                                                
      35                                                                        
      36 # Models                                                               
      37 import hdbscan                                                         
  ❱   38 from umap import UMAP                                                  
      39 from sklearn.preprocessing import normalize                            
      40 from sklearn import __version__ as sklearn_version                     
      41 from sklearn.cluster import AgglomerativeClustering                    
────────────────────────────────────────────────────────────────────────────────
ImportError: cannot import name 'UMAP' from 'umap' 
(/home/adminuser/venv/lib/python3.11/site-packages/umap/__init__.py)

Hey @Jad-Shahine
Hope you’re doing well! :slight_smile:

Didn’t you upload the .py file where you’ve written all the above code during app deployment? Because I can’t seem to find the above code anywhere in your attached GitHub repo. :thinking:

Make sure to upload the file if you haven’t yet. Also, please add import streamlit as st in your main app script mezyan_reviews_streamlit.py file.

Let me know your further queries!
Thanks,
Shruti

Hello, thank you for replying. Apologies i was trying something.

I have reuploaded the file. the issue with UMAP was fixed however i have a new issue with importing BERTopic. I get the following error

ImportError: cannot import name 'BERTopic' from partially initialized module 
'bertopic' (most likely due to a circular import) 
(/home/adminuser/venv/lib/python3.12/site-packages/bertopic/__init__.py)

i have even tried a new app that only has
import streamlit as st
from bertopic import BERTopic

and i get the same error

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.