Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions ex_app/lib/all_tools/image_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
from langchain_core.tools import tool
from nc_py_api import Nextcloud

from ex_app.lib.all_tools.lib.files import get_file_id_from_file_url
from ex_app.lib.all_tools.lib.task_processing import run_task
from ex_app.lib.all_tools.lib.decorator import safe_tool


def get_tools(nc: Nextcloud):

@tool
@safe_tool
def generate_image(input: str) -> str:
"""
Generate an image with the input string as description
:param text: the instructions for the image generation
:return: a download link to the generated image
"""
tasktype = "core:text2image"
task_input = {
'input': input,
'numberOfImages': 1,
}
task = run_task(nc, tasktype, task_input)

return f"https://nextcloud.local/ocs/v2.php/apps/assistant/api/v1/task/{task.id}/output-file/{task.output['images'][0]}/download"

return [
generate_image,
]

def get_category_name():
return "Image generation"

def is_available(nc: Nextcloud):
tasktypes = nc.ocs('GET', '/ocs/v2.php/taskprocessing/tasktypes')['types'].keys()
return 'core:text2image' in tasktypes
2 changes: 1 addition & 1 deletion ex_app/lib/all_tools/lib/task_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run_task(nc, type, task_input):
if task.status != "STATUS_SUCCESSFUL":
raise Exception("Nextcloud TaskProcessing Task failed")

if not isinstance(task.output, dict) or ("file" not in task.output and "output" not in task.output):
if not isinstance(task.output, dict) or all(x not in ["file", "output", "images"] for x in task.output):
raise Exception('"output" key not found in Nextcloud TaskProcessing task result')

return task
Loading