Skip to content

Commit

Permalink
Update for AppAPI 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bigcat88 committed Jan 25, 2024
1 parent e9f0c1a commit 8d0758a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
26 changes: 13 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,42 @@ 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.1.0 --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:2.0.0 --tag ghcr.io/cloud-py-api/talk_bot_ai_example:latest .

.PHONY: deploy28
deploy28:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:deploy talk_bot_ai_example docker_dev \
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:deploy talk_bot_ai_example \
--info-xml https://raw.githubusercontent.com/cloud-py-api/talk_bot_ai_example/main/appinfo/info.xml

.PHONY: run28
run28:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register talk_bot_ai_example docker_dev -e --force-scopes \
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register talk_bot_ai_example --force-scopes \
--info-xml https://raw.githubusercontent.com/cloud-py-api/talk_bot_ai_example/main/appinfo/info.xml

.PHONY: deploy27
deploy27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:deploy talk_bot_ai_example docker_dev \
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:deploy talk_bot_ai_example \
--info-xml https://raw.githubusercontent.com/cloud-py-api/talk_bot_ai_example/main/appinfo/info.xml

.PHONY: run27
run27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register talk_bot_ai_example docker_dev -e --force-scopes \
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register talk_bot_ai_example --force-scopes \
--info-xml https://raw.githubusercontent.com/cloud-py-api/talk_bot_ai_example/main/appinfo/info.xml

.PHONY: register28
register28:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register talk_bot_ai_example manual_install --json-info \
"{\"appid\":\"talk_bot_ai_example\",\"name\":\"TalkBotAI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":10034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"talk_bot_ai_example\",\"name\":\"TalkBotAI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":10034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register27
register27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai_example --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register talk_bot_ai_example manual_install --json-info \
"{\"appid\":\"talk_bot_ai_example\",\"name\":\"TalkBotAI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":10034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"talk_bot_ai_example\",\"name\":\"TalkBotAI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":10034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish
4 changes: 2 additions & 2 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
]]></description>
<version>1.1.0</version>
<version>2.0.0</version>
<licence>MIT</licence>
<author mail="andrey18106x@gmail.com" homepage="https://github.com/andrey18106">Andrey Borysenko</author>
<author mail="bigcat88@icloud.com" homepage="https://github.com/bigcat88">Alexander Piskun</author>
Expand All @@ -32,7 +32,7 @@ Refer to [How to install](https://github.com/cloud-py-api/talk_bot_ai_example/bl
<docker-install>
<registry>ghcr.io</registry>
<image>cloud-py-api/talk_bot_ai_example</image>
<image-tag>1.1.0</image-tag>
<image-tag>2.0.0</image-tag>
</docker-install>
<scopes>
<required>
Expand Down
30 changes: 16 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

APP = FastAPI()
MODEL_NAME = "MBZUAI/LaMini-Flan-T5-248M"
BOT_URL = "/ai_talk_bot_example"
BOT_URL = "ai_talk_bot_example"


@dataclasses.dataclass
Expand Down Expand Up @@ -65,7 +65,9 @@ def conversation_name(self) -> str:


def get_nc_url() -> str:
return os.environ["NEXTCLOUD_URL"].removesuffix("/index.php").removesuffix("/")
nc_url = os.environ["NEXTCLOUD_URL"].removesuffix("/index.php").removesuffix("/")
print(nc_url)
return nc_url


def sign_request(headers: dict, user="") -> None:
Expand Down Expand Up @@ -177,17 +179,8 @@ def send_message(
return _sign_send_request("POST", f"/{token}/message", params, message), reference_id


def talk_bot_app(request: Request) -> TalkBotMessage:
def talk_bot_msg(request: Request) -> TalkBotMessage:
body = asyncio.run(request.body())
secret = get_bot_secret(request.url.components.path)
if not secret:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
hmac_sign = hmac.new(
secret, request.headers.get("X-NEXTCLOUD-TALK-RANDOM", "").encode("UTF-8"), digestmod=hashlib.sha256
)
hmac_sign.update(body)
if request.headers["X-NEXTCLOUD-TALK-SIGNATURE"] != hmac_sign.hexdigest():
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
return TalkBotMessage(json.loads(body))


Expand All @@ -203,11 +196,16 @@ def ai_talk_bot_process_request(message: TalkBotMessage):
send_message(response_text, message)


@APP.post(BOT_URL)
@APP.post("/" + BOT_URL)
async def ai_talk_bot(
message: Annotated[TalkBotMessage, Depends(talk_bot_app)],
message: Annotated[TalkBotMessage, Depends(talk_bot_msg)],
background_tasks: BackgroundTasks,
request: Request
):
try:
sign_check(request)
except ValueError:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
if message.object_name == "message":
background_tasks.add_task(ai_talk_bot_process_request, message)
return requests.Response()
Expand Down Expand Up @@ -255,13 +253,17 @@ def update_progress_status(progress: int):


def fetch_models_task():
print("starting model download")

class TqdmProgress(tqdm.tqdm):
def display(self, msg=None, pos=None):
finish_percent = min(int(self.n * 100 / self.total), 100)
print(f"progress: {finish_percent}")
update_progress_status(finish_percent)
return super().display(msg, pos)

snapshot_download(MODEL_NAME, cache_dir=os.environ["APP_PERSISTENT_STORAGE"], tqdm_class=TqdmProgress) # noqa
print(f"progress: 100")
update_progress_status(100)


Expand Down

0 comments on commit 8d0758a

Please sign in to comment.