mirror of
https://github.com/karl0ss/ai_image_frame_server.git
synced 2025-06-08 19:35:07 +01:00
Compare commits
5 Commits
41fd1444eb
...
9c606451c2
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9c606451c2 | ||
![]() |
305b55eaf6 | ||
![]() |
41de36856b | ||
![]() |
7cf3fdb432 | ||
![]() |
32afc70d03 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,6 +5,5 @@ script.log
|
|||||||
build/
|
build/
|
||||||
dist/
|
dist/
|
||||||
user_config.cfg
|
user_config.cfg
|
||||||
output/
|
output/**.*
|
||||||
prompts_log.jsonl
|
prompts_log.jsonl
|
||||||
publish.sh
|
|
@ -9,10 +9,7 @@ import os
|
|||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
from apscheduler.schedulers.background import BackgroundScheduler
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
# from lib import create_image, load_config, create_prompt_on_openwebui, cancel_current_job, get_prompt_from_png
|
from lib import create_image, load_config, create_prompt_on_openwebui, cancel_current_job
|
||||||
from libs.generic import load_config, load_recent_prompts, get_prompt_from_png
|
|
||||||
from libs.comfyui import cancel_current_job, create_image
|
|
||||||
from libs.ollama import create_prompt_on_openwebui
|
|
||||||
|
|
||||||
user_config = load_config()
|
user_config = load_config()
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -22,44 +19,32 @@ image_folder = "./output"
|
|||||||
@app.route("/", methods=["GET"])
|
@app.route("/", methods=["GET"])
|
||||||
def index() -> str:
|
def index() -> str:
|
||||||
"""
|
"""
|
||||||
Renders the main HTML template with image and prompt.
|
Renders the main HTML template.
|
||||||
|
Args:
|
||||||
|
None
|
||||||
|
Returns:
|
||||||
|
str: The rendered HTML template.
|
||||||
"""
|
"""
|
||||||
image_filename = "./image.png"
|
|
||||||
image_path = os.path.join(image_folder, image_filename)
|
|
||||||
|
|
||||||
prompt = get_prompt_from_png(image_path)
|
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"index.html",
|
"index.html",
|
||||||
image=image_filename,
|
image="./image.png",
|
||||||
prompt=prompt,
|
|
||||||
reload_interval=user_config["frame"]["reload_interval"],
|
reload_interval=user_config["frame"]["reload_interval"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/images", methods=["GET"])
|
@app.route("/images", methods=["GET"])
|
||||||
def gallery() -> str:
|
def gallery() -> str:
|
||||||
"""
|
"""
|
||||||
Renders the gallery HTML template.
|
Renders the gallery HTML template.
|
||||||
|
Args:
|
||||||
|
None
|
||||||
Returns:
|
Returns:
|
||||||
str: The rendered HTML template.
|
str: The rendered HTML template.
|
||||||
"""
|
"""
|
||||||
images = []
|
images = [f for f in os.listdir(image_folder) if f.lower().endswith(('png', 'jpg', 'jpeg', 'gif'))]
|
||||||
for f in os.listdir(image_folder):
|
images = sorted(images, reverse=True)
|
||||||
if f.lower().endswith(('png', 'jpg', 'jpeg', 'gif')):
|
|
||||||
path = os.path.join(image_folder, f) # Full path to the image
|
|
||||||
prompt = get_prompt_from_png(path) # Your method to extract the prompt
|
|
||||||
images.append({'filename': f, 'prompt': prompt, 'path': path}) # Add 'path' to the dictionary
|
|
||||||
|
|
||||||
images = sorted(images, key=lambda x: os.path.getmtime(x['path']), reverse=True)
|
|
||||||
return render_template("gallery.html", images=images)
|
return render_template("gallery.html", images=images)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/images/thumbnails/<path:filename>')
|
|
||||||
def serve_thumbnail(filename):
|
|
||||||
return send_from_directory('output/thumbnails', filename)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/images/<filename>", methods=["GET"])
|
@app.route("/images/<filename>", methods=["GET"])
|
||||||
def images(filename: str) -> None:
|
def images(filename: str) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
import os
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
# Define paths
|
|
||||||
input_folder = "output"
|
|
||||||
thumbs_folder = "output/thumbnails"
|
|
||||||
thumb_width = 500
|
|
||||||
|
|
||||||
# Create the thumbs folder if it doesn't exist
|
|
||||||
os.makedirs(thumbs_folder, exist_ok=True)
|
|
||||||
|
|
||||||
# Supported image extensions
|
|
||||||
image_extensions = (".png", ".jpg", ".jpeg", ".webp")
|
|
||||||
|
|
||||||
# Loop through files
|
|
||||||
for filename in os.listdir(input_folder):
|
|
||||||
if filename.lower().endswith(image_extensions):
|
|
||||||
input_path = os.path.join(input_folder, filename)
|
|
||||||
output_path = os.path.join(thumbs_folder, filename)
|
|
||||||
|
|
||||||
try:
|
|
||||||
with Image.open(input_path) as img:
|
|
||||||
# Maintain aspect ratio
|
|
||||||
img.thumbnail((thumb_width, img.height), Image.LANCZOS)
|
|
||||||
img.save(output_path)
|
|
||||||
print(f"✅ Thumbnail saved: {output_path}")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"❌ Error processing {filename}: {e}")
|
|
33
lib.py
33
lib.py
@ -6,7 +6,6 @@ import litellm
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
from PIL import Image
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from comfy_api_simplified import ComfyApiWrapper, ComfyWorkflowWrapper
|
from comfy_api_simplified import ComfyApiWrapper, ComfyWorkflowWrapper
|
||||||
from tenacity import (
|
from tenacity import (
|
||||||
@ -19,7 +18,7 @@ from tenacity import (
|
|||||||
import nest_asyncio
|
import nest_asyncio
|
||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from libs.create_thumbnail import generate_thumbnail
|
|
||||||
nest_asyncio.apply()
|
nest_asyncio.apply()
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
@ -96,7 +95,6 @@ def rename_image() -> str | None:
|
|||||||
new_filename = f"{str(time.time())}.png"
|
new_filename = f"{str(time.time())}.png"
|
||||||
new_path = os.path.join(user_config["comfyui"]["output_dir"], new_filename)
|
new_path = os.path.join(user_config["comfyui"]["output_dir"], new_filename)
|
||||||
os.rename(old_path, new_path)
|
os.rename(old_path, new_path)
|
||||||
generate_thumbnail(new_path)
|
|
||||||
print(f"Renamed 'image.png' to '{new_filename}'")
|
print(f"Renamed 'image.png' to '{new_filename}'")
|
||||||
return new_filename
|
return new_filename
|
||||||
else:
|
else:
|
||||||
@ -106,21 +104,11 @@ def rename_image() -> str | None:
|
|||||||
|
|
||||||
def create_prompt_on_openwebui(prompt: str) -> str:
|
def create_prompt_on_openwebui(prompt: str) -> str:
|
||||||
"""Sends prompt to OpenWebui and returns the generated response."""
|
"""Sends prompt to OpenWebui and returns the generated response."""
|
||||||
# Unique list of recent prompts
|
recent_prompts = load_recent_prompts()
|
||||||
recent_prompts = list(set(load_recent_prompts()))
|
|
||||||
# Decide on whether to include a topic (e.g., 30% chance to include)
|
|
||||||
topics = [t.strip() for t in user_config["comfyui"]["topics"].split(",") if t.strip()]
|
|
||||||
topic_instruction = ""
|
|
||||||
if random.random() < 0.3 and topics:
|
|
||||||
selected_topic = random.choice(topics)
|
|
||||||
topic_instruction = f" Incorporate the theme of '{selected_topic}' into the new prompt."
|
|
||||||
|
|
||||||
user_content = (
|
user_content = (
|
||||||
"Here are the prompts from the last 7 days:\n\n"
|
"Here are the prompts from the last 7 days:\n\n"
|
||||||
+ "\n".join(f"{i+1}. {p}" for i, p in enumerate(recent_prompts))
|
+ "\n".join(f"{i+1}. {p}" for i, p in enumerate(recent_prompts))
|
||||||
+ "\n\nDo not repeat ideas, themes, or settings from the above. "
|
+ "\n\nDo not repeat ideas, themes, or settings from the above. Now generate a new, completely original Stable Diffusion prompt that hasn't been done yet."
|
||||||
"Now generate a new, completely original Stable Diffusion prompt that hasn't been done yet."
|
|
||||||
+ topic_instruction
|
|
||||||
)
|
)
|
||||||
|
|
||||||
model = random.choice(user_config["openwebui"]["models"].split(","))
|
model = random.choice(user_config["openwebui"]["models"].split(","))
|
||||||
@ -272,20 +260,5 @@ def create_image(prompt: str | None = None) -> None:
|
|||||||
logging.error("No prompt generated.")
|
logging.error("No prompt generated.")
|
||||||
|
|
||||||
|
|
||||||
def get_prompt_from_png(path):
|
|
||||||
try:
|
|
||||||
with Image.open(path) as img:
|
|
||||||
try:
|
|
||||||
# Flux workflow
|
|
||||||
meta = json.loads(img.info["prompt"])['44']['inputs']['text']
|
|
||||||
except KeyError:
|
|
||||||
# SDXL workflow
|
|
||||||
meta = json.loads(img.info["prompt"])['6']['inputs']['text']
|
|
||||||
return meta or ""
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error reading metadata from {path}: {e}")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
user_config = load_config()
|
user_config = load_config()
|
||||||
output_folder = user_config["comfyui"]["output_dir"]
|
output_folder = user_config["comfyui"]["output_dir"]
|
||||||
|
|
||||||
|
163
libs/comfyui.py
163
libs/comfyui.py
@ -1,163 +0,0 @@
|
|||||||
import random
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import requests
|
|
||||||
from typing import Optional
|
|
||||||
from comfy_api_simplified import ComfyApiWrapper, ComfyWorkflowWrapper
|
|
||||||
from tenacity import (
|
|
||||||
retry,
|
|
||||||
stop_after_attempt,
|
|
||||||
wait_fixed,
|
|
||||||
before_log,
|
|
||||||
retry_if_exception_type,
|
|
||||||
)
|
|
||||||
import nest_asyncio
|
|
||||||
from libs.generic import rename_image, load_config, save_prompt
|
|
||||||
from libs.create_thumbnail import generate_thumbnail
|
|
||||||
from libs.ollama import create_prompt_on_openwebui
|
|
||||||
nest_asyncio.apply()
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
LOG_FILE = "./prompts_log.jsonl"
|
|
||||||
|
|
||||||
user_config = load_config()
|
|
||||||
output_folder = user_config["comfyui"]["output_dir"]
|
|
||||||
|
|
||||||
|
|
||||||
def get_available_models() -> list:
|
|
||||||
"""Fetches available models from ComfyUI."""
|
|
||||||
url = user_config["comfyui"]["comfyui_url"] + "/object_info"
|
|
||||||
response = requests.get(url)
|
|
||||||
if response.status_code == 200:
|
|
||||||
data = response.json()
|
|
||||||
return (
|
|
||||||
data.get("CheckpointLoaderSimple", {})
|
|
||||||
.get("input", {})
|
|
||||||
.get("required", {})
|
|
||||||
.get("ckpt_name", [])[0]
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
print(f"Failed to fetch models: {response.status_code}")
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
def cancel_current_job() -> list:
|
|
||||||
"""Fetches available models from ComfyUI."""
|
|
||||||
url = user_config["comfyui"]["comfyui_url"] + "/interrupt"
|
|
||||||
response = requests.post(url)
|
|
||||||
if response.status_code == 200:
|
|
||||||
return "Cancelled"
|
|
||||||
else:
|
|
||||||
return "Failed to cancel"
|
|
||||||
|
|
||||||
|
|
||||||
# Define the retry logic using Tenacity
|
|
||||||
@retry(
|
|
||||||
stop=stop_after_attempt(3),
|
|
||||||
wait=wait_fixed(5),
|
|
||||||
before=before_log(logging.getLogger(), logging.DEBUG),
|
|
||||||
retry=retry_if_exception_type(Exception),
|
|
||||||
)
|
|
||||||
def generate_image(
|
|
||||||
file_name: str,
|
|
||||||
comfy_prompt: str,
|
|
||||||
workflow_path: str = "./workflow_api.json",
|
|
||||||
prompt_node: str = "CLIP Text Encode (Prompt)",
|
|
||||||
seed_node: str = "KSampler",
|
|
||||||
seed_param: str = "seed",
|
|
||||||
save_node: str = "Save Image",
|
|
||||||
save_param: str = "filename_prefix",
|
|
||||||
model_node: Optional[str] = "Load Checkpoint",
|
|
||||||
model_param: Optional[str] = "ckpt_name",
|
|
||||||
) -> None:
|
|
||||||
"""Generates an image using the Comfy API with configurable workflow settings."""
|
|
||||||
try:
|
|
||||||
api = ComfyApiWrapper(user_config["comfyui"]["comfyui_url"])
|
|
||||||
wf = ComfyWorkflowWrapper(workflow_path)
|
|
||||||
|
|
||||||
# Set workflow parameters
|
|
||||||
wf.set_node_param(seed_node, seed_param, random.getrandbits(32))
|
|
||||||
wf.set_node_param(prompt_node, "text", comfy_prompt)
|
|
||||||
wf.set_node_param(save_node, save_param, file_name)
|
|
||||||
wf.set_node_param(
|
|
||||||
(
|
|
||||||
"Empty Latent Image"
|
|
||||||
if workflow_path.endswith("workflow_api.json")
|
|
||||||
else "CR Aspect Ratio"
|
|
||||||
),
|
|
||||||
"width",
|
|
||||||
user_config["comfyui"]["width"],
|
|
||||||
)
|
|
||||||
wf.set_node_param(
|
|
||||||
(
|
|
||||||
"Empty Latent Image"
|
|
||||||
if workflow_path.endswith("workflow_api.json")
|
|
||||||
else "CR Aspect Ratio"
|
|
||||||
),
|
|
||||||
"height",
|
|
||||||
user_config["comfyui"]["height"],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Conditionally set model if node and param are provided
|
|
||||||
if model_node and model_param:
|
|
||||||
if user_config["comfyui"].get("FLUX"):
|
|
||||||
valid_models = user_config["comfyui:flux"]["models"].split(",")
|
|
||||||
else:
|
|
||||||
available_model_list = user_config["comfyui"]["models"].split(",")
|
|
||||||
valid_models = list(
|
|
||||||
set(get_available_models()) & set(available_model_list)
|
|
||||||
)
|
|
||||||
|
|
||||||
if not valid_models:
|
|
||||||
raise Exception("No valid models available.")
|
|
||||||
|
|
||||||
model = random.choice(valid_models)
|
|
||||||
wf.set_node_param(model_node, model_param, model)
|
|
||||||
|
|
||||||
# Generate image
|
|
||||||
logging.debug(f"Generating image: {file_name}")
|
|
||||||
results = api.queue_and_wait_images(wf, save_node)
|
|
||||||
rename_image()
|
|
||||||
|
|
||||||
for _, image_data in results.items():
|
|
||||||
output_path = os.path.join(
|
|
||||||
user_config["comfyui"]["output_dir"], f"{file_name}.png"
|
|
||||||
)
|
|
||||||
with open(output_path, "wb+") as f:
|
|
||||||
f.write(image_data)
|
|
||||||
generate_thumbnail(output_path)
|
|
||||||
|
|
||||||
logging.debug(f"Image generated successfully for UID: {file_name}")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Failed to generate image for UID: {file_name}. Error: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def create_image(prompt: str | None = None) -> None:
|
|
||||||
"""Main function for generating images."""
|
|
||||||
if prompt is None:
|
|
||||||
prompt = create_prompt_on_openwebui(user_config["comfyui"]["prompt"])
|
|
||||||
if prompt:
|
|
||||||
logging.info(f"Generated prompt: {prompt}") # Log generated prompt
|
|
||||||
save_prompt(prompt)
|
|
||||||
if user_config["comfyui"]["FLUX"]:
|
|
||||||
generate_image(
|
|
||||||
file_name="image",
|
|
||||||
comfy_prompt=prompt,
|
|
||||||
workflow_path="./FLUX.json",
|
|
||||||
prompt_node="Positive Prompt T5",
|
|
||||||
seed_node="Seed",
|
|
||||||
seed_param="seed",
|
|
||||||
save_node="CivitAI Image Saver",
|
|
||||||
save_param="filename",
|
|
||||||
model_node="CivitAI Image Saver",
|
|
||||||
model_param="modelname",
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
generate_image("image", prompt)
|
|
||||||
print(f"Image generation started with prompt: {prompt}")
|
|
||||||
else:
|
|
||||||
logging.error("No prompt generated.")
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
|||||||
from PIL import Image
|
|
||||||
import os
|
|
||||||
|
|
||||||
def generate_thumbnail(image_path: str, size=(500, 500)) -> str:
|
|
||||||
"""
|
|
||||||
Generates a thumbnail for a given image with a max size of 500x500,
|
|
||||||
and saves it in a 'thumbnails' subdirectory alongside the original.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
image_path (str): Path to the original image.
|
|
||||||
size (tuple): Maximum width and height of the thumbnail.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: Path to the thumbnail image.
|
|
||||||
"""
|
|
||||||
image_dir = os.path.dirname(image_path)
|
|
||||||
thumbnail_dir = os.path.join(image_dir, "thumbnails")
|
|
||||||
os.makedirs(thumbnail_dir, exist_ok=True)
|
|
||||||
|
|
||||||
filename = os.path.basename(image_path)
|
|
||||||
thumbnail_path = os.path.join(thumbnail_dir, filename)
|
|
||||||
|
|
||||||
if not os.path.exists(thumbnail_path):
|
|
||||||
try:
|
|
||||||
img = Image.open(image_path)
|
|
||||||
img.thumbnail(size, Image.Resampling.LANCZOS)
|
|
||||||
img.save(thumbnail_path, optimize=True)
|
|
||||||
print(f"Created thumbnail: {thumbnail_path}")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error creating thumbnail for {image_path}: {e}")
|
|
||||||
else:
|
|
||||||
print(f"Thumbnail already exists: {thumbnail_path}")
|
|
||||||
|
|
||||||
return thumbnail_path
|
|
@ -1,82 +0,0 @@
|
|||||||
import configparser
|
|
||||||
import logging
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
import os
|
|
||||||
from PIL import Image
|
|
||||||
import nest_asyncio
|
|
||||||
import json
|
|
||||||
from datetime import datetime
|
|
||||||
from libs.create_thumbnail import generate_thumbnail
|
|
||||||
nest_asyncio.apply()
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
LOG_FILE = "./prompts_log.jsonl"
|
|
||||||
|
|
||||||
def load_recent_prompts(count=7):
|
|
||||||
recent_prompts = []
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(LOG_FILE, "r") as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
for line in lines[-count:]:
|
|
||||||
data = json.loads(line.strip())
|
|
||||||
recent_prompts.append(data["prompt"])
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass # No prompts yet
|
|
||||||
|
|
||||||
return recent_prompts
|
|
||||||
|
|
||||||
|
|
||||||
def save_prompt(prompt):
|
|
||||||
entry = {"date": datetime.now().strftime("%Y-%m-%d"), "prompt": prompt}
|
|
||||||
with open(LOG_FILE, "a") as f:
|
|
||||||
f.write(json.dumps(entry) + "\n")
|
|
||||||
|
|
||||||
|
|
||||||
def load_config() -> configparser.ConfigParser:
|
|
||||||
"""Loads user configuration from ./user_config.cfg."""
|
|
||||||
user_config = configparser.ConfigParser()
|
|
||||||
try:
|
|
||||||
user_config.read("./user_config.cfg")
|
|
||||||
logging.debug("Configuration loaded successfully.")
|
|
||||||
return user_config
|
|
||||||
except KeyError as e:
|
|
||||||
logging.error(f"Missing configuration key: {e}")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def rename_image() -> str | None:
|
|
||||||
"""Renames 'image.png' in the output folder to a timestamped filename if it exists."""
|
|
||||||
old_path = os.path.join(user_config["comfyui"]["output_dir"], "image.png")
|
|
||||||
|
|
||||||
if os.path.exists(old_path):
|
|
||||||
new_filename = f"{str(time.time())}.png"
|
|
||||||
new_path = os.path.join(user_config["comfyui"]["output_dir"], new_filename)
|
|
||||||
os.rename(old_path, new_path)
|
|
||||||
generate_thumbnail(new_path)
|
|
||||||
print(f"Renamed 'image.png' to '{new_filename}'")
|
|
||||||
return new_filename
|
|
||||||
else:
|
|
||||||
print("No image.png found.")
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def get_prompt_from_png(path):
|
|
||||||
try:
|
|
||||||
with Image.open(path) as img:
|
|
||||||
try:
|
|
||||||
# Flux workflow
|
|
||||||
meta = json.loads(img.info["prompt"])['44']['inputs']['text']
|
|
||||||
except KeyError:
|
|
||||||
# SDXL workflow
|
|
||||||
meta = json.loads(img.info["prompt"])['6']['inputs']['text']
|
|
||||||
return meta or ""
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error reading metadata from {path}: {e}")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
|
|
||||||
user_config = load_config()
|
|
||||||
output_folder = user_config["comfyui"]["output_dir"]
|
|
@ -1,70 +0,0 @@
|
|||||||
import random
|
|
||||||
import logging
|
|
||||||
import litellm
|
|
||||||
import nest_asyncio
|
|
||||||
from libs.generic import load_recent_prompts, load_config
|
|
||||||
nest_asyncio.apply()
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
LOG_FILE = "./prompts_log.jsonl"
|
|
||||||
|
|
||||||
user_config = load_config()
|
|
||||||
output_folder = user_config["comfyui"]["output_dir"]
|
|
||||||
|
|
||||||
def create_prompt_on_openwebui(prompt: str) -> str:
|
|
||||||
"""Sends prompt to OpenWebui and returns the generated response."""
|
|
||||||
# Unique list of recent prompts
|
|
||||||
recent_prompts = list(set(load_recent_prompts()))
|
|
||||||
# Decide on whether to include a topic (e.g., 30% chance to include)
|
|
||||||
topics = [t.strip() for t in user_config["comfyui"]["topics"].split(",") if t.strip()]
|
|
||||||
topic_instruction = ""
|
|
||||||
if random.random() < 0.3 and topics:
|
|
||||||
selected_topic = random.choice(topics)
|
|
||||||
topic_instruction = f" Incorporate the theme of '{selected_topic}' into the new prompt."
|
|
||||||
|
|
||||||
user_content = (
|
|
||||||
"Here are the prompts from the last 7 days:\n\n"
|
|
||||||
+ "\n".join(f"{i+1}. {p}" for i, p in enumerate(recent_prompts))
|
|
||||||
+ "\n\nDo not repeat ideas, themes, or settings from the above. "
|
|
||||||
"Now generate a new, completely original Stable Diffusion prompt that hasn't been done yet."
|
|
||||||
+ topic_instruction
|
|
||||||
)
|
|
||||||
|
|
||||||
model = random.choice(user_config["openwebui"]["models"].split(","))
|
|
||||||
response = litellm.completion(
|
|
||||||
api_base=user_config["openwebui"]["base_url"],
|
|
||||||
model="openai/" + model,
|
|
||||||
messages=[
|
|
||||||
{
|
|
||||||
"role": "system",
|
|
||||||
"content": (
|
|
||||||
"You are a prompt generator for Stable Diffusion. "
|
|
||||||
"Generate a detailed and imaginative prompt with a strong visual theme. "
|
|
||||||
"Focus on lighting, atmosphere, and artistic style. "
|
|
||||||
"Keep the prompt concise, no extra commentary or formatting."
|
|
||||||
),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content": user_content,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
api_key=user_config["openwebui"]["api_key"],
|
|
||||||
)
|
|
||||||
|
|
||||||
prompt = response["choices"][0]["message"]["content"].strip('"')
|
|
||||||
# response = litellm.completion(
|
|
||||||
# api_base=user_config["openwebui"]["base_url"],
|
|
||||||
# model="openai/brxce/stable-diffusion-prompt-generator:latest",
|
|
||||||
# messages=[
|
|
||||||
# {
|
|
||||||
# "role": "user",
|
|
||||||
# "content": prompt,
|
|
||||||
# },
|
|
||||||
# ],
|
|
||||||
# api_key=user_config["openwebui"]["api_key"],
|
|
||||||
# )
|
|
||||||
# prompt = response["choices"][0]["message"]["content"].strip('"')
|
|
||||||
logging.debug(prompt)
|
|
||||||
return prompt
|
|
22
publish.sh
Normal file
22
publish.sh
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Set variables
|
||||||
|
IMAGE_NAME="ai-frame-image-server"
|
||||||
|
REGISTRY="kithub.k-world.me.uk"
|
||||||
|
USERNAME="karl"
|
||||||
|
TAG="latest"
|
||||||
|
FULL_IMAGE="$REGISTRY/$USERNAME/$IMAGE_NAME:$TAG"
|
||||||
|
|
||||||
|
# Build the image
|
||||||
|
echo "🛠️ Building Docker image..."
|
||||||
|
docker build -t $IMAGE_NAME .
|
||||||
|
|
||||||
|
# Tag the image
|
||||||
|
echo "🏷️ Tagging image as $FULL_IMAGE"
|
||||||
|
docker tag $IMAGE_NAME $FULL_IMAGE
|
||||||
|
|
||||||
|
# Push the image
|
||||||
|
echo "📤 Pushing $FULL_IMAGE to $REGISTRY..."
|
||||||
|
docker push $FULL_IMAGE
|
||||||
|
|
||||||
|
echo "✅ Done!"
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
@ -5,37 +5,17 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Image Archive</title>
|
<title>Image Archive</title>
|
||||||
<style>
|
<style>
|
||||||
* {
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
body {
|
|
||||||
background-color: black;
|
|
||||||
color: white;
|
|
||||||
font-family: sans-serif;
|
|
||||||
padding: 2rem;
|
|
||||||
}
|
|
||||||
h1 {
|
|
||||||
text-align: center;
|
|
||||||
margin-bottom: 2rem;
|
|
||||||
}
|
|
||||||
.gallery {
|
.gallery {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
|
grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
|
||||||
gap: 20px;
|
gap: 10px;
|
||||||
}
|
}
|
||||||
.gallery img {
|
.gallery img {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: auto;
|
height: auto;
|
||||||
border-radius: 10px;
|
border-radius: 5px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: transform 0.2s ease;
|
|
||||||
}
|
}
|
||||||
.gallery img:hover {
|
|
||||||
transform: scale(1.02);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Lightbox styles */
|
/* Lightbox styles */
|
||||||
.lightbox {
|
.lightbox {
|
||||||
display: none;
|
display: none;
|
||||||
@ -44,16 +24,15 @@
|
|||||||
left: 0;
|
left: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
background: rgba(0, 0, 0, 0.9);
|
background: rgba(0, 0, 0, 0.8);
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
z-index: 999;
|
|
||||||
}
|
}
|
||||||
.lightbox img {
|
.lightbox img {
|
||||||
max-width: 90%;
|
max-width: 90%;
|
||||||
max-height: 80%;
|
max-height: 90%;
|
||||||
border-radius: 10px;
|
border-radius: 5px;
|
||||||
}
|
}
|
||||||
.lightbox .close {
|
.lightbox .close {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@ -78,27 +57,13 @@
|
|||||||
.arrow.right {
|
.arrow.right {
|
||||||
right: 20px;
|
right: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#lightbox-prompt {
|
|
||||||
color: #ccc;
|
|
||||||
font-family: monospace;
|
|
||||||
white-space: pre-wrap;
|
|
||||||
background: rgba(0, 0, 0, 0.6);
|
|
||||||
padding: 10px 20px;
|
|
||||||
border-radius: 10px;
|
|
||||||
max-width: 80%;
|
|
||||||
text-align: left;
|
|
||||||
margin-top: 20px;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Image Archive</h1>
|
<h1>Image Archive</h1>
|
||||||
<div class="gallery">
|
<div class="gallery">
|
||||||
{% for image in images %}
|
{% for image in images %}
|
||||||
<!-- <img src="{{ url_for('images', filename=image.thumbnail_filename) }}" alt="Image" loading="lazy" onclick="openLightbox({{ loop.index0 }})"> -->
|
<img src="{{ url_for('images', filename=image) }}" alt="Image" onclick="openLightbox({{ loop.index0 }})">
|
||||||
<img src="{{ url_for('images', filename='thumbnails/' + image.filename) }}" data-fullsrc="{{ url_for('images', filename=image.filename) }}" onclick="openLightbox({{ loop.index0 }})">
|
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -107,26 +72,20 @@
|
|||||||
<span class="close" onclick="closeLightbox()">×</span>
|
<span class="close" onclick="closeLightbox()">×</span>
|
||||||
<span class="arrow left" onclick="prevImage()">❮</span>
|
<span class="arrow left" onclick="prevImage()">❮</span>
|
||||||
<img id="lightbox-img" src="">
|
<img id="lightbox-img" src="">
|
||||||
<p id="lightbox-prompt"></p>
|
|
||||||
<span class="arrow right" onclick="nextImage()">❯</span>
|
<span class="arrow right" onclick="nextImage()">❯</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let images = [
|
let images = [
|
||||||
{% for image in images %}
|
{% for image in images %}
|
||||||
{
|
"{{ url_for('images', filename=image) }}",
|
||||||
src: "{{ url_for('images', filename=image.filename) }}",
|
|
||||||
prompt: `{{ image.prompt | escape }}`
|
|
||||||
},
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
];
|
];
|
||||||
let currentIndex = 0;
|
let currentIndex = 0;
|
||||||
|
|
||||||
function openLightbox(index) {
|
function openLightbox(index) {
|
||||||
currentIndex = index;
|
currentIndex = index;
|
||||||
<!-- document.getElementById("lightbox-img").src = images[currentIndex].src; -->
|
document.getElementById("lightbox-img").src = images[currentIndex];
|
||||||
document.getElementById("lightbox-img").src = document.querySelectorAll('.gallery img')[currentIndex].dataset.fullsrc;
|
|
||||||
document.getElementById("lightbox-prompt").textContent = images[currentIndex].prompt;
|
|
||||||
document.getElementById("lightbox").style.display = "flex";
|
document.getElementById("lightbox").style.display = "flex";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,12 +95,12 @@
|
|||||||
|
|
||||||
function nextImage() {
|
function nextImage() {
|
||||||
currentIndex = (currentIndex + 1) % images.length;
|
currentIndex = (currentIndex + 1) % images.length;
|
||||||
openLightbox(currentIndex);
|
document.getElementById("lightbox-img").src = images[currentIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
function prevImage() {
|
function prevImage() {
|
||||||
currentIndex = (currentIndex - 1 + images.length) % images.length;
|
currentIndex = (currentIndex - 1 + images.length) % images.length;
|
||||||
openLightbox(currentIndex);
|
document.getElementById("lightbox-img").src = images[currentIndex];
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>AI Image of the Day</title>
|
<title>AI Image of the day</title>
|
||||||
<style>
|
<style>
|
||||||
* {
|
* {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
@ -12,57 +12,28 @@
|
|||||||
}
|
}
|
||||||
body {
|
body {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
background: black;
|
background: black;
|
||||||
color: white;
|
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
}
|
|
||||||
.image-container {
|
|
||||||
max-width: 90vw;
|
|
||||||
max-height: 80vh;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
margin-bottom: 20px;
|
|
||||||
}
|
}
|
||||||
img {
|
img {
|
||||||
max-width: 100%;
|
width: 100vw;
|
||||||
max-height: 100%;
|
height: 100vh;
|
||||||
object-fit: contain;
|
object-fit: contain;
|
||||||
border-radius: 20px;
|
|
||||||
box-shadow: 0 0 20px rgba(255, 255, 255, 0.2);
|
|
||||||
}
|
|
||||||
.prompt {
|
|
||||||
color: #ccc;
|
|
||||||
font-family: monospace;
|
|
||||||
white-space: pre-wrap;
|
|
||||||
background: rgba(0, 0, 0, 0.6);
|
|
||||||
padding: 15px 20px;
|
|
||||||
border-radius: 10px;
|
|
||||||
max-width: 80vw;
|
|
||||||
text-align: left;
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<script>
|
<script>
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
location.reload();
|
location.reload();
|
||||||
}, {{ reload_interval }}); // Refresh every X ms
|
}, {{ reload_interval }}); // Refresh every 5 seconds
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{% if image %}
|
{% if image %}
|
||||||
<div class="image-container">
|
<img src="{{ url_for('images', filename=image) }}" alt="Latest Image">
|
||||||
<img src="{{ url_for('images', filename=image) }}" alt="Latest Image">
|
|
||||||
</div>
|
|
||||||
{% if prompt %}
|
|
||||||
<div class="prompt">{{ prompt }}</div>
|
|
||||||
<a href="/images" id="archive-link">Archive</a>
|
|
||||||
{% endif %}
|
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>No images found</p>
|
<p style="color: white;">No images found</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user