Spaces:
Sleeping
Sleeping
Commit ·
af760b7
1
Parent(s): 8693100
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
# cwd = os.getcwd()
|
| 3 |
+
# cachedir = cwd+'/cache'
|
| 4 |
+
# os.mkdir(cachedir)
|
| 5 |
+
# os.environ['TRANSFORMERS_CACHE'] = "/app/yo/cache"
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
import uvicorn
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from sentence_transformers import SentenceTransformer, util
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Load the SentenceTransformer model
|
| 16 |
+
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
|
| 17 |
+
|
| 18 |
+
class StringInput(BaseModel):
|
| 19 |
+
sentence1: str
|
| 20 |
+
sentence2: str
|
| 21 |
+
|
| 22 |
+
class ListInput(BaseModel):
|
| 23 |
+
sentences1: list
|
| 24 |
+
sentences2: list
|
| 25 |
+
|
| 26 |
+
class MatrixInput(BaseModel):
|
| 27 |
+
matrix: list
|
| 28 |
+
|
| 29 |
+
# Configure CORS settings
|
| 30 |
+
app.add_middleware(
|
| 31 |
+
CORSMiddleware,
|
| 32 |
+
allow_origins=["*"], # You should restrict this to specific origins in production
|
| 33 |
+
allow_credentials=True,
|
| 34 |
+
allow_methods=["*"],
|
| 35 |
+
allow_headers=["*"],
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
@app.post("/calculate-cosine-similarity-tabulated")
|
| 39 |
+
async def calculate_cosine_similarity_tabulated(data: ListInput):
|
| 40 |
+
try:
|
| 41 |
+
# Encode the sentences
|
| 42 |
+
embeddings1 = model.encode(data.sentences1)
|
| 43 |
+
embeddings2 = model.encode(data.sentences2)
|
| 44 |
+
|
| 45 |
+
# Calculate cosine similarity scores
|
| 46 |
+
similarity_table = []
|
| 47 |
+
|
| 48 |
+
# Add the first row with the specified headers
|
| 49 |
+
first_row = [""] + ["PO1", "PO2", "PO3", "PO4", "PO5", "PO6", "PO7", "PO8", "PO9", "PO10", "PO11", "PO12", "PSO1", "PSO2", "PSO3"]
|
| 50 |
+
similarity_table.append(first_row)
|
| 51 |
+
|
| 52 |
+
# Iterate through the sentences and calculate similarity scores
|
| 53 |
+
for i, sentence1 in enumerate(embeddings1):
|
| 54 |
+
row = ["CO" + str(i + 1)] # Start the row with "S1", "S2", ...
|
| 55 |
+
|
| 56 |
+
for sentence2 in embeddings2:
|
| 57 |
+
score = util.pytorch_cos_sim(sentence1, sentence2).item()
|
| 58 |
+
score = max(score, 0)
|
| 59 |
+
formatted_score = f"{score:.3f}"
|
| 60 |
+
row.append(formatted_score)
|
| 61 |
+
|
| 62 |
+
# Add the row to the similarity table
|
| 63 |
+
similarity_table.append(row)
|
| 64 |
+
|
| 65 |
+
return {"cosine_similarity_table": similarity_table}
|
| 66 |
+
except Exception as e:
|
| 67 |
+
# Handle any exceptions that might occur during calculation
|
| 68 |
+
raise HTTPException(status_code=500, detail="An error occurred during calculation")
|
| 69 |
+
|
| 70 |
+
@app.post("/calculate-cosine-similarity")
|
| 71 |
+
async def calculate_cosine_similarity(data: StringInput):
|
| 72 |
+
try:
|
| 73 |
+
# Encode the sentences
|
| 74 |
+
my_embedding = model.encode(data.sentence1)
|
| 75 |
+
embeddings = model.encode(data.sentence2)
|
| 76 |
+
|
| 77 |
+
# Calculate cosine similarity
|
| 78 |
+
cos_sim = util.pytorch_cos_sim(my_embedding, embeddings).item()
|
| 79 |
+
|
| 80 |
+
return {"cosine_similarity": cos_sim}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
raise HTTPException(status_code=500, detail="An error occurred during calculation")
|