Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker image with DeepDanbooru #94

Open
ImoutoChan opened this issue May 21, 2023 · 5 comments
Open

Docker image with DeepDanbooru #94

ImoutoChan opened this issue May 21, 2023 · 5 comments

Comments

@ImoutoChan
Copy link

ImoutoChan commented May 21, 2023

Is there any docker image with some kind of HTTP API (like your demo)?

If there is none I would like to make one, but I have zero knowledge in Python development, maybe you have some tips?
I also don't see the trained model, only instructions on how to make my own. Can I just download it from somewhere?

The way I see it:

  1. Write some kind of HTTP wrapper
  2. Download trained model from somewhere
  3. Take base image with Python 3.7
  4. Call pip install -r requirements.txt
  5. Start the python app and forward some port from outside to the wrapper

Am I right?

@Superfloh
Copy link

The models are under releases.
Technically that should work, but I remember that there were some issues. You will have to try and see if you can get it working I fear.

@kamuridesu
Copy link

I have created a Dockerfile with and without the model: https://github.com/kamuridesu/DeepDanbooru
Tags are:

  • kamuri/deepdanbooru:nomodel for the image without a model
  • kamuri/deepdanbooru:latest for image with model

Although it does not have any API but isn't so hard, I may create one later.

Also, check https://github.com/kamuridesu/deepdanbooryu, a project using events and Docker to create a fully scalable backend if you're interested.

@ImoutoChan
Copy link
Author

Thank you, I'm definitely waiting for a docker image with API.

If anyone else is interested in using DeepDanbooru through docker you can:

linux
docker run -v /images:/app/images kamuri/deepdanbooru:latest evaluate /app/images/1.png --project-path /app/model

windows
docker run -v //c/images:/app/images kamuri/deepdanbooru:latest evaluate /app/images/1.png --project-path /app/model

for the image in C:\images\1.png or in /images/1.png

@kamuridesu
Copy link

A simple Web API implementation for evaluate_image:

import os
import deepdanbooru
from io import BytesIO
from deepdanbooru.commands import evaluate_image
from flask import Flask, request, render_template

PROJECT_PATH = os.path.abspath("model")
print("Loading model")
MODEL = deepdanbooru.project.load_model_from_project(
    PROJECT_PATH, compile_model=False
)
print("Loading tags")
TAGS = deepdanbooru.project.load_tags_from_project(PROJECT_PATH)


app = Flask("deepdanbooru", template_folder=os.path.abspath("templates"))


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/upload", methods=["POST"])
async def upload():
    if "file" not in request.files:
        return "No file uploaded!"

    file = request.files["file"]

    result = evaluate_image(BytesIO(file.stream.read()), MODEL, TAGS, 0.5)
    results = {}
    for tag, score in result:
        results[tag] = float(f"{score:05.3f}")
    return results


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port="8080")

The model is stored in a subdir called model, placed in the same folder as the script, just extract the .zip in releases to the folder. It also has a templates/index.html file to test the upload:

<!DOCTYPE html>
<html>
<head>
    <title>File Upload</title>
</head>
<body>
    <h1>Upload a File</h1>
    <form action="/upload" method="POST" enctype="multipart/form-data">
        <input type="file" name="file" accept="*">
        <input type="submit" value="Upload">
    </form>
</body>
</html>

@sky-cake
Copy link

sky-cake commented Sep 8, 2024

@kamuridesu I took your example and added it to a DDB scripts repo at https://github.com/sky-cake/ddb_scripts. The scripts also include an image crawler which saves tags and scores in a db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants