dhruv107 commited on
Commit
b3fc83b
·
1 Parent(s): 9b9ba39
Files changed (1) hide show
  1. pretrained.py +18 -18
pretrained.py CHANGED
@@ -6,16 +6,16 @@ import os
6
  import json
7
  from flask import Flask, request, jsonify
8
  from werkzeug.utils import secure_filename
9
- import logging
10
 
11
 
12
- # Set up root logger, and add a file handler to root logger
13
- logging.basicConfig(filename = 'log_file.log',
14
- filemode='w',
15
- level = logging.DEBUG,
16
- format = '%(asctime)s:%(levelname)s:%(filename)s:%(funcName)s:%(lineno)d:%(message)s')
17
 
18
- logger = logging.getLogger()
19
 
20
 
21
  app = Flask(__name__)
@@ -27,40 +27,40 @@ def similarity():
27
 
28
  try:
29
 
30
- logger.debug(f'receiving the json data')
31
  data = request.get_json()
32
- logger.debug(f'received the json data')
33
 
34
  if 'text1' not in data or 'text2' not in data:
35
- logger.debug(f'Error : Both text1 and text2 must be provided!')
36
  return jsonify({'error': 'Both text1 and text2 must be provided.'}), 400
37
 
38
- logger.debug(f'extracting the sentences from the request')
39
  sentences1 = data['text1']
40
  sentences2 = data['text2']
41
- logger.debug(f'extracted the sentences from the request')
42
 
43
- logger.debug(f'calculating the embeddings')
44
  embeddings1 = model.encode(sentences1, convert_to_tensor=True)
45
  embeddings2 = model.encode(sentences2, convert_to_tensor=True)
46
- logger.debug(f'embeddings calculated')
47
 
48
- logger.debug(f'calculating the cosine score')
49
  cosine_scores = util.cos_sim(embeddings1, embeddings2)
50
- logger.debug(f'calculated the cosine score')
51
 
52
  print(f'{cosine_scores[0][0].item()}')
53
  return jsonify({'similarity_score': cosine_scores[0][0].item()}), 200
54
 
55
  except Exception as e:
56
- logger.debug(f'Unknown error! : {e}')
57
  return jsonify({'error' : str(e)}), 500
58
 
59
 
60
 
61
  if __name__ == '__main__':
62
 
63
- logger.debug(f'loading model...')
64
  print(f'loading model...')
65
 
66
  # model = SentenceTransformer("all-MiniLM-L6-v2", cache_folder='./')
 
6
  import json
7
  from flask import Flask, request, jsonify
8
  from werkzeug.utils import secure_filename
9
+ # import logging
10
 
11
 
12
+ # # Set up root logger, and add a file handler to root logger
13
+ # logging.basicConfig(filename = 'log_file.log',
14
+ # filemode='w',
15
+ # level = logging.DEBUG,
16
+ # format = '%(asctime)s:%(levelname)s:%(filename)s:%(funcName)s:%(lineno)d:%(message)s')
17
 
18
+ # logger = logging.getLogger()
19
 
20
 
21
  app = Flask(__name__)
 
27
 
28
  try:
29
 
30
+ # logger.debug(f'receiving the json data')
31
  data = request.get_json()
32
+ # logger.debug(f'received the json data')
33
 
34
  if 'text1' not in data or 'text2' not in data:
35
+ # logger.debug(f'Error : Both text1 and text2 must be provided!')
36
  return jsonify({'error': 'Both text1 and text2 must be provided.'}), 400
37
 
38
+ # logger.debug(f'extracting the sentences from the request')
39
  sentences1 = data['text1']
40
  sentences2 = data['text2']
41
+ # logger.debug(f'extracted the sentences from the request')
42
 
43
+ # logger.debug(f'calculating the embeddings')
44
  embeddings1 = model.encode(sentences1, convert_to_tensor=True)
45
  embeddings2 = model.encode(sentences2, convert_to_tensor=True)
46
+ # logger.debug(f'embeddings calculated')
47
 
48
+ # logger.debug(f'calculating the cosine score')
49
  cosine_scores = util.cos_sim(embeddings1, embeddings2)
50
+ # logger.debug(f'calculated the cosine score')
51
 
52
  print(f'{cosine_scores[0][0].item()}')
53
  return jsonify({'similarity_score': cosine_scores[0][0].item()}), 200
54
 
55
  except Exception as e:
56
+ # logger.debug(f'Unknown error! : {e}')
57
  return jsonify({'error' : str(e)}), 500
58
 
59
 
60
 
61
  if __name__ == '__main__':
62
 
63
+ # logger.debug(f'loading model...')
64
  print(f'loading model...')
65
 
66
  # model = SentenceTransformer("all-MiniLM-L6-v2", cache_folder='./')