chore: add CI workflow, build config, and initial project scaffolding for Rbabel v1.0.0

This commit is contained in:
2024-12-03 16:52:00 +00:00
commit ce01cbb078
27 changed files with 310 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
Metadata-Version: 2.1
Name: Rbabel
Version: 1.3.37
Summary: English sentence corrector
Author: Retoor
Author-email: retoor@molodetz.nl
License: MIT
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: aiohttp
Requires-Dist: dataset
Requires-Dist: yura@ git+https://retoor.molodetz.nl/retoor/yura.git@main
+13
View File
@@ -0,0 +1,13 @@
pyproject.toml
setup.cfg
src/Rbabel.egg-info/PKG-INFO
src/Rbabel.egg-info/SOURCES.txt
src/Rbabel.egg-info/dependency_links.txt
src/Rbabel.egg-info/entry_points.txt
src/Rbabel.egg-info/requires.txt
src/Rbabel.egg-info/top_level.txt
src/rbabel/__init__.py
src/rbabel/__main__.py
src/rbabel/app.py
src/rbabel/args.py
src/rbabel/cli.py
+1
View File
@@ -0,0 +1 @@
+3
View File
@@ -0,0 +1,3 @@
[console_scripts]
rbabel = rbabel.cli:main
rbabel.serve = rbabel.__main__:main
+3
View File
@@ -0,0 +1,3 @@
aiohttp
dataset
yura@ git+https://retoor.molodetz.nl/retoor/yura.git@main
+1
View File
@@ -0,0 +1 @@
rbabel
+12
View File
@@ -0,0 +1,12 @@
Metadata-Version: 2.1
Name: rbabel
Version: 1.0.0
Summary: English sentence corrector
Author: Retoor
Author-email: retoor@molodetz.nl
License: MIT
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: aiohttp
Requires-Dist: dataset
Requires-Dist: yura@ git+https://retoor.molodetz.nl/retoor/yura.git@main
+13
View File
@@ -0,0 +1,13 @@
pyproject.toml
setup.cfg
src/rbabel/__init__.py
src/rbabel/__main__.py
src/rbabel/app.py
src/rbabel/args.py
src/rbabel/cli.py
src/rbabel.egg-info/PKG-INFO
src/rbabel.egg-info/SOURCES.txt
src/rbabel.egg-info/dependency_links.txt
src/rbabel.egg-info/entry_points.txt
src/rbabel.egg-info/requires.txt
src/rbabel.egg-info/top_level.txt
+1
View File
@@ -0,0 +1 @@
+4
View File
@@ -0,0 +1,4 @@
[console_scripts]
rbabel = rbabel.cli:main
rbabel.bench = rbabel.cli:cli_bench
rbabel.serve = rbabel.__main__:main
+3
View File
@@ -0,0 +1,3 @@
aiohttp
dataset
yura@ git+https://retoor.molodetz.nl/retoor/yura.git@main
+1
View File
@@ -0,0 +1 @@
rbabel
+9
View File
@@ -0,0 +1,9 @@
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
log = logging.getLogger(__name__)
+13
View File
@@ -0,0 +1,13 @@
from aiohttp import web
from rbabel.app import create_app
from rbabel.args import parse_args
def main():
args = parse_args()
app = create_app(args.url)
web.run_app(app,host=args.host,port=args.port)
if __name__ == '__main__':
nain()
+50
View File
@@ -0,0 +1,50 @@
from aiohttp import web
from yura.client import AsyncClient as LLMClient
from rbabel import log
LLM_NAME = "rbabel"
LLM_CONTEXT = "You are an English grammar corrector. You repsond with only the corrected English of by user given prompt and nothing more. Also replace numbers with the word variant."
LLM_MODEL = "gemma2:latest"
class Application(web.Application):
def __init__(self, llm_url=None, llm_name=LLM_NAME, llm_model=LLM_MODEL,llm_context=LLM_CONTEXT, *args, **kwargs):
self.llm_url = llm_url
self.llm_name = llm_name
self.llm_model = LLM_MODEL
self.llm_context = llm_context
super().__init__(*args, **kwargs)
self.on_startup.append(self.create_llm)
self.router.add_post("/", self.optimize_grammar_handler)
async def create_llm(self, app):
llm_client = LLMClient(self.llm_url)
log.info("Creating LLM named {}.".format(self.llm_name))
success = await llm_client.create(self.llm_name, self.llm_model, self.llm_context)
assert(success)
log.info("LLM {} created.".format(self.llm_name))
await llm_client.close()
async def fix_grammar(self, content):
corrected_content = []
llm_client = LLMClient(self.llm_url)
token = await llm_client.connect(self.llm_name)
async for chunk in llm_client.chat(token, content):
corrected_content.append(chunk['content'])
await llm_client.close()
return "".join(corrected_content)
async def optimize_grammar_handler(self,request):
text = await request.json()
assert(type(text) ==str)
corrected = await self.fix_grammar(text)
print("was:",text)
print("became:",corrected)
return web.json_response(corrected,content_type="application/json")
def create_app(llm_url):
return Application(llm_url, LLM_NAME, LLM_MODEL, LLM_CONTEXT)
+35
View File
@@ -0,0 +1,35 @@
import argparse
def parse_args():
parser = argparse.ArgumentParser(
description="AI enabled English corrector. Only minor improvements."
)
parser.add_argument(
'--url',
type=str,
required=False,
default="http://127.0.0.1:3011/",
help='URL of Katya LLM Server.'
)
parser.add_argument(
'--host',
type=str,
default="127.0.0.1",
required=False,
help='Host to bind to.'
)
parser.add_argument(
'--port',
type=int,
default=3011,
required=False,
help='Port to bind to.'
)
return parser.parse_args()
+50
View File
@@ -0,0 +1,50 @@
import asyncio
from aiohttp import ClientSession
from rbabel.args import parse_args
import time
async def cli_client(url):
while True:
sentence = input("> ")
async with ClientSession() as session:
async with session.post("http://localhost:8080",json=sentence) as response:
try:
print(await response.json())
except Exception as ex:
print(ex)
print(await response.text())
async def bench(url):
index = 0
while True:
index += 1
sentence = "I bougt {} woden shoe".format(index)
time_start = time.time()
async with ClientSession() as session:
async with session.post(url,json=sentence) as response:
try:
print(await response.json())
except Exception as ex:
print(ex)
print(await response.text())
time_end = time.time()
print("Duration: {}".format(time_end-time_start))
def cli_bench():
args = parse_args()
asyncio.run(bench(args.url))
def main():
args = parse_args()
asyncio.run(cli_client(args.url))
if __name__ == '__main__':
main()