From 7818c6cb6512206e225a0f86ab3bd18cf2f75dcc Mon Sep 17 00:00:00 2001 From: db0 Date: Wed, 21 Jun 2023 19:37:34 +0200 Subject: [PATCH] Initial working DB instance --- overseer/apis/models/v1.py | 16 +- overseer/apis/v1/__init__.py | 3 +- overseer/apis/v1/base.py | 35 ++- overseer/classes/__init__.py | 31 +++ overseer/classes/instance.py | 68 +++++ overseer/classes/news.py | 449 +++++++++++++++++++++++++++++++++ overseer/database/__init__.py | 0 overseer/database/functions.py | 12 + overseer/flask.py | 4 +- overseer/utils.py | 103 ++++++++ 10 files changed, 715 insertions(+), 6 deletions(-) create mode 100644 overseer/classes/__init__.py create mode 100644 overseer/classes/instance.py create mode 100644 overseer/classes/news.py create mode 100644 overseer/database/__init__.py create mode 100644 overseer/database/functions.py create mode 100644 overseer/utils.py diff --git a/overseer/apis/models/v1.py b/overseer/apis/models/v1.py index 8aa5520..1c20b60 100644 --- a/overseer/apis/models/v1.py +++ b/overseer/apis/models/v1.py @@ -14,9 +14,23 @@ class Models: 'active_users_monthly': fields.Integer(description="The amount of active users monthly."), 'signup': fields.Boolean(default=False,description="True when subscriptions are open, else False"), 'activity_suspicion': fields.Float(description="Local Comments+Posts per User. Higher is worse"), + 'activity_suspicion': fields.Float(description="Local Comments+Posts per User. Higher is worse"), }) - self.response_model_model_SusInstances_get = api.model('SuspiciousInstancesDomainList', { + self.response_model_model_Suspicions_get = api.model('SuspiciousInstances', { 'instances': fields.List(fields.Nested(self.response_model_suspicious_instances)), 'domains': fields.List(fields.String(description="The suspicious domains as a list.")), 'csv': fields.String(description="The suspicious domains as a csv."), }) + self.response_model_instances = api.model('InstanceDetails', { + 'domain': fields.String(description="The instance domain"), + 'open_registrations': fields.Boolean(description="The instance uptime pct. 100% and thousand of users is unlikely"), + 'email_verify': fields.Boolean(description="The amount of local posts in that instance"), + 'approvals': fields.Integer(description="The amount of endorsements this instance has given out"), + 'endorsements': fields.Integer(description="The amount of endorsements this instance has received"), + 'guarantor': fields.String(description="The domain of the instance which guaranteed this instance."), + }) + self.response_model_model_Instances_get = api.model('Instances', { + 'instances': fields.List(fields.Nested(self.response_model_instances)), + 'domains': fields.List(fields.String(description="The instance domains as a list.")), + 'csv': fields.String(description="The instance domains as a csv."), + }) diff --git a/overseer/apis/v1/__init__.py b/overseer/apis/v1/__init__.py index 02c3678..7bb35b8 100644 --- a/overseer/apis/v1/__init__.py +++ b/overseer/apis/v1/__init__.py @@ -1,4 +1,5 @@ import overseer.apis.v1.base as base from overseer.apis.v1.base import api -api.add_resource(base.SusInstances, "/instances") +api.add_resource(base.Suspicions, "/suspicions") +api.add_resource(base.Instances, "/instances") diff --git a/overseer/apis/v1/base.py b/overseer/apis/v1/base.py index 84a3354..f0c5de6 100644 --- a/overseer/apis/v1/base.py +++ b/overseer/apis/v1/base.py @@ -3,6 +3,8 @@ from flask_restx import Namespace, Resource, reqparse from overseer.flask import cache from overseer.observer import retrieve_suspicious_instances from loguru import logger +from overseer.classes.instance import Instance +from overseer.database import functions as database api = Namespace('v1', 'API Version 1' ) @@ -16,7 +18,7 @@ def get_request_path(): return f"{request.remote_addr}@{request.method}@{request.path}" -class SusInstances(Resource): +class Suspicions(Resource): get_parser = reqparse.RequestParser() get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") get_parser.add_argument("activity_suspicion", required=False, default=20, type=int, help="How many users per local post+comment to consider suspicious", location="args") @@ -26,7 +28,7 @@ class SusInstances(Resource): @api.expect(get_parser) @logger.catch(reraise=True) @cache.cached(timeout=10, query_string=True) - @api.marshal_with(models.response_model_model_SusInstances_get, code=200, description='Suspicious Instances', skip_none=True) + @api.marshal_with(models.response_model_model_Suspicions_get, code=200, description='Suspicious Instances', skip_none=True) def get(self): '''A List with the details of all suspicious instances ''' @@ -37,3 +39,32 @@ class SusInstances(Resource): if self.args.domains: return {"domains": [instance["domain"] for instance in sus_instances]},200 return {"instances": sus_instances},200 + + +class Instances(Resource): + get_parser = reqparse.RequestParser() + get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") + get_parser.add_argument("endorsements", required=False, default=1, type=int, help="Limit to this amount of endorsements of more", location="args") + get_parser.add_argument("domain", required=False, type=str, help="Filter by instance domain", location="args") + get_parser.add_argument("csv", required=False, type=bool, help="Set to true to return just the domains as a csv. Mutually exclusive with domains", location="args") + get_parser.add_argument("domains", required=False, type=bool, help="Set to true to return just the domains as a list. Mutually exclusive with csv", location="args") + + @api.expect(get_parser) + @logger.catch(reraise=True) + @cache.cached(timeout=10, query_string=True) + @api.marshal_with(models.response_model_model_Instances_get, code=200, description='Instances', skip_none=True) + def get(self): + '''A List with the details of all instances and their endorsements + ''' + self.args = self.get_parser.parse_args() + instance_details = [] + for instance in database.get_all_instances(): + logger.debug(instance) + instance_details.append(instance.get_details()) + if self.args.csv: + return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + if self.args.domains: + return {"domains": [instance["domain"] for instance in instance_details]},200 + logger.debug(instance_details) + return {"instances": instance_details},200 + diff --git a/overseer/classes/__init__.py b/overseer/classes/__init__.py new file mode 100644 index 0000000..d8ab436 --- /dev/null +++ b/overseer/classes/__init__.py @@ -0,0 +1,31 @@ +import os +from loguru import logger +from overseer.argparser import args +from importlib import import_module +from overseer.flask import db, OVERSEER +from overseer.utils import hash_api_key + +# Importing for DB creation +from overseer.classes.instance import Instance, Guarantee + +with OVERSEER.app_context(): + + db.create_all() + + admin_domain = os.getenv("ADMIN_DOMAIN") + admin = db.session.query(Instance).filter_by(domain=admin_domain).first() + if not admin: + admin = Instance( + id=0, + domain=admin_domain, + api_key=hash_api_key(os.getenv("ADMIN_PASSWORD")), + open_registrations=False, + email_verify=False, + ) + admin.create() + guarantee = Guarantee( + guarantor_id = admin.id, + guaranteed_id = admin.id, + ) + db.session.add(guarantee) + db.session.commit() \ No newline at end of file diff --git a/overseer/classes/instance.py b/overseer/classes/instance.py new file mode 100644 index 0000000..853aaf5 --- /dev/null +++ b/overseer/classes/instance.py @@ -0,0 +1,68 @@ +import uuid +import os + +import dateutil.relativedelta +from datetime import datetime +from sqlalchemy import Enum, UniqueConstraint +from sqlalchemy.dialects.postgresql import UUID + +from loguru import logger +from overseer.flask import db, SQLITE_MODE + +uuid_column_type = lambda: UUID(as_uuid=True) if not SQLITE_MODE else db.String(36) + + +class Guarantee(db.Model): + __tablename__ = "guarantees" + id = db.Column(db.Integer, primary_key=True) + guarantor_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False) + guarantor_instance = db.relationship("Instance", back_populates="guarantees", foreign_keys=[guarantor_id]) + guaranteed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), unique=True, nullable=False) + guaranteed_instance = db.relationship("Instance", back_populates="guarantors", foreign_keys=[guaranteed_id]) + + +class Endorsement(db.Model): + __tablename__ = "endorsements" + __table_args__ = (UniqueConstraint('approving_id', 'endorsed_id', name='endoresements_approving_id_endorsed_id'),) + id = db.Column(db.Integer, primary_key=True) + approving_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False) + approving_instance = db.relationship("Instance", back_populates="approvals", foreign_keys=[approving_id]) + endorsed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False) + endorsed_instance = db.relationship("Instance", back_populates="endorsements", foreign_keys=[endorsed_id]) + + +class Instance(db.Model): + __tablename__ = "instances" + + id = db.Column(db.Integer, primary_key=True) + domain = db.Column(db.String(255), unique=True, nullable=False) + api_key = db.Column(db.String(100), unique=True, nullable=False, index=True) + created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + updated = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + + open_registrations = db.Column(db.Boolean, unique=False, nullable=False, index=True) + email_verify = db.Column(db.Boolean, unique=False, nullable=False, index=True) + + approvals = db.relationship("Endorsement", back_populates="approving_instance", cascade="all, delete-orphan", foreign_keys=[Endorsement.approving_id]) + endorsements = db.relationship("Endorsement", back_populates="endorsed_instance", cascade="all, delete-orphan", foreign_keys=[Endorsement.endorsed_id]) + guarantees = db.relationship("Guarantee", back_populates="guarantor_instance", cascade="all, delete-orphan", foreign_keys=[Guarantee.guarantor_id]) + guarantors = db.relationship("Guarantee", back_populates="guaranteed_instance", cascade="all, delete-orphan", foreign_keys=[Guarantee.guaranteed_id]) + + def create(self): + db.session.add(self) + db.session.commit() + + def get_details(self): + ret_dict = { + "domain": self.domain, + "open_registrations": self.open_registrations, + "email_verify": self.email_verify, + "endorsements": len(self.endorsements), + "approvals": len(self.approvals), + "guarantor": self.get_guarantor().domain, + } + return ret_dict + + def get_guarantor(self): + guarantee = self.guarantors[0] + return Instance.query.filter_by(id=guarantee.guarantor_id).first() diff --git a/overseer/classes/news.py b/overseer/classes/news.py new file mode 100644 index 0000000..6d6044e --- /dev/null +++ b/overseer/classes/news.py @@ -0,0 +1,449 @@ +from datetime import datetime + +class News: + + HORDE_NEWS = [ + { + "date_published": "2023-06-01", + "newspiece": + "LoRas support has now been merged into the main worker branch! " + "All kudos to [Jug](https://github.com/jug-dev/) and [Tazlin](https://github.com/tazlin/) for their invaluable efforts! " + "Read the [devlog](https://dbzer0.com/blog/the-ai-horde-now-seamlessly-provides-all-civitai-loras/)", + "tags": ["devlog", "lora", "text2img"], + "importance": "Workers", + }, + { + "date_published": "2023-05-30", + "newspiece": + "Early support for LoRa has been added to the AI Horde with a few workers providing it. " + "UIs are still adding it, with [Lucid Creations](https://dbzer0.itch.io/lucid-creations/devlog/537949/1170-loras), ArtBot and the Krita plugin already supporting it." + "Try it out and let us know how it works for you.", + "tags": ["UI", "lora", "text2img"], + "importance": "Information", + }, + { + "date_published": "2023-05-25", + "newspiece": + "I wanted to point out a very cool voice-2-text-2-voice AI Horde integration: [ProtoReplicant](https://github.com/OpenReplicant/ProtoReplicant). " + "It converts your voice into text which it then sends to an LLM model, and finally converts the resulting text into voice and plays it back." + "Here's the new [Discord integration channel](https://discordapp.com/channels/781145214752129095/1111189841120596008)", + "tags": ["UI", "voice", "llm"], + "importance": "Integration", + }, + { + "date_published": "2023-05-22", + "newspiece": + "A new AI Horde integration has been created. A Telegram bot by the name of [Imaginarium](https://t.me/ImaginariumAIbot). " + "Here's the new [Discord integration channel](https://discordapp.com/channels/781145214752129095/1109825249933000714)", + "tags": ["bot", "telegram"], + "importance": "Integration", + }, + { + "date_published": "2023-05-14", + "newspiece": + "The AI Horde has finally moved to the [hordelib](https://pypi.org/project/hordelib/) library. " + "Which is powered by the [ComfyUI](https://github.com/comfyanonymous/ComfyUI) inference backend. " + "[Read the Devlog](https://dbzer0.com/blog/the-ai-horde-worker-moves-to-a-completely-new-inference-backend/)!", + "tags": ["devlog", "backend", "Jug", "Tazlin", "dreamer", "alchemist"], + "importance": "Information", + }, + { + "date_published": "2023-05-11", + "newspiece": + "With the upcoming deployment of the [hordelib](https://pypi.org/project/hordelib/)-based worker. " + "[Jug](https://github.com/jug-dev/) has looked into creating a more efficient model to determine generation kudos " + "instead of reusing the numbers I hallucinated one day. " + "He used what we know best and we trained an explicit model to calculate kudos, based on the performance of his own GPU on the comfy branch " + "This new calculation should be much more accurate in terms of things like controlnet and resolution impact. " + "The good news is that the new comfy branch this seems to reduce kudos costs for high resolutions accross the board. " + "Note: Due to the current worker (based on nataili) being slightly lower quality at the benefit of speed, and thus getting a boost due to the new kudos model, " + "we have implemented a 25% reduction for its rewards to bring it up to line with its actual performance.", + "tags": ["kudos", "dreamer", "Jug"], + "importance": "Workers", + }, + { + "date_published": "2023-05-09", + "newspiece": + "A new feature appeared on the Horde. " + "You can now create [API keys you can share with others](https://dbzer0.com/blog/key-sharing/) to use your own priority.", + "tags": ["apikey", "shared key"], + "importance": "Information", + }, + { + "date_published": "2023-05-05", + "newspiece": + "You can now run an AI Horde worker inside a docker container. " + "http://ghcr.io/db0/ai-horde-worker:main " + "Our README [contains information on how to configure it](https://github.com/db0/AI-Horde-Worker/blob/main/README.md#docker) " + "All kudos to [Gus Puffy#8887](https://github.com/guspuffygit)", + "tags": ["docker", "dreamer"], + "importance": "Workers", + }, + { + "date_published": "2023-04-23", + "newspiece": + "The Command Line Interface for the AI Horde has now been extended to support Image Generation, Text Generation and Image Alchemy. " + "It has been split into three files and is now available in its own repository: " + "https://github.com/db0/AI-Horde-CLI", + "tags": ["cli"], + "importance": "Information", + }, + { + "date_published": "2023-04-16", + "newspiece": + "The AI Horde has received its first patreon sponsorship " + "Many thanks to [pawkygame VR](https://discord.gg/Zbe63QTU9X) for their support!", + "tags": ["sponsor", "patreon"], + "importance": "Information", + }, + { + "date_published": "2023-03-23", + "newspiece": + "Inpainting is re-enabled that to the work of [ResidentChief](https://github.com/ResidentChief)! " + "Now also have support for multiple inpainting models.", + "tags": ["inpainting", "ResidentChief"], + "importance": "Information", + }, + { + "date_published": "2023-03-19", + "newspiece": + "The AI Horde Interrogator Worker has now been renamed to 'Alchemist' " + "The Horde alchemist can now run all the post-processors, along with all the interrogation forms. " + "This means that if you have an existing image you wish to face-fix or upscale, you can just do that " + "by requesting it via alchemy. " + "For now, the alchemist does not support extracting ControlNet intermediate images, but this will be coming soon. " + "The endpoints remain as `api/v2interrogation/` for now but I plan to rename them in v3.", + "tags": ["upscale", "post-processing", "alchemy"], + "importance": "Information", + }, + { + "date_published": "2023-03-15", + "newspiece": + "the AI Horde now supports the DDIM sampler and the RealESRGAN_x4plus_anime_6B upscaler! " + "Keep in mind that you cannot use two upscalers at the same time. " + "All kudos to [ResidentChief](https://github.com/ResidentChief)!", + "tags": ["upscale", "post-processing", "ResidentChief", "samplers"], + "importance": "Information", + }, + { + "date_published": "2023-03-13", + "newspiece": + "A new option `replacement_filter` is available for image generations. " + "When set to True and a potential CSAM prompt is detected, " + "all underage context will be transparently replaced or removed " + "and some extra negative prompts will be added to the negative prompt." + "When set to False (default) or the prompt size is over 500 chars " + "The previous behaviour will be used, where the prompt is rejected and an IP timeout will be put in place. " + "This feature should make sending text generations to be turned into images a less frustrating experience.", + "tags": ["csam", "text2text", "text2img"], + "importance": "Information", + }, + { + "date_published": "2023-03-10", + "newspiece": "We now have an AI-driven anti-CSAM filter as well. Read about it on [the main developer's blog](https://dbzer0.com/blog/ai-powered-anti-csam-filter-for-stable-diffusion/).", + "tags": ["csam"], + "importance": "Information", + }, + { + "date_published": "2023-03-03", + "newspiece": "The Horde Ratings are back in action. Go to your typical UI and rate away!", + "tags": ["ratings"], + "importance": "Information", + }, + { + "date_published": "2023-02-23", + "newspiece": "KoboldAI Horde has been merged into Stable Horde as a unified AI Horde!", + "tags": ["text2text", "ai horde"], + "importance": "Information", + }, + { + "date_published": "2023-02-21", + "newspiece": ( + 'The Horde now supports ControlNet on all models! All kudos go to [hlky](https://github.com/hlky) who again weaved the dark magic!' + ), + "tags": ["controlnet", "img2img", "hlky"], + "importance": "Information" + }, + { + "date_published": "2023-02-14", + "newspiece": ( + 'You can now use an almost unlimited prompt size thanks to the work of ResidentChief!' + ), + "tags": ["text2img", "img2img", "ResidentChief"], + "importance": "Information" + }, + { + "date_published": "2023-02-09", + "newspiece": ( + 'You can now select to generate a higher-sized image using hires_fix, which uses the composition of stable diffusion at 512x512 which tends to be more consistent.' + ), + "tags": ["text2img", "img2img", "ResidentChief"], + "importance": "Information" + }, + { + "date_published": "2023-02-03", + "newspiece": ( + 'The horde now supports pix2pix. All you have to do is use img2img as normal and select the pix2pix model!' + ), + "tags": ["img2img", "ResidentChief"], + "importance": "Information" + }, + { + "date_published": "2023-01-24", + "newspiece": ( + 'We now support sending tiling requests! Send `"tiling":true` into your payload params to request an image that seamlessly tiles.' + ), + "tags": ["text2img", "img2img", "ResidentChief"], + "importance": "Information" + }, + { + "date_published": "2023-01-23", + "newspiece": ( + "I have tightened the rules around NSFW models. As they seem to be straying into 'unethical' territory even when not explicitly prompted, " + "I am forced to tighten the safety controls around them. From now on, otherwise generic terms for young people like `girl` ,`boy` etc " + "Cannot be used on those models. Please either use terms like `woman` or `man` or switch to a non-NSFW model instead." + ), + "tags": ["countermeasures", "nsfw"], + "importance": "Information" + }, + { + "date_published": "2023-01-23", + "newspiece": ( + "The horde now has a [Blender Plugin](https://github.com/benrugg/AI-Render)!" + ), + "tags": ["plugin", "blender"], + "importance": "Information" + }, + { + "date_published": "2023-01-18", + "newspiece": ( + "We now have a [New Discord Bot](https://github.com/ZeldaFan0225/Stable_Horde_Discord), courtesy of Zelda_Fan#0225. Check out [their other bot](https://slashbot.de/) as well! " + "Only downside is that if you were already logged in to the old bot, you will need to /login again." + ), + "importance": "Information" + }, + { + "date_published": "2023-01-18", + "newspiece": ( + "The prompts now support weights! Use them like so `(sub prompt:1.1)` where 1.1 corresponds to +10% weight " + "You can tweak upwards more like `1.25` or downwards like `0.7`, but don't go above +=30%" + ), + "importance": "Information" + }, + { + "date_published": "2023-01-12", + "newspiece": ( + "We plan to be replacing our official discord bot with [new a new codebase](https://github.com/ZeldaFan0225/Stable_Horde_Discord) based on the work of Zelda_Fan#0225. " + "Once we do, be aware that the controls will be slightly different and you will have to log-in again with your API key." + ), + "importance": "Upcoming" + }, + { + "date_published": "2023-01-11", + "newspiece": ( + "The Stable Horde has its first browser extension! " + "[GenAlt](https://chrome.google.com/webstore/detail/genalt-generated-alt-text/ekbmkapnmnhhgfmjdnchgmcfggibebnn) is an accessibility plugin to help people with bad eyesight always find alt text for images." + "The extension relies on the Stable Horde's newly added image interrogation capabilities to generate captions which are then serves as the image's alt text." + ), + "importance": "Information" + }, + { + "date_published": "2023-01-04", + "newspiece": "We are proud to announce that we have [initiated a collaboration with LAION](https://dbzer0.com/blog/a-collaboration-begins-between-stable-horde-and-laion/) to help them improve their dataset!", + "importance": "Information" + }, + { + "date_published": "2023-01-06", + "newspiece": ( + "The amount of kudos consumed when generating images [has been slightly adjusted](https://dbzer0.com/blog/sharing-is-caring/). " + "To simulate the resource costs of the horde, each image generation request will now burn +3 kudos. Those will not go to the generating worker! " + "However we also have a new opt-in feature: You can choose to share your text2img generations with [LAION](https://laion.ai/). " + "If you do, this added cost will be just +1 kudos. " + "We have also updated our Terms of Service to make this more obvious." + ), + "importance": "Information" + }, + { + "date_published": "2023-01-05", + "newspiece": "[Worker now have a WebUI](https://dbzer0.com/blog/the-ai-horde-worker-has-a-control-ui/) which they can use to configure themselves. Use it by running `worker-webui.sh/cmd`", + "importance": "Workers" + }, + { + "date_published": "2023-01-04", + "newspiece": "[You can now interrogate images](https://dbzer0.com/blog/image-interrogations-are-now-available-on-the-stable-horde/) (AKA img2txt) to retrieve information about them such as captions and whether they are NSFW. Check the api/v2/interrogate endpoint documentation.", + "importance": "Information" + }, + { + "date_published": "2023-01-01", + "newspiece": "Stable Horde can now be used on the automatic1111 Web UI via [an external script](https://github.com/natanjunges/stable-diffusion-webui-stable-horde)", + "importance": "Information" + }, + { + "date_published": "2022-12-30", + "newspiece": "Stable Horde now supports depth2img! To use it you need to send a source image and select the `Stable Difffusion 2 Depth` model", + "importance": "Information" + }, + { + "date_published": "2022-12-28", + "newspiece": "Stable Horde workers can now opt-in to loading post-processors. Check your bridge_data.py for options. This should help workers who started being more unstable due to the PP requirements.", + "importance": "Workers" + }, + { + "date_published": "2022-12-24", + "newspiece": "Stable Horde has now support for [CodeFormer](https://shangchenzhou.com/projects/CodeFormer/). Simply use 'CodeFormers' for your postprocessor (case sensitive). This will fix any faces in the image. Be aware that due to the processing cost of this model, the kudos requirement will be 50% higher! Note: The inbuilt upscaler has been disabled", + "importance": "Information" + }, + { + "date_published": "2022-12-08", + "newspiece": "The Stable Horde workers now support dynamically swapping models. This means that models will always switch to support the most in demand models every minute, allowing us to support demand much better!", + "importance": "Information" + }, + { + "date_published": "2022-11-28", + "newspiece": "The Horde has undertaken a massive code refactoring to allow me to move to a proper SQL DB. This will finally allow me to scale the frontend systems horizontally and allow for way more capacity!", + "importance": "Information" + }, + { + "date_published": "2022-11-24", + "newspiece": "Due to the massive increase in demand from the Horde, we have to limit the amount of concurrent anonymous requests we can serve. We will revert this once our infrastructure can scale better.", + "importance": "Crisis" + }, + { + "date_published": "2022-11-24", + "newspiece": "Stable Diffusion 2.0 has been released and now it is available on the Horde as well.", + "importance": "Information" + }, + { + "date_published": "2022-11-22", + "newspiece": "A new Stable Horde Bot has been deployed, this time for Mastodon. You can find [the stablehorde_generator}(https://sigmoid.social/@stablehorde_generator) as well as our [official Stable Horde account](https://sigmoid.social/@stablehorde) on sigmoid.social", + "importance": "Information" + }, + { + "date_published": "2022-11-22", + "newspiece": "We now have [support for the Unreal Engine](https://github.com/Mystfit/Unreal-StableDiffusionTools/releases/tag/v0.5.0) via a community-provided plugin", + "importance": "Information" + }, + { + "date_published": "2022-11-18", + "newspiece": "The stable horde [now supports post-processing](https://www.patreon.com/posts/post-processing-74815675) on images automatically", + "importance": "Information" + }, + { + "date_published": "2022-11-05", + "newspiece": "Due to suddenly increased demand, we have adjusted how much requests accounts can request before needing to have the kudos upfront. More than 50 steps will require kudos and the max resolution will be adjusted based on the current horde demand.", + "importance": "Information" + }, + { + "date_published": "2022-11-05", + "newspiece": "Workers can now [join teams](https://www.patreon.com/posts/teams-74247978) to get aggregated stats.", + "importance": "Information" + }, + { + "date_published": "2022-11-02", + "newspiece": "The horde can now generate images up to 3072x3072 and 500 steps! However you need to already have the kudos to burn to do so!", + "importance": "Information" + }, + { + "date_published": "2022-10-29", + "newspiece": "Inpainting is now available on the stable horde! Many kudos to [blueturtle](https://github.com/blueturtleai) for the support!", + "importance": "Information" + }, + { + "date_published": "2022-10-25", + "newspiece": "Another [Discord Bot for Stable Horde integration](https://github.com/ZeldaFan0225/Stable_Horde_Discord) has appeared!", + "importance": "Information" + }, + { + "date_published": "2022-10-24", + "newspiece": "The Stable Horde Client has been renamed to [Lucid Creations](https://dbzer0.itch.io/lucid-creations) and has a new version and UI out which supports multiple models and img2img!", + "importance": "Information" + }, + { + "date_published": "2022-10-22", + "newspiece": "We have [a new npm SDK](https://github.com/ZeldaFan0225/stable_horde) for integrating into the Stable Horde.", + "importance": "Information" + }, + { + "date_published": "2022-10-22", + "newspiece": "Krita and GIMP plugins now support img2img", + "importance": "Information" + }, + { + "date_published": "2022-10-21", + "newspiece": "Image 2 Image is now available for everyone!", + "importance": "Information" + }, + { + "date_published": "2022-10-20", + "newspiece": "Stable Diffusion 1.5 is now available!", + "importance": "Information" + }, + { + "date_published": "2022-10-17", + "newspiece": "We now have [a Krita plugin](https://github.com/blueturtleai/krita-stable-diffusion).", + "importance": "Information" + }, + { + "date_published": "2022-10-17", + "newspiece": "Img2img on the horde is now on pilot for trusted users.", + "importance": "Information" + }, + { + "date_published": "2022-10-16", + "newspiece": "Yet [another Web UI](https://tinybots.net/artbot) has appeared.", + "importance": "Information" + }, + { + "date_published": "2022-10-11", + "newspiece": "A [new dedicated Web UI](https://aqualxx.github.io/stable-ui/) has entered the scene!", + "importance": "Information" + }, + { + "date_published": "2022-10-10", + "newspiece": "You can now contribute a worker to the horde [via google colab](https://colab.research.google.com/github/harrisonvanderbyl/ravenbot-ai/blob/master/Horde.ipynb). Just fill-in your API key and run!", + "importance": "Information" + }, + { + "date_published": "2022-10-06", + "newspiece": "We have a [new installation video](https://youtu.be/wJrp5lpByCc) for both the Stable Horde Client and the Stable horde worker.", + "importance": "Information" + }, { + "date_published": "2023-01-23", + "newspiece": "All workers must start sending the `bridge_agent` key in their job pop payloads. See API documentation.", + "importance": "Workers" + }, + { + "date_published": "2022-10-10", + "newspiece": "The [discord rewards bot](https://www.patreon.com/posts/new-kind-of-73097166) has been unleashed. Reward good contributions to the horde directly from the chat!", + "importance": "Information" + }, + { + "date_published": "2022-10-13", + "newspiece": "KoboldAI Has been upgraded to the new countermeasures", + "tags": ["countermeasures", "ai horde"], + "importance": "Information", + }, + { + "date_published": "2022-10-09", + "newspiece": "The horde now includes News functionality. Also [In the API!](/api/v2/status/news)", + "importance": "Information" + }, + ] + + def get_news(self): + '''extensible function from gathering nodes from extensing classes''' + return(self.HORDE_NEWS) + + def sort_news(self, raw_news): + # unsorted_news = [] + # for piece in raw_news: + # piece_dict = { + # "date": datetime.strptime(piece["piece"], '%y-%m-%d'), + # "piece": piece["news"], + # } + # unsorted_news.append(piece_dict) + sorted_news = sorted(raw_news, key=lambda p: datetime.strptime(p["date_published"], '%Y-%m-%d'), reverse=True) + return(sorted_news) + + def sorted_news(self): + return(self.sort_news(self.get_news())) diff --git a/overseer/database/__init__.py b/overseer/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/overseer/database/functions.py b/overseer/database/functions.py new file mode 100644 index 0000000..0dd52c5 --- /dev/null +++ b/overseer/database/functions.py @@ -0,0 +1,12 @@ +import time +import uuid +import json +from datetime import datetime, timedelta +from sqlalchemy import func, or_, and_, not_, Boolean +from sqlalchemy.orm import noload +from overseer.flask import db, SQLITE_MODE + +from overseer.classes.instance import Instance + +def get_all_instances(): + return db.session.query(Instance).all() diff --git a/overseer/flask.py b/overseer/flask.py index c41644b..44dea2a 100644 --- a/overseer/flask.py +++ b/overseer/flask.py @@ -13,9 +13,9 @@ SQLITE_MODE = os.getenv("USE_SQLITE", "0") == "1" if SQLITE_MODE: logger.warning("Using SQLite for database") - OVERSEER.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///horde.db" + OVERSEER.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///overseer.db" else: - OVERSEER.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://postgres:{os.getenv('POSTGRES_PASS')}@{os.getenv('POSTGRES_URL')}" + OVERSEER.config["SQLALCHEMY_DATABASE_URI"] = os.getenv('POSTGRES_URI') OVERSEER.config['SQLALCHEMY_ENGINE_OPTIONS'] = { "pool_size": 50, "max_overflow": -1, diff --git a/overseer/utils.py b/overseer/utils.py new file mode 100644 index 0000000..6fca757 --- /dev/null +++ b/overseer/utils.py @@ -0,0 +1,103 @@ +import uuid +import bleach +import secrets +import hashlib +import os +import random +import regex as re +import json +from datetime import datetime +import dateutil.relativedelta +from loguru import logger +from overseer.flask import SQLITE_MODE + + +random.seed(random.SystemRandom().randint(0, 2**32 - 1)) + + +def count_digits(number): + digits = 1 + while number > 10: + number = number / 10 + digits += 1 + return digits + +class ConvertAmount: + + def __init__(self,amount,decimals = 1): + self.digits = count_digits(amount) + self.decimals = decimals + if self.digits < 4: + self.amount = round(amount, self.decimals) + self.prefix = '' + self.char = '' + elif self.digits < 7: + self.amount = round(amount / 1000, self.decimals) + self.prefix = 'kilo' + self.char = 'K' + elif self.digits < 10: + self.amount = round(amount / 1000000, self.decimals) + self.prefix = 'mega' + self.char = 'M' + elif self.digits < 13: + self.amount = round(amount / 1000000000, self.decimals) + self.prefix = 'giga' + self.char = 'G' + else: + self.amount = round(amount / 1000000000000, self.decimals) + self.prefix = 'tera' + self.char = 'T' + +def get_db_uuid(): + if SQLITE_MODE: + return str(uuid.uuid4()) + else: + return uuid.uuid4() + +def generate_client_id(): + return secrets.token_urlsafe(16) + +def sanitize_string(text): + santxt = bleach.clean(text).lstrip().rstrip() + return santxt + +def hash_api_key(unhashed_api_key): + salt = os.getenv("secret_key", "s0m3s3cr3t") # Note default here, just so it can run without env file + hashed_key = hashlib.sha256(salt.encode() + unhashed_api_key.encode()).hexdigest() + # logger.warning([os.getenv("secret_key", "s0m3s3cr3t"), hashed_key,unhashed_api_key]) + return hashed_key + + +def hash_dictionary(dictionary): + # Convert the dictionary to a JSON string + json_string = json.dumps(dictionary, sort_keys=True) + # Create a hash object + hash_object = hashlib.sha256(json_string.encode()) + # Get the hexadecimal representation of the hash + hash_hex = hash_object.hexdigest() + return hash_hex + +def get_expiry_date(): + return datetime.utcnow() + dateutil.relativedelta.relativedelta(minutes=+20) + +def get_random_seed(start_point=0): + '''Generated a random seed, using a random number unique per node''' + return random.randint(start_point, 2**32 - 1) + +def count_parentheses(s): + open_p = False + count = 0 + for c in s: + if c == "(": + open_p = True + elif c == ")" and open_p: + open_p = False + count += 1 + return count + +def validate_regex(regex_string): + try: + re.compile(regex_string, re.IGNORECASE) + except: + return False + return True \ No newline at end of file