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
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ See [the nextcloud admin docs](https://docs.nextcloud.com/server/latest/admin_ma
<repository type="git">https://github.com/nextcloud/llm2</repository>
<screenshot>https://raw.githubusercontent.com/nextcloud/llm2/main/img/Logo.png</screenshot>
<dependencies>
<nextcloud min-version="30" max-version="32"/>
<nextcloud min-version="30" max-version="33"/>
</dependencies>
<external-app>
<docker-install>
Expand Down
26 changes: 20 additions & 6 deletions lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ def log(nc, level, content):
"https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/4f0c246f125fc7594238ebe7beb1435a8335f519/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf": { "save_path": os.path.join(persistent_storage(), "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf") },
}
app_enabled = Event()

trigger = Event()
CHECK_INTERVAL = 5
CHECK_INTERVAL_WITH_TRIGGER = 5 * 60
CHECK_INTERVAL_ON_ERROR = 10

@asynccontextmanager
async def lifespan(_app: FastAPI):
set_handlers(
APP,
enabled_handler, # type: ignore
models_to_fetch=models_to_fetch,
trigger_handler=trigger_handler
)
nc = NextcloudApp()
if nc.enabled_state:
Expand Down Expand Up @@ -88,11 +92,11 @@ def background_thread_task():
try:
response = nc.providers.task_processing.next_task(list(provider_ids), list(task_type_ids))
if not response:
sleep(5)
wait_for_tasks()
continue
except (NextcloudException, RequestException, JSONDecodeError) as e:
log(nc, LogLvl.ERROR, f"Network error fetching the next task {e}")
sleep(5)
wait_for_tasks(CHECK_INTERVAL_ON_ERROR)
continue

task = response["task"]
Expand Down Expand Up @@ -122,7 +126,6 @@ def background_thread_task():
# Error when reporting the result
exception_info = traceback.format_exception(type(e), e, e.__traceback__)
log(nc, LogLvl.ERROR, f"Error: {''.join(exception_info)}")
sleep(5)
except Exception as e: # noqa
exception_info = traceback.format_exception(type(e), e, e.__traceback__)
log(nc, LogLvl.ERROR, f"Error: {''.join(exception_info)}")
Expand All @@ -132,8 +135,6 @@ def background_thread_task():
except (NextcloudException, RequestException) as net_err:
log(nc, LogLvl.INFO, f"Network error in reporting the error: {net_err}")

sleep(5)


def start_bg_task():
t = Thread(target=background_thread_task, args=())
Expand Down Expand Up @@ -184,5 +185,18 @@ async def enabled_handler(enabled: bool, nc: AsyncNextcloudApp) -> str:
return ""


def trigger_handler(providerId: str):
print('TRIGGER called')
trigger.set()

def wait_for_tasks(interval = None):
global CHECK_INTERVAL
global CHECK_INTERVAL_WITH_TRIGGER
actual_interval = CHECK_INTERVAL if interval is None else interval
if trigger.wait(timeout=actual_interval):
CHECK_INTERVAL = CHECK_INTERVAL_WITH_TRIGGER
trigger.clear()


if __name__ == "__main__":
run_app("main:APP", log_level="trace")
Loading
Loading