retire xc/ai endpoints
This commit is contained in:
parent
796c8cd913
commit
8958636232
3
base.py
3
base.py
@ -75,11 +75,9 @@ Actionable Routes
|
|||||||
routes: dict = {
|
routes: dict = {
|
||||||
'randmsg': importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants),
|
'randmsg': importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants),
|
||||||
'transcriptions': importlib.import_module("endpoints.transcriptions").Transcriptions(app, util, constants),
|
'transcriptions': importlib.import_module("endpoints.transcriptions").Transcriptions(app, util, constants),
|
||||||
'ai': importlib.import_module("endpoints.ai").AI(app, util, constants),
|
|
||||||
'lyrics': importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants),
|
'lyrics': importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants),
|
||||||
'lastfm': importlib.import_module("endpoints.lastfm").LastFM(app, util, constants),
|
'lastfm': importlib.import_module("endpoints.lastfm").LastFM(app, util, constants),
|
||||||
'yt': importlib.import_module("endpoints.yt").YT(app, util, constants),
|
'yt': importlib.import_module("endpoints.yt").YT(app, util, constants),
|
||||||
# 'xc': importlib.import_module("endpoints.xc").XC(app, util, constants),
|
|
||||||
'karma': importlib.import_module("endpoints.karma").Karma(app, util, constants),
|
'karma': importlib.import_module("endpoints.karma").Karma(app, util, constants),
|
||||||
'radio': importlib.import_module("endpoints.radio").Radio(app, util, constants),
|
'radio': importlib.import_module("endpoints.radio").Radio(app, util, constants),
|
||||||
'mgr': importlib.import_module("endpoints.mgr.mgr_test").Mgr(app, util, constants),
|
'mgr': importlib.import_module("endpoints.mgr.mgr_test").Mgr(app, util, constants),
|
||||||
@ -98,6 +96,7 @@ End Actionable Routes
|
|||||||
"""
|
"""
|
||||||
Startup
|
Startup
|
||||||
"""
|
"""
|
||||||
|
|
||||||
redis = redis_cache.RedisCache()
|
redis = redis_cache.RedisCache()
|
||||||
loop.create_task(
|
loop.create_task(
|
||||||
redis.create_index())
|
redis.create_index())
|
@ -1,89 +0,0 @@
|
|||||||
import logging
|
|
||||||
import regex
|
|
||||||
from regex import Pattern
|
|
||||||
from typing import Union
|
|
||||||
from aiohttp import ClientSession, ClientTimeout
|
|
||||||
from fastapi import FastAPI, Request, HTTPException
|
|
||||||
from fastapi.responses import JSONResponse
|
|
||||||
|
|
||||||
class AI(FastAPI):
|
|
||||||
"""AI Endpoints"""
|
|
||||||
def __init__(self, app: FastAPI,
|
|
||||||
my_util, constants):
|
|
||||||
self.app: FastAPI = app
|
|
||||||
self.util = my_util
|
|
||||||
self.constants = constants
|
|
||||||
self.url_clean_regex: Pattern = regex.compile(r'^\/ai\/(openai|base)\/')
|
|
||||||
self.endpoints: dict = {
|
|
||||||
"ai/openai": self.ai_openai_handler,
|
|
||||||
"ai/base": self.ai_handler,
|
|
||||||
#tbd
|
|
||||||
}
|
|
||||||
|
|
||||||
for endpoint, handler in self.endpoints.items():
|
|
||||||
app.add_api_route(f"/{endpoint}", handler, methods=["GET", "POST"],
|
|
||||||
include_in_schema=False)
|
|
||||||
|
|
||||||
async def ai_handler(self, request: Request) -> JSONResponse:
|
|
||||||
"""
|
|
||||||
/ai/base
|
|
||||||
AI BASE Request
|
|
||||||
(Requires key)
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not self.util.check_key(request.url.path, request.headers.get('X-Authd-With')):
|
|
||||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
|
||||||
|
|
||||||
local_llm_headers = {
|
|
||||||
'Authorization': f'Bearer {self.constants.LOCAL_LLM_KEY}'
|
|
||||||
}
|
|
||||||
|
|
||||||
forward_path = self.url_clean_regex.sub('', request.url.path)
|
|
||||||
try:
|
|
||||||
async with ClientSession() as session:
|
|
||||||
async with await session.post(f'{self.constants.LOCAL_LLM_BASE}/{forward_path}',
|
|
||||||
json=await request.json(),
|
|
||||||
headers=local_llm_headers,
|
|
||||||
timeout=ClientTimeout(connect=15, sock_read=30)) as out_request:
|
|
||||||
response = await out_request.json()
|
|
||||||
return JSONResponse(content=response)
|
|
||||||
except Exception as e:
|
|
||||||
logging.error("Error: %s", e)
|
|
||||||
return JSONResponse(status_code=500, content={
|
|
||||||
'err': True,
|
|
||||||
'errorText': 'General Failure'
|
|
||||||
})
|
|
||||||
|
|
||||||
async def ai_openai_handler(self, request: Request) -> JSONResponse:
|
|
||||||
"""
|
|
||||||
/ai/openai
|
|
||||||
AI Request
|
|
||||||
(Requires key)
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not self.util.check_key(request.url.path, request.headers.get('X-Authd-With')):
|
|
||||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
|
||||||
|
|
||||||
"""
|
|
||||||
TODO: Implement Claude
|
|
||||||
Currently only routes to local LLM
|
|
||||||
"""
|
|
||||||
|
|
||||||
local_llm_headers = {
|
|
||||||
'Authorization': f'Bearer {self.constants.LOCAL_LLM_KEY}'
|
|
||||||
}
|
|
||||||
forward_path = self.url_clean_regex.sub('', request.url.path)
|
|
||||||
try:
|
|
||||||
async with ClientSession() as session:
|
|
||||||
async with await session.post(f'{self.constants.LOCAL_LLM_HOST}/{forward_path}',
|
|
||||||
json=await request.json(),
|
|
||||||
headers=local_llm_headers,
|
|
||||||
timeout=ClientTimeout(connect=15, sock_read=30)) as out_request:
|
|
||||||
response = await out_request.json()
|
|
||||||
return JSONResponse(content=response)
|
|
||||||
except Exception as e:
|
|
||||||
logging.error("Error: %s", e)
|
|
||||||
return JSONResponse(status_code=500, content={
|
|
||||||
'err': True,
|
|
||||||
'errorText': 'General Failure'
|
|
||||||
})
|
|
@ -1,60 +0,0 @@
|
|||||||
import logging
|
|
||||||
from typing import Optional
|
|
||||||
from fastapi import FastAPI, Request, HTTPException
|
|
||||||
from fastapi.responses import JSONResponse
|
|
||||||
from aiohttp import ClientSession, ClientTimeout
|
|
||||||
from .constructors import ValidXCRequest
|
|
||||||
|
|
||||||
class XC(FastAPI):
|
|
||||||
"""XC (CrossComm) Endpoints"""
|
|
||||||
def __init__(self, app: FastAPI, util, constants) -> None:
|
|
||||||
self.app: FastAPI = app
|
|
||||||
self.util = util
|
|
||||||
self.constants = constants
|
|
||||||
|
|
||||||
self.endpoints: dict = {
|
|
||||||
"xc": self.xc_handler,
|
|
||||||
}
|
|
||||||
|
|
||||||
for endpoint, handler in self.endpoints.items():
|
|
||||||
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
|
|
||||||
include_in_schema=False)
|
|
||||||
|
|
||||||
async def xc_handler(self, data: ValidXCRequest,
|
|
||||||
request: Request) -> JSONResponse:
|
|
||||||
"""Handle XC Commands"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
key: str = data.key
|
|
||||||
bid: int = int(data.bid)
|
|
||||||
cmd: str = data.cmd
|
|
||||||
cmd_data: Optional[dict] = data.data
|
|
||||||
if not self.util.check_key(path=request.url.path, req_type=0, key=key):
|
|
||||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
|
||||||
|
|
||||||
BID_ADDR_MAP: dict = {
|
|
||||||
# TODO: add Havoc?
|
|
||||||
}
|
|
||||||
|
|
||||||
if not bid in BID_ADDR_MAP:
|
|
||||||
return JSONResponse(status_code=500, content={
|
|
||||||
'err': True,
|
|
||||||
'errorText': 'Invalid bot id'
|
|
||||||
})
|
|
||||||
|
|
||||||
bot_api_url: str = f'http://{BID_ADDR_MAP[bid]}/'
|
|
||||||
async with ClientSession() as session:
|
|
||||||
async with await session.post(f"{bot_api_url}{cmd}", json=cmd_data, headers={
|
|
||||||
'Content-Type': 'application/json; charset=utf-8'
|
|
||||||
}, timeout=ClientTimeout(connect=5, sock_read=5)) as aiohttp_request:
|
|
||||||
response: dict = await aiohttp_request.json()
|
|
||||||
return JSONResponse(content={
|
|
||||||
'success': True,
|
|
||||||
'response': response
|
|
||||||
})
|
|
||||||
except Exception as e:
|
|
||||||
logging.debug("Error: %s", str(e))
|
|
||||||
return JSONResponse(status_code=500, content={
|
|
||||||
'err': True,
|
|
||||||
'errorText': 'General error.',
|
|
||||||
})
|
|
@ -194,7 +194,7 @@ class RadioUtil:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
# db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
||||||
# WHERE (artist LIKE "%more of myself to kill%" OR artist LIKE "%tethered%") AND (NOT song LIKE "%%stripped%%" AND NOT song LIKE "%(live%%" AND NOT song LIKE "%%acoustic%%" AND NOT song LIKE "%%instrumental%%" AND NOT song LIKE "%%remix%%" AND NOT song LIKE "%%reimagined%%" AND NOT song LIKE "%%alternative%%" AND NOT song LIKE "%%unzipped%%") GROUP BY artistdashsong ORDER BY artist DESC, album DESC, id ASC'
|
# WHERE (artist LIKE "%bayside%") AND (NOT song LIKE "%%stripped%%" AND NOT song LIKE "%(live%%" AND NOT song LIKE "%%acoustic%%" AND NOT song LIKE "%%instrumental%%" AND NOT song LIKE "%%remix%%" AND NOT song LIKE "%%reimagined%%" AND NOT song LIKE "%%alternative%%" AND NOT song LIKE "%%unzipped%%") GROUP BY artistdashsong ORDER BY artist DESC, album DESC, id ASC'
|
||||||
|
|
||||||
async with sqlite3.connect(self.active_playlist_path,
|
async with sqlite3.connect(self.active_playlist_path,
|
||||||
timeout=2) as db_conn:
|
timeout=2) as db_conn:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user