Skip to content

Commit

Permalink
feat: Added documents endpoint (#416)
Browse files Browse the repository at this point in the history
* feat: Added documents endpoint
  • Loading branch information
db0 authored Jun 6, 2024
1 parent 89903c2 commit 69ce1fe
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 4.36.0

* Added Document endpoints

# 4.35.1

* Added allow_sdxl_controlnet worker key
Expand Down
13 changes: 13 additions & 0 deletions horde/apis/models/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,3 +1507,16 @@ def __init__(self, api):
"reference": fields.String(description="The reference which points how and where this text should be used.", min_length=3),
},
)
self.response_model_doc_terms = api.model(
"HordeDocument",
{
"html": fields.String(
required=False,
description="The document in html format.",
),
"markdown": fields.String(
required=False,
description="The document in markdown format.",
),
},
)
3 changes: 3 additions & 0 deletions horde/apis/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@
api.add_resource(stable.ImageHordeStatsModels, "/stats/img/models")
api.add_resource(kobold.TextHordeStatsTotals, "/stats/text/totals")
api.add_resource(kobold.TextHordeStatsModels, "/stats/text/models")
api.add_resource(base.DocsTerms, "/documents/terms")
api.add_resource(base.DocsPrivacy, "/documents/privacy")
api.add_resource(base.DocsSponsors, "/documents/sponsors")
138 changes: 136 additions & 2 deletions horde/apis/v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from datetime import datetime, timedelta

import regex as re
from flask import request
from flask import render_template, request
from flask_restx import Namespace, Resource, reqparse
from flask_restx.reqparse import ParseResult
from markdownify import markdownify
from sqlalchemy import or_
from sqlalchemy.exc import IntegrityError, InvalidRequestError

Expand Down Expand Up @@ -35,7 +36,7 @@
from horde.r2 import upload_prompt
from horde.suspicions import Suspicions
from horde.utils import hash_api_key, hash_dictionary, is_profane, sanitize_string
from horde.vars import horde_title
from horde.vars import horde_contact_email, horde_title, horde_url

# Not used yet
authorizations = {"apikey": {"type": "apiKey", "in": "header", "name": "apikey"}}
Expand Down Expand Up @@ -2914,3 +2915,136 @@ def delete(self, sharedkey_id=""):
db.session.delete(sharedkey)
db.session.commit()
return {"message": "OK"}, 200


