1
<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' )

1
  • The package sentence_transformers does not have a pure Python 3 wheel which means it is currently not supported. You could download the source and try building it yourself. Also, read @Thomas' answer which is correct and provides additional details. Commented Jun 3, 2022 at 8:06

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.