From 9992d303d84e981ce1a66cd886822ae3ace98837 Mon Sep 17 00:00:00 2001 From: Alexander Piskun Date: Tue, 17 Oct 2023 16:08:01 +0300 Subject: [PATCH 1/4] "/init" endpoint implementation --- Makefile | 2 +- appinfo/info.xml | 4 ++-- requirements.txt | 1 + src/main.py | 47 +++++++++++++++++++++++++++++------------------ 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index b6e788b..e49f564 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ help: .PHONY: build-push build-push: docker login ghcr.io - docker buildx build --push --platform linux/arm64/v8,linux/amd64 --tag ghcr.io/cloud-py-api/talk_bot_ai_example:1.0.3 --tag ghcr.io/cloud-py-api/talk_bot_ai_example:latest . + docker buildx build --push --platform linux/arm64/v8,linux/amd64 --tag ghcr.io/cloud-py-api/talk_bot_ai_example:1.1.0 --tag ghcr.io/cloud-py-api/talk_bot_ai_example:latest . .PHONY: deploy28 deploy28: diff --git a/appinfo/info.xml b/appinfo/info.xml index 7aab037..d5a2188 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -15,7 +15,7 @@ It shouldn't be too difficult to use a more advanced model based on this example Refer to [How to install](https://github.com/cloud-py-api/talk_bot_ai_example/blob/main/HOW_TO_INSTALL.md) to try it. ]]> - 1.0.3 + 1.1.0 MIT Andrey Borysenko Alexander Piskun @@ -32,7 +32,7 @@ Refer to [How to install](https://github.com/cloud-py-api/talk_bot_ai_example/bl ghcr.io cloud-py-api/talk_bot_ai_example - 1.0.3 + 1.1.0 diff --git a/requirements.txt b/requirements.txt index 4574ac8..2b91289 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ httpx>=0.24.1 fastapi>=0.101 uvicorn[standard]>=0.23.2 transformers[torch]>=4.33 +tqdm diff --git a/src/main.py b/src/main.py index 63db56f..8c395eb 100644 --- a/src/main.py +++ b/src/main.py @@ -1,14 +1,9 @@ """Example of an application that uses Python Transformers library with Talk Bot APIs.""" import os - -# This line should be on top before any import of the "Transformers" library. -os.environ["TRANSFORMERS_CACHE"] = os.environ["APP_PERSISTENT_STORAGE"] # noqa - import asyncio import dataclasses import re -from threading import Thread from typing import Annotated from base64 import b64encode, b64decode from random import choice @@ -20,9 +15,11 @@ import httpx import json import requests +import tqdm from fastapi import BackgroundTasks, Depends, FastAPI, responses, Request, HTTPException, status from transformers import pipeline import uvicorn +from huggingface_hub import snapshot_download APP = FastAPI() MODEL_NAME = "MBZUAI/LaMini-Flan-T5-248M" @@ -199,7 +196,7 @@ def ai_talk_bot_process_request(message: TalkBotMessage): r = re.search(r"@assistant\s(.*)", message.object_content["message"], re.IGNORECASE) if r is None: return - model = pipeline("text2text-generation", model=MODEL_NAME) + model = pipeline("text2text-generation", model=snapshot_download(MODEL_NAME, local_files_only=True)) response_text = model(r.group(1), max_length=64, do_sample=True)[0]["generated_text"] send_message(response_text, message) @@ -241,21 +238,35 @@ def enabled_handler(enabled: bool, request: Request): return responses.JSONResponse(content={"error": r}, status_code=200) -def download_models(): - pipeline("text2text-generation", model=MODEL_NAME) - - @APP.get("/heartbeat") def heartbeat_handler(): - global MODEL_INIT_THREAD print("heartbeat_handler: called") - if MODEL_INIT_THREAD is None: - MODEL_INIT_THREAD = Thread(target=download_models) - MODEL_INIT_THREAD.start() - print("heartbeat_handler: started initialization thread") - r = "init" if MODEL_INIT_THREAD.is_alive() else "ok" - print(f"heartbeat_handler: result={r}") - return responses.JSONResponse(content={"status": r}, status_code=200) + return responses.JSONResponse(content={"status": "ok"}, status_code=200) + + +def update_progress_status(progress: int): + ocs_call( + method="PUT", path=f"/index.php/apps/app_api/apps/status/{os.environ['APP_ID']}", json={"progress": progress} + ) + + +def fetch_models_task(): + class TqdmProgress(tqdm.tqdm): + def display(self, msg=None, pos=None): + if init_handler is None: + a = min(int(self.n * 100 / self.total), 100) + print(a) + update_progress_status(a) + return super().display(msg, pos) + + snapshot_download(MODEL_NAME, max_workers=2, cache_dir=os.environ["APP_PERSISTENT_STORAGE"], tqdm_class=TqdmProgress) # noqa + update_progress_status(100) + + +@APP.post("/init") +def init_handler(background_tasks: BackgroundTasks): + background_tasks.add_task(fetch_models_task) + return responses.JSONResponse(content={}, status_code=200) if __name__ == "__main__": From d455831f3be1143b10abcf82875b539f2bc73540 Mon Sep 17 00:00:00 2001 From: Alexander Piskun Date: Wed, 18 Oct 2023 18:54:24 +0300 Subject: [PATCH 2/4] "/init" endpoint = OCS API --- src/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 8c395eb..91d3bbd 100644 --- a/src/main.py +++ b/src/main.py @@ -246,7 +246,9 @@ def heartbeat_handler(): def update_progress_status(progress: int): ocs_call( - method="PUT", path=f"/index.php/apps/app_api/apps/status/{os.environ['APP_ID']}", json={"progress": progress} + method="PUT", + path=f"/ocs/v1.php/apps/app_api/apps/status/{os.environ['APP_ID']}", + json_data={"progress": progress} ) From fd46a9e66db8c62e4adf259e4aeffa979771d2c5 Mon Sep 17 00:00:00 2001 From: Alexander Piskun Date: Sun, 22 Oct 2023 13:20:24 +0300 Subject: [PATCH 3/4] fixed bug + adjustments --- .run/TalkBotAI.run.xml | 5 ++--- Makefile | 4 ++-- src/main.py | 13 +++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.run/TalkBotAI.run.xml b/.run/TalkBotAI.run.xml index 2b9a58b..565898a 100644 --- a/.run/TalkBotAI.run.xml +++ b/.run/TalkBotAI.run.xml @@ -10,13 +10,12 @@ - +