GVAmaresh
commited on
Commit
·
c666a1c
1
Parent(s):
6e5a0ec
dev check working
Browse files
app.py
CHANGED
@@ -1,47 +1,4 @@
|
|
1 |
-
from transformers import AutoTokenizer, AutoModel
|
2 |
-
import torch
|
3 |
-
import torch.nn.functional as F
|
4 |
|
5 |
-
def mean_pooling(model_output, attention_mask):
|
6 |
-
token_embeddings = model_output[
|
7 |
-
0
|
8 |
-
]
|
9 |
-
input_mask_expanded = (
|
10 |
-
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
11 |
-
)
|
12 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
|
13 |
-
input_mask_expanded.sum(1), min=1e-9
|
14 |
-
)
|
15 |
-
|
16 |
-
def cosine_similarity(u, v):
|
17 |
-
return F.cosine_similarity(u, v, dim=1)
|
18 |
-
|
19 |
-
|
20 |
-
def compare(text1, text2):
|
21 |
-
|
22 |
-
sentences = [text1, text2]
|
23 |
-
|
24 |
-
tokenizer = AutoTokenizer.from_pretrained("dmlls/all-mpnet-base-v2-negation")
|
25 |
-
model = AutoModel.from_pretrained("dmlls/all-mpnet-base-v2-negation")
|
26 |
-
|
27 |
-
encoded_input = tokenizer(
|
28 |
-
sentences, padding=True, truncation=True, return_tensors="pt"
|
29 |
-
)
|
30 |
-
|
31 |
-
with torch.no_grad():
|
32 |
-
model_output = model(**encoded_input)
|
33 |
-
|
34 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input["attention_mask"])
|
35 |
-
|
36 |
-
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
37 |
-
|
38 |
-
similarity_score = cosine_similarity(
|
39 |
-
sentence_embeddings[0].unsqueeze(0), sentence_embeddings[1].unsqueeze(0)
|
40 |
-
)
|
41 |
-
return similarity_score.item()
|
42 |
-
|
43 |
-
|
44 |
-
#--------------------------------------------------------------------------------------------------------------------
|
45 |
from fastapi import FastAPI
|
46 |
|
47 |
app = FastAPI()
|
@@ -49,94 +6,3 @@ app = FastAPI()
|
|
49 |
@app.get("/")
|
50 |
def greet_json():
|
51 |
return {"Hello": "World!"}
|
52 |
-
|
53 |
-
#--------------------------------------------------------------------------------------------------------------------
|
54 |
-
|
55 |
-
from transformers import pipeline
|
56 |
-
|
57 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
58 |
-
|
59 |
-
def Summerized_Text(text):
|
60 |
-
text = text.strip()
|
61 |
-
a = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
62 |
-
print(a)
|
63 |
-
return a[0]['summary_text']
|
64 |
-
|
65 |
-
#--------------------------------------------------------------------------------------------------------------------
|
66 |
-
|
67 |
-
from fastapi.responses import JSONResponse
|
68 |
-
from pydantic import BaseModel
|
69 |
-
from fastapi import FastAPI
|
70 |
-
|
71 |
-
class StrRequest(BaseModel):
|
72 |
-
text: str
|
73 |
-
|
74 |
-
|
75 |
-
class CompareRequest(BaseModel):
|
76 |
-
summary: str
|
77 |
-
text: str
|
78 |
-
|
79 |
-
|
80 |
-
@app.get("/api/check")
|
81 |
-
def check_connection():
|
82 |
-
try:
|
83 |
-
return JSONResponse(
|
84 |
-
{"status": 200, "message": "Message Successfully Sent"}, status_code=200
|
85 |
-
)
|
86 |
-
except Exception as e:
|
87 |
-
print("Error => ", e)
|
88 |
-
return JSONResponse({"status": 500, "message": str(e)}, status_code=500)
|
89 |
-
|
90 |
-
|
91 |
-
@app.post("/api/summerized")
|
92 |
-
async def get_summerized(request: StrRequest):
|
93 |
-
try:
|
94 |
-
print(request)
|
95 |
-
text = request.text
|
96 |
-
if not text:
|
97 |
-
return JSONResponse(
|
98 |
-
{"status": 422, "message": "Invalid Input"}, status_code=422
|
99 |
-
)
|
100 |
-
summary = Summerized_Text(text)
|
101 |
-
if "No abstract text." in summary:
|
102 |
-
return JSONResponse(
|
103 |
-
{"status": 500, "message": "No matching text found", "data": "None"}
|
104 |
-
)
|
105 |
-
|
106 |
-
if not summary:
|
107 |
-
return JSONResponse(
|
108 |
-
{"status": 500, "message": "No matching text found", "data": {}}
|
109 |
-
)
|
110 |
-
|
111 |
-
return JSONResponse(
|
112 |
-
{"status": 200, "message": "Matching text found", "data": summary}
|
113 |
-
)
|
114 |
-
|
115 |
-
except Exception as e:
|
116 |
-
print("Error => ", e)
|
117 |
-
return JSONResponse({"status": 500, "message": str(e)}, status_code=500)
|
118 |
-
|
119 |
-
|
120 |
-
@app.post("/api/compare")
|
121 |
-
def compareTexts(request: CompareRequest):
|
122 |
-
try:
|
123 |
-
text = request.text
|
124 |
-
summary = request.summary
|
125 |
-
if not summary or not text:
|
126 |
-
return JSONResponse(
|
127 |
-
{"status": 422, "message": "Invalid Input"}, status_code=422
|
128 |
-
)
|
129 |
-
value = compare(text, summary)
|
130 |
-
return JSONResponse(
|
131 |
-
{
|
132 |
-
"status": 200,
|
133 |
-
"message": "Comparisons made",
|
134 |
-
"value": value,
|
135 |
-
"text": text,
|
136 |
-
"summary": summary,
|
137 |
-
}
|
138 |
-
)
|
139 |
-
except Exception as e:
|
140 |
-
print("Error => ", e)
|
141 |
-
return JSONResponse({"status": 500, "message": str(e)}, status_code=500)
|
142 |
-
|
|
|
|
|
|
|
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from fastapi import FastAPI
|
3 |
|
4 |
app = FastAPI()
|
|
|
6 |
@app.get("/")
|
7 |
def greet_json():
|
8 |
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|