Skip to content

Commit db7a0e5

Browse files
Merge pull request Samagra-Development#265 from AmakrushAI/gpu_embed
Gpu embed
2 parents 872a18d + 4529801 commit db7a0e5

File tree

9 files changed

+144
-3
lines changed

9 files changed

+144
-3
lines changed

config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"nginx": [],
1010
"nginx": ["client_max_body_size 100M;", "proxy_read_timeout 600;", "proxy_connect_timeout 600;", "proxy_send_timeout 600;"],
1111
"constraints": ["node.labels.node_vm_type==gpu"],
12-
"build": true
12+
"build": false
1313
},
1414
{
1515
"serviceName": "asr_lang_detect",
@@ -20,7 +20,7 @@
2020
"nginx": [],
2121
"nginx": ["client_max_body_size 100M;", "proxy_read_timeout 600;", "proxy_connect_timeout 600;", "proxy_send_timeout 600;"],
2222
"constraints": ["node.labels.node_vm_type==gpu"],
23-
"build": true
23+
"build": false
2424
},
2525
{
2626
"serviceName": "ner",
@@ -176,7 +176,7 @@
176176
},
177177
"nginx": [],
178178
"constraints": ["node.labels.node_vm_type==gpu"],
179-
"build": true
179+
"build": false
180180
}
181181
]
182182
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Instructor model for generating embedding
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.9-slim
3+
4+
WORKDIR /app
5+
6+
7+
#install requirements
8+
COPY requirements.txt requirements.txt
9+
RUN pip3 install -r requirements.txt
10+
11+
# Copy the rest of the application code to the working directory
12+
COPY . /app/
13+
EXPOSE 8000
14+
# Set the entrypoint for the container
15+
CMD ["hypercorn", "--bind", "0.0.0.0:8000", "api:app"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Instructor Embedding model:
2+
3+
### Purpose :
4+
Model to Create Embeddings from given text using Instructor Large model.
5+
6+
### Testing the model deployment :
7+
To run for testing just the Hugging Face deployment for grievence recognition, you can follow the following steps :
8+
9+
- Git clone the repo
10+
- Go to current folder location i.e. ``` cd src/embeddings/instructor/local ```
11+
- Create docker image file and test the api:
12+
```
13+
docker build -t testmodel .
14+
docker run -p 8000:8000 testmodel
15+
curl -X POST -H "Content-Type: application/json" -d '{"query": "Where is my money? "}' http://localhost:8000/
16+
17+
curl -X POST -F "[email protected]" http://localhost:8000/embeddings/instructor/local -o output.csv
18+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .request import *
2+
from .model import *
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from model import Model
2+
from request import ModelRequest
3+
from quart import Quart, request,Response, send_file
4+
import aiohttp
5+
import pandas as pd
6+
import io
7+
8+
app = Quart(__name__)
9+
10+
model = None
11+
12+
@app.before_serving
13+
async def startup():
14+
app.client = aiohttp.ClientSession()
15+
global model
16+
model = Model(app)
17+
18+
@app.route('/', methods=['POST'])
19+
async def embed():
20+
global model
21+
data = await request.get_json()
22+
files = await request.files # await the coroutine
23+
uploaded_file = files.get('file') # now you can use .get()
24+
25+
if uploaded_file:
26+
df = pd.read_csv(uploaded_file.stream)
27+
req = ModelRequest(df=df) # Pass the DataFrame to ModelRequest
28+
response = await model.inference(req)
29+
df = pd.read_csv(io.StringIO(response)) # Convert the CSV string back to a DataFrame
30+
# Save the DataFrame to a CSV file
31+
df.to_csv('output.csv', index=False)
32+
33+
return await send_file('output.csv', mimetype='text/csv', as_attachment=True, attachment_filename='output.csv')
34+
35+
else:
36+
req = ModelRequest(**data)
37+
return await model.inference(req)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import torch
2+
from request import ModelRequest
3+
from InstructorEmbedding import INSTRUCTOR
4+
import wget
5+
import pandas as pd
6+
import os
7+
8+
class Model():
9+
def __new__(cls, context):
10+
cls.context = context
11+
if not hasattr(cls, 'instance'):
12+
cls.instance = super(Model, cls).__new__(cls)
13+
model_name = "hkunlp/instructor-large"
14+
cls.model = INSTRUCTOR(model_name)
15+
return cls.instance
16+
17+
async def inference(self, request: ModelRequest):
18+
# Modify this function according to model requirements such that inputs and output remains the same
19+
corpus_instruction = "Represent the Wikipedia document for retrieval:"
20+
query_instruction = 'Represent the Wikipedia question for retrieving supporting documents: '
21+
query = request.query
22+
23+
if(query != None):
24+
# print('Query Encoding Process :-')
25+
query_embeddings = self.model.encode(
26+
[[query_instruction, query]],
27+
show_progress_bar=False,
28+
batch_size=32,
29+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30+
)
31+
return query_embeddings.tolist()
32+
33+
if not request.df.empty:
34+
# print('Text corpus Encoding Process :-')
35+
data = request.df
36+
37+
text_corpus = data.loc[:,'content'].to_list()
38+
corpus_embeddings = self.model.encode(
39+
[[corpus_instruction, text] for text in text_corpus],
40+
show_progress_bar=False,
41+
batch_size=32,
42+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
43+
)
44+
data['embeddings'] = corpus_embeddings.tolist()
45+
csv_string = data.to_csv(index=False)
46+
47+
return str(csv_string)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import json
2+
import pandas as pd
3+
4+
5+
class ModelRequest():
6+
def __init__(self, query=None, df = pd.DataFrame()):
7+
# Url to download csv file
8+
self.query = query # String
9+
self.df = df
10+
11+
def to_json(self):
12+
return json.dumps(self, default=lambda o: o.__dict__,
13+
sort_keys=True, indent=4)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
torch
2+
quart
3+
aiohttp
4+
InstructorEmbedding
5+
wget
6+
pandas
7+
tqdm
8+
sentence_transformers

0 commit comments

Comments
 (0)