From cbc51babbe5255bc1378d6d5ec434f7b62866249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Thu, 11 Apr 2024 14:22:44 -0700 Subject: [PATCH] services: use asyncio to_thread in moondreamservice --- src/dailyai/services/moondream_ai_service.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/dailyai/services/moondream_ai_service.py b/src/dailyai/services/moondream_ai_service.py index 2b9835404..704d4c51b 100644 --- a/src/dailyai/services/moondream_ai_service.py +++ b/src/dailyai/services/moondream_ai_service.py @@ -1,3 +1,5 @@ +import asyncio + from dailyai.pipeline.frames import ImageFrame, VisionImageFrame from dailyai.services.ai_services import VisionService @@ -43,10 +45,15 @@ def __init__( self._model.eval() async def run_vision(self, frame: VisionImageFrame) -> str: - image = Image.frombytes("RGB", (frame.size[0], frame.size[1]), frame.image) - image_embeds = self._model.encode_image(image) - description = self._model.answer_question( - image_embeds=image_embeds, - question=frame.text, - tokenizer=self._tokenizer) + def get_image_description(frame: VisionImageFrame): + image = Image.frombytes("RGB", (frame.size[0], frame.size[1]), frame.image) + image_embeds = self._model.encode_image(image) + description = self._model.answer_question( + image_embeds=image_embeds, + question=frame.text, + tokenizer=self._tokenizer) + return description + + description = await asyncio.to_thread(get_image_description, frame) + return description