Skip to content

Commit 695419d

Browse files
Merge pull request #296 from Samagra-Development/rerank
added bge reranker
2 parents 7a21229 + 260025a commit 695419d

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed
+15
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"]
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Grievance classification:
2+
3+
4+
### Purpose :
5+
Re rank given a question and a list of contetn
6+
7+
8+
### Testing the model deployment :
9+
To run for testing just the Hugging Face deployment for grievence recognition, you can follow the following steps :
10+
11+
- Git clone the repo
12+
- Go to current folder location i.e. ``` cd /src/rerankers/bge_base/local ```
13+
- Create docker image file and test the api:
14+
```
15+
docker build -t testmodel .
16+
docker run -p 8000:8000 testmodel
17+
curl -X POST -H "Content-Type: application/json" \
18+
-d '{"question": "What is agriculture ?", "content_chunks": ["Farming is a practice of growing crops to sell them to generate money", "LLM are the present day hype machine but will they be useful until you can truly reason with them? ", "Things are generally better than what people deep into it feel"]}' \
19+
http://localhost:8000/
20+
```
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .request import ModelRequest
2+
from .request import Model

src/rerankers/bge_base/local/api.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from model import Model
2+
from request import ModelRequest
3+
from quart import Quart, request,jsonify
4+
import aiohttp
5+
6+
app = Quart(__name__)
7+
8+
model = None
9+
10+
@app.before_serving
11+
async def startup():
12+
app.client = aiohttp.ClientSession()
13+
global model
14+
model = Model(app)
15+
16+
@app.route('/', methods=['POST'])
17+
async def embed():
18+
global model
19+
data = await request.get_json()
20+
req = ModelRequest(**data)
21+
prediction = await model.inference(req)
22+
# Convert the NumPy array to a list (or another serializable format) and return as JSON
23+
if prediction is not None:
24+
return jsonify(prediction.tolist()) # Assuming 'prediction' is a NumPy array
25+
else:
26+
# Return a meaningful error message if prediction is None
27+
return jsonify({'error': 'Prediction failed'}), 500
28+
29+
if __name__ == "__main__":
30+
app.run()

src/rerankers/bge_base/local/model.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from request import ModelRequest
2+
from sentence_transformers.cross_encoder import CrossEncoder
3+
import torch
4+
5+
class Model():
6+
def __new__(cls, context):
7+
cls.context = context
8+
if not hasattr(cls, 'instance'):
9+
cls.instance = super(Model, cls).__new__(cls)
10+
model_name = "BAAI/bge-reranker-base"
11+
cls.model = CrossEncoder(model_name)
12+
return cls.instance
13+
14+
15+
async def inference(self, request: ModelRequest):
16+
predict_array = request.predict_array
17+
predictions = self.model.predict(predict_array)
18+
return (predictions)
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import requests
2+
import json
3+
4+
class ModelRequest():
5+
def __init__(self, question, content_chunks):
6+
self.question = question
7+
self.content_chunks = content_chunks
8+
self.predict_array = [[question, content] for content in content_chunks]
9+
10+
def to_json(self):
11+
return json.dumps(self, default=lambda o: o.__dict__,
12+
sort_keys=True, indent=4)
13+
14+
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
torch
2+
sentence_transformers
3+
quart
4+
aiohttp

0 commit comments

Comments
 (0)