Skip to content

Commit a24aba7

Browse files
committed
feat: image generation tool
Signed-off-by: Jana Peper <[email protected]>
1 parent a264ec8 commit a24aba7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

ex_app/lib/all_tools/image_gen.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
from langchain_core.tools import tool
4+
from nc_py_api import Nextcloud
5+
6+
from ex_app.lib.all_tools.lib.files import get_file_id_from_file_url
7+
from ex_app.lib.all_tools.lib.task_processing import run_task
8+
from ex_app.lib.all_tools.lib.decorator import safe_tool
9+
10+
11+
def get_tools(nc: Nextcloud):
12+
13+
@tool
14+
@safe_tool
15+
def generate_image(input: str) -> str:
16+
"""
17+
Generate an image with the input string as description
18+
:param text: the instructions for the image generation
19+
:return: a download link to the generated image
20+
"""
21+
tasktype = "core:text2image"
22+
task_input = {
23+
'input': input,
24+
'numberOfImages': 1,
25+
}
26+
task = run_task(nc, tasktype, task_input)
27+
28+
return f"https://nextcloud.local/ocs/v2.php/apps/assistant/api/v1/task/{task.id}/output-file/{task.output['images'][0]}/download"
29+
30+
return [
31+
generate_image,
32+
]
33+
34+
def get_category_name():
35+
return "Image generation"
36+
37+
def is_available(nc: Nextcloud):
38+
tasktypes = nc.ocs('GET', '/ocs/v2.php/taskprocessing/tasktypes')['types'].keys()
39+
return 'core:text2image' in tasktypes

ex_app/lib/all_tools/lib/task_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def run_task(nc, type, task_input):
6161
if task.status != "STATUS_SUCCESSFUL":
6262
raise Exception("Nextcloud TaskProcessing Task failed")
6363

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

6767
return task

0 commit comments

Comments
 (0)