saicharan1234 commited on
Commit
79b9263
·
1 Parent(s): 84d404f

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +27 -0
  2. main.py +79 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10.12
2
+ COPY main.py /app/
3
+ COPY requirements.txt /app/
4
+ RUN pip install --no-cache-dir -r /app/requirements.txt
5
+ WORKDIR /app
6
+ CMD [ "uvicorn ","main:app","--host","0.0.0.0","--port","80" ]
7
+
8
+
9
+ FROM python:3.10.12
10
+
11
+ # Set the working directory in the container
12
+ WORKDIR /app
13
+
14
+ # Copy the requirements file to the container
15
+ COPY requirements.txt .
16
+
17
+ # Install dependencies
18
+ RUN pip install -r requirements.txt
19
+
20
+ # Copy the rest of the project files to the container
21
+ COPY main.py .
22
+
23
+ # Expose the port that the application will be running on
24
+ EXPOSE 8000
25
+
26
+ # Run the application
27
+ CMD ["uvicorn", "main:app"]
main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ import uvicorn
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from pydantic import BaseModel
5
+ from sentence_transformers import SentenceTransformer, util
6
+
7
+ app = FastAPI()
8
+
9
+ # Load the SentenceTransformer model
10
+ model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
11
+
12
+ class StringInput(BaseModel):
13
+ sentence1: str
14
+ sentence2: str
15
+
16
+ class ListInput(BaseModel):
17
+ sentences1: list
18
+ sentences2: list
19
+
20
+ class MatrixInput(BaseModel):
21
+ matrix: list
22
+
23
+ # Configure CORS settings
24
+ app.add_middleware(
25
+ CORSMiddleware,
26
+ allow_origins=["*"], # You should restrict this to specific origins in production
27
+ allow_credentials=True,
28
+ allow_methods=["*"],
29
+ allow_headers=["*"],
30
+ )
31
+
32
+ @app.post("/calculate-cosine-similarity-tabulated")
33
+ async def calculate_cosine_similarity_tabulated(data: ListInput):
34
+ try:
35
+ # Encode the sentences
36
+ embeddings1 = model.encode(data.sentences1)
37
+ embeddings2 = model.encode(data.sentences2)
38
+
39
+ # Calculate cosine similarity scores
40
+ similarity_table = []
41
+
42
+ # Add the first row with the specified headers
43
+ first_row = [""] + ["PO1", "PO2", "PO3", "PO4", "PO5", "PO6", "PO7", "PO8", "PO9", "PO10", "PO11", "PO12", "PSO1", "PSO2", "PSO3"]
44
+ similarity_table.append(first_row)
45
+
46
+ # Iterate through the sentences and calculate similarity scores
47
+ for i, sentence1 in enumerate(embeddings1):
48
+ row = ["S" + str(i + 1)] # Start the row with "S1", "S2", ...
49
+
50
+ for sentence2 in embeddings2:
51
+ score = util.pytorch_cos_sim(sentence1, sentence2).item()
52
+ formatted_score = f"{score:.3f}"
53
+ row.append(formatted_score)
54
+
55
+ # Add the row to the similarity table
56
+ similarity_table.append(row)
57
+
58
+ return {"cosine_similarity_table": similarity_table}
59
+ except Exception as e:
60
+ # Handle any exceptions that might occur during calculation
61
+ raise HTTPException(status_code=500, detail="An error occurred during calculation")
62
+
63
+ @app.post("/calculate-cosine-similarity")
64
+ async def calculate_cosine_similarity(data: StringInput):
65
+ try:
66
+ # Encode the sentences
67
+ my_embedding = model.encode(data.sentence1)
68
+ embeddings = model.encode(data.sentence2)
69
+
70
+ # Calculate cosine similarity
71
+ cos_sim = util.pytorch_cos_sim(my_embedding, embeddings).item()
72
+
73
+ return {"cosine_similarity": cos_sim}
74
+ except Exception as e:
75
+ raise HTTPException(status_code=500, detail="An error occurred during calculation")
76
+
77
+
78
+ if __name__ == "__main__":
79
+ uvicorn.run(app,port=8000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pydantic
4
+ sentence-transformers
5
+
6
+