GVAmaresh
commited on
Commit
·
744370a
1
Parent(s):
956b06c
dev: check working
Browse files- Dockerfile +13 -0
- app.py +142 -0
- requirements.txt +25 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
48 |
+
|
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 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
torch
|
4 |
+
torchaudio
|
5 |
+
tensorflow
|
6 |
+
scipy
|
7 |
+
websockets
|
8 |
+
wsproto
|
9 |
+
soundfile
|
10 |
+
SpeechRecognition
|
11 |
+
pydub
|
12 |
+
transformers
|
13 |
+
ffmpeg
|
14 |
+
librosa
|
15 |
+
soundfile
|
16 |
+
python-multipart
|
17 |
+
matplotlib
|
18 |
+
numpy
|
19 |
+
google-api-python-client
|
20 |
+
google-auth-httplib2
|
21 |
+
google-auth-oauthlib
|
22 |
+
gdown
|
23 |
+
PyPDF2
|
24 |
+
tf-keras
|
25 |
+
requests
|