-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from GautamR-Samagra/ai4bharat_translation
Added ai4bharat translate
- Loading branch information
Showing
7 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .request import ModelRequest | ||
from .request import Model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |