Skip to content

Commit

Permalink
services: use asyncio to_thread in moondreamservice
Browse files Browse the repository at this point in the history
  • Loading branch information
aconchillo committed Apr 11, 2024
1 parent b0faafc commit cbc51ba
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/dailyai/services/moondream_ai_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from dailyai.pipeline.frames import ImageFrame, VisionImageFrame
from dailyai.services.ai_services import VisionService

Expand Down Expand Up @@ -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

0 comments on commit cbc51ba

Please sign in to comment.