后端接收时的代码:
xx=request.form.get('xx'); xx=request.form['xx']
xx=request.args.get(xx)
传送单个数据:
return render_template('html文件',xx='xx')
传送多个数据:先把数据写进字典,字典整体进行传输
return render_template('html文件',xx='字典变量')
目录结构:
index.py文件:
# --*-- coding:utf-8 --*-- # @Author : 一只楚楚猫 # @File : index.py # @Software : PyCharm from flask import * from sentence_transformers import SentenceTransformer import torch.nn as nn import torch import torch.nn.functional as F model = SentenceTransformer(r'E:\楚楚猫\code\python\01design\01creativity\01distance\all-MiniLM-L6-v2') app = Flask(__name__) result = dict() result["results"] = "" @app.route('/', methods=('GET', 'POST')) def index(): global result if request.method == 'POST': step1 = request.form.get("step1") step2 = request.form.get("step2") step3 = request.form.get("step3") step4 = request.form.get("step4") # 用户输入的内容 sentences = [step1, step2, step3, step4] results = list() # 384维 embeddings = torch.FloatTensor(model.encode(sentences)) # p=2就是计算欧氏距离,p=1就是曼哈顿距离 euclidean_distance = nn.PairwiseDistance(p=2) for i in range(0, embeddings.size()[0]): for j in range(i + 1, embeddings.size()[0]): cosine_similarity = round(F.cosine_similarity(embeddings[i], embeddings[j], dim=0).item(), 4) distance = round(euclidean_distance(embeddings[i], embeddings[j]).item(), 4) results.append( f"step{i + 1} & step{j + 1}的相关性:{cosine_similarity} step{i + 1} & step{j + 1}的距离:{distance}") print( f"step{i + 1} & step{j + 1}之间的相关性:{cosine_similarity}step{i + 1} & step{j + 1}之间的距离:{distance}") result["results"] = results return render_template('hello.html', result=result) return render_template('hello.html', result=result) if __name__ == '__main__': app.run(port=11252)
hello.html文件:
欢迎来到我的世界