Skip to content

Commit

Permalink
Merge pull request #230 from GautamR-Samagra/ai4bharat_translation
Browse files Browse the repository at this point in the history
Added ai4bharat translate
  • Loading branch information
Gautam-Rajeev authored Aug 3, 2023
2 parents 5347137 + 46bbefe commit afab9ce
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/text_translation/ai4bharat/remote/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim

WORKDIR /app

#install requirements
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

# Copy the rest of the application code to the working directory
COPY . /app/
EXPOSE 8000
# Set the entrypoint for the container
CMD ["hypercorn", "--bind", "0.0.0.0:8000", "api:app"]

1 change: 1 addition & 0 deletions src/text_translation/ai4bharat/remote/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST -H "Content-Type: application/json" -d '{"text": "मेरा पैसा कहाँ है?", "source_language": "hi", "target_language": "en"}'
2 changes: 2 additions & 0 deletions src/text_translation/ai4bharat/remote/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .request import ModelRequest
from .request import Model
20 changes: 20 additions & 0 deletions src/text_translation/ai4bharat/remote/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from model import Model
from request import ModelRequest
from quart import Quart, request
import aiohttp

#from fastapi import FastAPI, Body
app = Quart(__name__)
#app.client = aiohttp.ClientSession()
#app = FastAPI()

@app.before_serving
async def startup():
app.client = aiohttp.ClientSession()

@app.route('/', methods=['POST'])
async def translate():
data = await request.get_json()
req = ModelRequest(**data)
model = Model(app)
return await model.inference(req)
42 changes: 42 additions & 0 deletions src/text_translation/ai4bharat/remote/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from cache import AsyncTTL
from request import ModelRequest
import json
import requests
import os

authorization_key = os.getenv("AI4BHARAT_KEY")


class Model:
def __new__(cls, context):
cls.context = context
if not hasattr(cls, 'instance'):
cls.instance = super(Model, cls).__new__(cls)
return cls.instance

@AsyncTTL(time_to_live=600000, maxsize=1024)
async def inference(self, request: ModelRequest):

url = "https://api.dhruva.ai4bharat.org/services/inference/translation?serviceId=ai4bharat%2Findictrans-v2-all-gpu--t4"
headers = {
"Content-Type": "application/json",
"authorization": authorization_key
}
payload = {
"config": {
"language": {
"sourceLanguage": request.source_language,
"targetLanguage": request.target_language
}
},
"input": [
{
"source": request.text
}
]
}

response = requests.post(url, headers=headers, json=payload)
resp = response.json()

return {"translated": resp['output'][0]['target'], "success": True}
12 changes: 12 additions & 0 deletions src/text_translation/ai4bharat/remote/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json


class ModelRequest():
def __init__(self, text, source_language, target_language):
self.text = text
self.source_language = source_language
self.target_language = target_language

def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
4 changes: 4 additions & 0 deletions src/text_translation/ai4bharat/remote/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aiohttp==3.8.4
quart==0.18.3
async-cache==1.1.1
requests

0 comments on commit afab9ce

Please sign in to comment.