我尝试在 html 中使用 python 并收到此错误

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>

我遇到了这个错误

JsException(PythonError: Traceback (最近调用最后一次): 文件“/lib/python3.10/site-packages/_pyodide/_base.py”, 第 429 行,在 eval_code .运行(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", 第 300 行,在 运行 coroutine = eval(self.code, globals, locals) File " “,第 1 行,在 ModuleNotFoundError 中:没有名为 'sentence_transformers' )

的模块

sentence_transformers 不是 Python 标准库中内置的模块,因此您需要安装它。根据documentation, you do that by adding the package namepy-env元素:

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

但是,目前看来 sentence-transformers 并不是 available in pyodide。您可以打开一个 PR 或问题来添加它。