From f01b8e5ecc297d42078f711a5706759b4aefe330 Mon Sep 17 00:00:00 2001 From: nsde Date: Thu, 24 Aug 2023 14:57:36 +0200 Subject: [PATCH] Resolved some issues --- api/core.py | 4 +++- api/main.py | 5 +++++ api/streaming.py | 32 ++++++++++++++++++-------------- api/transfer.py | 12 +++++++++--- checks/client.py | 2 +- screen.sh | 2 +- 6 files changed, 37 insertions(+), 20 deletions(-) diff --git a/api/core.py b/api/core.py index 58a6490..b8011d3 100644 --- a/api/core.py +++ b/api/core.py @@ -52,6 +52,8 @@ async def get_users(discord_id: int, incoming_request: fastapi.Request): if not user: return await errors.error(404, 'Discord user not found in the API database.', 'Check the `discord_id` parameter.') + print(type(user)) + print(user) return user async def new_user_webhook(user: dict) -> None: @@ -128,7 +130,7 @@ async def run_checks(incoming_request: fastapi.Request): try: chat = await checks.client.test_chat() - except Exception as exc + except Exception as exc: print(exc) chat = None diff --git a/api/main.py b/api/main.py index 85139c4..dbce369 100644 --- a/api/main.py +++ b/api/main.py @@ -1,10 +1,13 @@ """FastAPI setup.""" import fastapi +import pydantic from rich import print from dotenv import load_dotenv +from bson.objectid import ObjectId from fastapi.middleware.cors import CORSMiddleware + import core import transfer @@ -25,6 +28,8 @@ app.include_router(core.router) @app.on_event('startup') async def startup_event(): """Runs when the API starts up.""" + # https://stackoverflow.com/a/74529009 + pydantic.json.ENCODERS_BY_TYPE[ObjectId]=str @app.get('/') async def root(): diff --git a/api/streaming.py b/api/streaming.py index 6077a8d..1b683f6 100644 --- a/api/streaming.py +++ b/api/streaming.py @@ -106,21 +106,21 @@ async def stream( # We haven't done any requests as of right now, everything until now was just preparation # Here, we process the request async with aiohttp.ClientSession(connector=proxies.get_proxy().connector) as session: - try: - async with session.get( - url='https://checkip.amazonaws.com', - timeout=aiohttp.ClientTimeout( - connect=3, - total=float(os.getenv('TRANSFER_TIMEOUT', '5')) - ) - ) as response: - for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '): - if actual_ip in await response.text(): - raise ValueError(f'Proxy {response.text()} is transparent!') + # try: + # async with session.get( + # url='https://checkip.amazonaws.com', + # timeout=aiohttp.ClientTimeout( + # connect=0.4, + # total=0.7 + # ) + # ) as response: + # for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '): + # if actual_ip in await response.text(): + # raise ValueError(f'Proxy {response.text()} is transparent!') - except Exception as exc: - print(f'[!] proxy {proxies.get_proxy()} error - ({type(exc)} {exc})') - continue + # except Exception as exc: + # print(f'[!] proxy {proxies.get_proxy()} error - ({type(exc)} {exc})') + # continue try: async with session.request( @@ -175,6 +175,10 @@ async def stream( print('[!] aiohttp came up with a dumb excuse to not work again ("cOnNeCtIoN rEsEt")') continue + except aiohttp.client_exceptions.ClientConnectionError: + print('[!] aiohttp came up with a dumb excuse to not work again ("cOnNeCtIoN cLosEd")') + continue + if is_chat and is_stream: yield await chat.create_chat_chunk(chat_id=chat_id, model=model, content=chat.CompletionStop) yield 'data: [DONE]\n\n' diff --git a/api/transfer.py b/api/transfer.py index 51d3bc5..b6df9ee 100644 --- a/api/transfer.py +++ b/api/transfer.py @@ -10,10 +10,11 @@ import streaming import moderation from db.users import UserManager -from helpers import tokens, errors +from helpers import tokens, errors, network load_dotenv() +users = UserManager() models_list = json.load(open('models.json', encoding='utf8')) with open('config/config.yml', encoding='utf8') as f: @@ -25,9 +26,11 @@ async def handle(incoming_request): Takes the request from the incoming request to the target endpoint. Checks method, token amount, auth and cost along with if request is NSFW. """ - users = UserManager() path = incoming_request.url.path.replace('v1/v1', 'v1').replace('//', '/') + ip_address = await network.get_ip(incoming_request) + print(f'[{ip_address}] {path}') + if '/models' in path: return fastapi.responses.JSONResponse(content=models_list) @@ -69,7 +72,10 @@ async def handle(incoming_request): policy_violation = await moderation.is_policy_violated(inp) if policy_violation: - return await errors.error(400, f'The request contains content which violates this model\'s policies for "{policy_violation}".', 'We currently don\'t support any NSFW models.') + return await errors.error( + 400, f'The request contains content which violates this model\'s policies for "{policy_violation}".', + 'We currently don\'t support any NSFW models.' + ) role = user.get('role', 'default') diff --git a/checks/client.py b/checks/client.py index 3702fbe..0b07762 100644 --- a/checks/client.py +++ b/checks/client.py @@ -22,7 +22,7 @@ MESSAGES = [ } ] -api_endpoint = 'http://localhost:2332/v1' +api_endpoint = os.getenv('CHECKS_ENDPOINT', 'http://localhost:2332/v1') async def test_server(): """Tests if the API server is running.""" diff --git a/screen.sh b/screen.sh index 3550051..c5db6aa 100755 --- a/screen.sh +++ b/screen.sh @@ -1,7 +1,7 @@ # Script to start the production server # Commit to the production branch -git commit -am "Auto-trigger - Production server started" && git push origin Production +# git commit -am "Auto-trigger - Production server started" && git push origin Production # Copy files to production cp -r * /home/nova-prod