Admin and start handlers work. Added testing txt2img from first bot version (will be replaced soon)
This commit is contained in:
1
bot/modules/api/__init__.py
Normal file
1
bot/modules/api/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
31
bot/modules/api/models.py
Normal file
31
bot/modules/api/models.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import aiohttp
|
||||
from bot.db import db, DBTables
|
||||
from rich import print
|
||||
|
||||
|
||||
async def get_models():
|
||||
endpoint = db[DBTables.config].get('endpoint')
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
r = await session.get(endpoint + "/sdapi/v1/sd-models")
|
||||
if r.status != 200:
|
||||
return None
|
||||
return [x["title"] for x in await r.json()]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return None
|
||||
|
||||
|
||||
async def set_model(model_name: str):
|
||||
endpoint = db[DBTables.config].get('endpoint')
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
r = await session.post(endpoint + "/sdapi/v1/options", json={
|
||||
"sd_model_checkpoint": model_name
|
||||
})
|
||||
if r.status != 200:
|
||||
return False
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
32
bot/modules/api/txt2img.py
Normal file
32
bot/modules/api/txt2img.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import aiohttp
|
||||
from bot.db import db, DBTables
|
||||
import json
|
||||
import base64
|
||||
|
||||
|
||||
async def txt2img(prompt: str, negative_prompt: str = None, steps: int = 20,
|
||||
cfg_scale: int = 7, width: int = 768, height: int = 768,
|
||||
restore_faces: bool = True, sampler: str = "Euler a") -> list[bytes, dict] | None:
|
||||
endpoint = db[DBTables.config].get('endpoint')
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
r = await session.post(
|
||||
endpoint + "/sdapi/v1/txt2img",
|
||||
json={
|
||||
"prompt": prompt,
|
||||
"steps": steps,
|
||||
"cfg_scale": cfg_scale,
|
||||
"width": width,
|
||||
"height": height,
|
||||
"restore_faces": restore_faces,
|
||||
"negative_prompt": negative_prompt,
|
||||
"sampler_index": sampler
|
||||
}
|
||||
)
|
||||
if r.status != 200:
|
||||
return None
|
||||
return [base64.b64decode((await r.json())["images"][0]),
|
||||
json.loads((await r.json())["info"])]
|
||||
except Exception as e:
|
||||
assert e
|
||||
return None
|
||||
Reference in New Issue
Block a user