Skip to content

Commit 15ee937

Browse files
committed
now it saves the model in local directory #1333
1 parent e05a0bd commit 15ee937

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

NLP/Sentence_Similarity.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
import os
12
from sentence_transformers import SentenceTransformer, util
23

4+
MODEL_NAME = 'all-MiniLM-L6-v2'
5+
MODEL_FOLDER = 'model'
6+
37
def load_file(file_path):
48
with open(file_path, 'r', encoding='utf-8') as file:
59
return [line.strip() for line in file if line.strip()]
610

11+
def load_or_download_model():
12+
model_path = os.path.join(MODEL_FOLDER, MODEL_NAME)
13+
if os.path.exists(model_path):
14+
print(f"Loading model from {model_path}")
15+
return SentenceTransformer(model_path)
16+
else:
17+
print(f"Downloading model {MODEL_NAME}")
18+
model = SentenceTransformer(MODEL_NAME)
19+
os.makedirs(MODEL_FOLDER, exist_ok=True)
20+
model.save(model_path)
21+
print(f"Model saved to {model_path}")
22+
return model
23+
724
def find_similar_sentences(query, file_path, top_n=5):
825
# Load the pre-trained model
9-
model = SentenceTransformer('all-MiniLM-L6-v2')
26+
model = load_or_download_model()
1027

1128
# Load and encode the sentences from the file
1229
sentences = load_file(file_path)
@@ -23,14 +40,27 @@ def find_similar_sentences(query, file_path, top_n=5):
2340

2441
return top_results
2542

43+
def validate_file_path(file_path):
44+
if not file_path.endswith('.txt'):
45+
file_path += '.txt'
46+
if not os.path.exists(file_path):
47+
raise FileNotFoundError(f"The file '{file_path}' does not exist.")
48+
return file_path
49+
2650
def main():
2751
print("Welcome to the Sentence Similarity Search Tool!")
2852

2953
# Get user input for query
3054
query = input("Enter your query: ")
3155

32-
# Get user input for file path
33-
file_path = input("Enter the path to your text file: ")
56+
# Get user input for file path and validate it
57+
while True:
58+
file_path = input("Enter the path to your text file without extension: ")
59+
try:
60+
file_path = validate_file_path(file_path)
61+
break
62+
except FileNotFoundError as e:
63+
print(f"Error: {str(e)} Please try again.")
3464

3565
try:
3666
results = find_similar_sentences(query, file_path)
@@ -39,8 +69,6 @@ def main():
3969
for sentence, score in results:
4070
print(f"Similarity: {score:.4f}")
4171
print(f"Sentence: {sentence}\n")
42-
except FileNotFoundError:
43-
print(f"Error: The file '{file_path}' was not found. Please check the file path and try again.")
4472
except Exception as e:
4573
print(f"An error occurred: {str(e)}")
4674

0 commit comments

Comments
 (0)