class DocsTerms(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(
"format",
required=False,
default="html",
type=str,
help="html or markdown",
location="args",
)

# @cache.cached(timeout=3600, query_string=True)
@api.response(400, "Validation Error", models.response_model_error)
@api.expect(get_parser)
@api.marshal_with(
models.response_model_doc_terms,
code=200,
description=f"{horde_title} documentation",
skip_none=True,
)
def get(self):
"""Terms and Conditions"""
self.args = self.get_parser.parse_args()
if self.args.format not in ["html", "markdown"]:
raise e.BadRequest("'format' needs to be one of ['html', 'markdown']")
html_template = render_template(
os.getenv("HORDE_HTML_TERMS", "terms_of_service.html"),
horde_title=horde_title,
horde_url=horde_url,
horde_contact_email=horde_contact_email,
)
if self.args.format == "markdown":
return {"markdown": markdownify(html_template).strip("\n")}, 200
return {"html": html_template}, 200


class DocsPrivacy(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(
"format",
required=False,
default="html",
type=str,
help="html or markdown",
location="args",
)

# @cache.cached(timeout=3600, query_string=True)
@api.response(400, "Validation Error", models.response_model_error)
@api.expect(get_parser)
@api.marshal_with(
models.response_model_doc_terms,
code=200,
description=f"{horde_title} documentation",
skip_none=True,
)
def get(self):
"""Privacy Policy"""
self.args = self.get_parser.parse_args()
if self.args.format not in ["html", "markdown"]:
raise e.BadRequest("'format' needs to be one of ['html', 'markdown']")
html_template = render_template(
os.getenv("HORDE_HTML_PRIVACY", "privacy_policy.html"),
horde_title=horde_title,
horde_url=horde_url,
horde_contact_email=horde_contact_email,
)
if self.args.format == "markdown":
return {"markdown": markdownify(html_template).strip("\n")}, 200
return {"html": html_template}, 200


class DocsSponsors(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(
"format",
required=False,
default="html",
type=str,
help="html or markdown",
location="args",
)

# @cache.cached(timeout=3600, query_string=True)
@api.response(400, "Validation Error", models.response_model_error)
@api.expect(get_parser)
@api.marshal_with(
models.response_model_doc_terms,
code=200,
description=f"{horde_title} documentation",
skip_none=True,
)
def get(self):
"""Sponsors"""
self.args = self.get_parser.parse_args()
if self.args.format not in ["html", "markdown"]:
raise e.BadRequest("'format' needs to be one of ['html', 'markdown']")
all_patrons = ", ".join(patrons.get_names(min_entitlement=3, max_entitlement=99))
html_template = render_template(
"sponsors.html",
page_title="Sponsors",
all_patrons=all_patrons,
all_sponsors=patrons.get_sponsors(),
)
if self.args.format == "markdown":
return {"markdown": markdownify(html_template).strip("\n")}, 200
return {"html": html_template}, 200
2 changes: 1 addition & 1 deletion horde/consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
HORDE_VERSION = "4.35.1"
HORDE_VERSION = "4.36.0"

WHITELISTED_SERVICE_IPS = {
"212.227.227.178", # Turing Bot
Expand Down
9 changes: 6 additions & 3 deletions horde/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def index():
def patrons_route():
all_patrons = ", ".join(patrons.get_names(min_entitlement=3, max_entitlement=99))
return render_template(
"sponsors.html",
"document.html",
doc="sponsors.html",
page_title="Sponsors",
all_patrons=all_patrons,
all_sponsors=patrons.get_sponsors(),
Expand Down Expand Up @@ -364,7 +365,8 @@ def finish_dance():
@HORDE.route("/privacy")
def privacy():
return render_template(
os.getenv("HORDE_HTML_PRIVACY", "privacy_policy.html"),
"document.html",
doc=os.getenv("HORDE_HTML_TERMS", "privacy_policy.html"),
horde_title=horde_title,
horde_url=horde_url,
horde_contact_email=horde_contact_email,
Expand All @@ -374,7 +376,8 @@ def privacy():
@HORDE.route("/terms")
def terms():
return render_template(
os.getenv("HORDE_HTML_TERMS", "terms_of_service.html"),
"document.html",
doc=os.getenv("HORDE_HTML_TERMS", "terms_of_service.html"),
horde_title=horde_title,
horde_url=horde_url,
horde_contact_email=horde_contact_email,
Expand Down
7 changes: 7 additions & 0 deletions horde/templates/document.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "master.html" %}
{% block content %}
{% include doc %}
<br/>
<br/>
<p><a href="/">Back to Main page</a></p>
{% endblock %}
4 changes: 0 additions & 4 deletions horde/templates/privacy_policy.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% extends "master.html" %}
{% block content %}
<h1>Privacy Policy</h1>
<p>Last updated: 18 Mar, 2024</p>
<p>This Privacy Policy describes Our policies and procedures on the collection, use and disclosure of Your information when You use the Service and tells You about Your privacy rights and how the law protects You.</p>
Expand Down Expand Up @@ -153,5 +151,3 @@ <h1>Contact Us</h1>
<ul>
<li>By email: {{ horde_contact_email }}</li>
</ul>
<p><a href="/">Back to Main page</a></p>
{% endblock %}
8 changes: 1 addition & 7 deletions horde/templates/sponsors.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% extends "master.html" %}
{% block content %}
<h1>Sponsors</h1>
<ul>
<li><a href="https://getipintel.net">GetIPIntel.net - Free Proxy/VPN Detection</a></li>
Expand All @@ -13,8 +11,4 @@ <h1>Sponsors</h1>

<br/>
<h1><a href="https://www.patreon.com/db0">Patrons</a></h1>
{{ all_patrons }}
<br/>
<br/>
<p><a href="/">Back to Main page</a></p>
{% endblock %}
{{ all_patrons }}
6 changes: 1 addition & 5 deletions horde/templates/terms_of_service.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% extends "master.html" %}
{% block content %}
<h1>Terms and Conditions</h1>
<p>Last updated: 07 January, 2023</p>
<p>Please read these terms and conditions carefully before using Our Service.</p>
Expand Down Expand Up @@ -122,6 +120,4 @@ <h1>Contact Us</h1>
<p>If you have any questions about these Terms and Conditions, You can contact us:</p>
<ul>
<li>By email: {{ horde_contact_email }}</li>
</ul>
<p><a href="/">Back to Main page</a></p>
{% endblock %}
</ul>
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ git+https://github.com/Patreon/patreon-python.git
torch
emoji
semver >= 3.0.2
numpy ~= 1.24.1 # better_profanity fails on later versions of numpy
numpy ~= 1.24.1 # better_profanity fails on later versions of numpy
markdownify

0 comments on commit 69ce1fe

Please sign in to comment.