From 46bbefeb0ff492fa3d08465ef4ee7fa1112e1179 Mon Sep 17 00:00:00 2001 From: GautamR-Samagra Date: Thu, 3 Aug 2023 13:58:28 +0000 Subject: [PATCH] Added ai4bharat translate --- .../ai4bharat/remote/Dockerfile | 15 +++++++ .../ai4bharat/remote/README.md | 1 + .../ai4bharat/remote/__init__.py | 2 + src/text_translation/ai4bharat/remote/api.py | 20 +++++++++ .../ai4bharat/remote/model.py | 42 +++++++++++++++++++ .../ai4bharat/remote/request.py | 12 ++++++ .../ai4bharat/remote/requirements.txt | 4 ++ 7 files changed, 96 insertions(+) create mode 100644 src/text_translation/ai4bharat/remote/Dockerfile create mode 100644 src/text_translation/ai4bharat/remote/README.md create mode 100644 src/text_translation/ai4bharat/remote/__init__.py create mode 100644 src/text_translation/ai4bharat/remote/api.py create mode 100644 src/text_translation/ai4bharat/remote/model.py create mode 100644 src/text_translation/ai4bharat/remote/request.py create mode 100644 src/text_translation/ai4bharat/remote/requirements.txt diff --git a/src/text_translation/ai4bharat/remote/Dockerfile b/src/text_translation/ai4bharat/remote/Dockerfile new file mode 100644 index 0000000..7cebb27 --- /dev/null +++ b/src/text_translation/ai4bharat/remote/Dockerfile @@ -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"] + diff --git a/src/text_translation/ai4bharat/remote/README.md b/src/text_translation/ai4bharat/remote/README.md new file mode 100644 index 0000000..e3bbecb --- /dev/null +++ b/src/text_translation/ai4bharat/remote/README.md @@ -0,0 +1 @@ +curl -X POST -H "Content-Type: application/json" -d '{"text": "मेरा पैसा कहाँ है?", "source_language": "hi", "target_language": "en"}' \ No newline at end of file diff --git a/src/text_translation/ai4bharat/remote/__init__.py b/src/text_translation/ai4bharat/remote/__init__.py new file mode 100644 index 0000000..55963ef --- /dev/null +++ b/src/text_translation/ai4bharat/remote/__init__.py @@ -0,0 +1,2 @@ +from .request import ModelRequest +from .request import Model \ No newline at end of file diff --git a/src/text_translation/ai4bharat/remote/api.py b/src/text_translation/ai4bharat/remote/api.py new file mode 100644 index 0000000..37db57c --- /dev/null +++ b/src/text_translation/ai4bharat/remote/api.py @@ -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) diff --git a/src/text_translation/ai4bharat/remote/model.py b/src/text_translation/ai4bharat/remote/model.py new file mode 100644 index 0000000..a6e5af1 --- /dev/null +++ b/src/text_translation/ai4bharat/remote/model.py @@ -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} diff --git a/src/text_translation/ai4bharat/remote/request.py b/src/text_translation/ai4bharat/remote/request.py new file mode 100644 index 0000000..e93ce69 --- /dev/null +++ b/src/text_translation/ai4bharat/remote/request.py @@ -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) diff --git a/src/text_translation/ai4bharat/remote/requirements.txt b/src/text_translation/ai4bharat/remote/requirements.txt new file mode 100644 index 0000000..46626df --- /dev/null +++ b/src/text_translation/ai4bharat/remote/requirements.txt @@ -0,0 +1,4 @@ +aiohttp==3.8.4 +quart==0.18.3 +async-cache==1.1.1 +requests