| | from sentence_transformers import SentenceTransformer, util |
| | import pickle |
| | import pandas as pd |
| | import numpy as np |
| | import os |
| | import json |
| | from flask import Flask, request, jsonify |
| | from werkzeug.utils import secure_filename |
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| |
|
| | app = Flask(__name__) |
| |
|
| |
|
| |
|
| | @app.route('/match_text', methods=['POST']) |
| | def similarity(): |
| |
|
| | try: |
| |
|
| | |
| | data = request.get_json() |
| | |
| |
|
| | if 'text1' not in data or 'text2' not in data: |
| | |
| | return jsonify({'error': 'Both text1 and text2 must be provided.'}), 400 |
| |
|
| | |
| | sentences1 = data['text1'] |
| | sentences2 = data['text2'] |
| | |
| |
|
| | |
| | embeddings1 = model.encode(sentences1, convert_to_tensor=True) |
| | embeddings2 = model.encode(sentences2, convert_to_tensor=True) |
| | |
| |
|
| | |
| | cosine_scores = util.cos_sim(embeddings1, embeddings2) |
| | |
| |
|
| | print(f'{cosine_scores[0][0].item()}') |
| | return jsonify({'similarity_score': cosine_scores[0][0].item()}), 200 |
| |
|
| | except Exception as e: |
| | |
| | return jsonify({'error' : str(e)}), 500 |
| |
|
| |
|
| |
|
| | if __name__ == '__main__': |
| |
|
| | |
| | print(f'loading model...') |
| |
|
| | |
| | model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", cache_folder='./') |
| | |
| | model.max_seq_length = 512 |
| | print(f'model max lenght is :{model.max_seq_length}') |
| |
|
| | app.run(debug=False, port = 7860, host = '0.0.0.0', threaded = False) |
| |
|