Search code examples
javascriptpythonhtmlcsspyscript

my try to use python in html and got this error


<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    
     <py-env>
 
    </py-env>
  </head>
  <body> <py-script> 
from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer('all-MiniLM-L6-v2')

# Two lists of sentences
sentences1 = ['The cat sits outside',
             'A man is playing guitar',
             'The new movie is awesome']

sentences2 = ['The dog plays in the garden',
              'A woman watches TV',
              'The new movie is so great']

#Compute embedding for both lists
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
embeddings2 = model.encode(sentences2, convert_to_tensor=True)

#Compute cosine-similarities
cosine_scores = util.cos_sim(embeddings1, embeddings2)

#Output the pairs with their score
for i in range(len(sentences1)):
    print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))
    print ("___________________________________________________")

  </py-script> </body>
</html>

i got this error

JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in ModuleNotFoundError: No module named 'sentence_transformers' )


Solution

  • sentence_transformers is not a module that's built into the Python standard library, so you need to install it. According to the documentation, you do that by adding the package name to the py-env element:

    <py-env>
    - sentence-transformers
    </py-env>
    

    However, it doesn't look like sentence-transformers is available in pyodide at the moment. You could open a PR or issue to get it added.