saicharan1234 commited on
Commit
3b144ec
·
1 Parent(s): 4776855

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +4 -21
main.py CHANGED
@@ -7,27 +7,9 @@ from sentence_transformers import SentenceTransformer, util
7
 
8
  app = FastAPI()
9
  current_directory = os.getcwd()
10
- os.environ['TRANSFORMERS_CACHE'] = current_directory
11
 
12
- # Function to load or download the SentenceTransformer model
13
- def load_model():
14
- try:
15
- model_path = os.path.join(current_directory, 'sentence-transformers/all-MiniLM-L6-v2')
16
-
17
- # Check if the model is already downloaded
18
- if not os.path.exists(model_path):
19
- # If not, download the model
20
- model = SentenceTransformer('all-MiniLM-L6-v2', cache_folder=current_directory)
21
- else:
22
- # If the model is already downloaded, load it
23
- model = SentenceTransformer(model_path, cache_folder=current_directory)
24
-
25
- return model
26
- except Exception as e:
27
- raise HTTPException(status_code=500, detail="An error occurred during model loading")
28
-
29
- # Load or download the SentenceTransformer model
30
- model = load_model()
31
 
32
  class StringInput(BaseModel):
33
  sentence1: str
@@ -93,6 +75,7 @@ async def calculate_cosine_similarity(data: StringInput):
93
  return {"cosine_similarity": cos_sim}
94
  except Exception as e:
95
  raise HTTPException(status_code=500, detail="An error occurred during calculation")
 
96
 
97
  if __name__ == "__main__":
98
- uvicorn.run(app, port=8000)
 
7
 
8
  app = FastAPI()
9
  current_directory = os.getcwd()
 
10
 
11
+ # Load the SentenceTransformer model
12
+ model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  class StringInput(BaseModel):
15
  sentence1: str
 
75
  return {"cosine_similarity": cos_sim}
76
  except Exception as e:
77
  raise HTTPException(status_code=500, detail="An error occurred during calculation")
78
+
79
 
80
  if __name__ == "__main__":
81
+ uvicorn.run(app,port=8000)