o
    i                  
   @   s  d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZ ddlmZ dd	lmZ zddlZdd
lmZ W n  eyh Z zede  ed ede dZ[ww dd ZeG dd deZG dd deZdS )zMoondream vision service implementation.

This module provides integration with the Moondream vision-language model
for image analysis and description generation.
    N)	dataclass)AsyncGeneratorOptional)logger)Image)
ErrorFrameFrameUserImageRawFrameVisionFullResponseEndFrameVisionFullResponseStartFrameVisionTextFrame)VisionSettings)VisionService)AutoModelForCausalLMzException: zKIn order to use Moondream, you need to `pip install pipecat-ai[moondream]`.zMissing module(s): c                  C   s   zddl } tj rtdtjfW S W n	 ty   Y nw tj r+tdtjfS tj	j
 r9tdtjfS tdtjfS )aM  Detect the appropriate device to run on.

    Detects available hardware acceleration and selects the best device
    and data type for optimal performance.

    Returns:
        tuple: A tuple containing (device, dtype) where device is a torch.device
               and dtype is the recommended torch data type for that device.
    r   Nxpucudampscpu)intel_extension_for_pytorchtorchr   is_availabledevicefloat32ImportErrorr   float16backendsr   )r    r   U/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/moondream/vision.pydetect_device(   s   


r   c                   @   s   e Zd ZdZdS )MoondreamSettingszkSettings for the Moondream vision service.

    Parameters:
        model: Moondream model identifier.
    N)__name__
__module____qualname____doc__r   r   r   r   r   A   s    r   c                       sf   e Zd ZU dZeZeed< ddddddee dee f fd	d
Z	de
deedf fddZ  ZS )MoondreamServicezMoondream vision-language model service.

    Provides image analysis and description generation using the Moondream
    vision-language model. Supports various hardware acceleration options
    including CUDA, MPS, and Intel XPU.
    	_settingsNz
2025-01-09F)modelrevisionuse_cpusettingsr&   r)   c          	         s   | j dd}|dur| dd ||_|dur|| t jdd|i| |s/t \}}ntd}tj	}t
d tj| jjd|d	|i|d
 | _t
d dS )am  Initialize the Moondream service.

        Args:
            model: Hugging Face model identifier for the Moondream model.

                .. deprecated:: 0.0.105
                    Use ``settings=MoondreamService.Settings(model=...)`` instead.

            revision: Specific model revision to use.
            use_cpu: Whether to force CPU usage instead of hardware acceleration.
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional arguments passed to the parent VisionService.
        zvikhyatk/moondream2)r&   Nr&   r)   r   zLoading Moondream model...T )trust_remote_coder'   
device_mapdtypezLoaded Moondream modelr   )Settings"_warn_init_param_moved_to_settingsr&   apply_updatesuper__init__r   r   r   r   r   debugr   from_pretrainedr%   eval_model)	selfr&   r'   r(   r)   kwargsdefault_settingsr   r-   	__class__r   r   r2   U   s,   


zMoondreamService.__init__framereturnc                   s   j stdV  dS tdt j d dtdtt dtf fdd	}t	
| j jI dH }t V  t|d
V  t V  dS )zsAnalyze an image and generate a description.

        Args:
            frame: The image frame to process.
        zMoondream model not availableNzAnalyzing image (bytes length: )image_bytestextr=   c                    s4   t  j j| }j|}j||d }|S )Nanswer)r   	frombytesformatsizer6   encode_imagequery)r?   r@   imageimage_embedsdescriptionr<   r7   r   r   get_image_description   s   z:MoondreamService.run_vision.<locals>.get_image_description)r@   )r6   r   r   r3   lenrG   bytesr   strasyncio	to_threadr@   r   r   r
   )r7   r<   rK   rI   r   rJ   r   
run_vision   s   
 zMoondreamService.run_vision)r    r!   r"   r#   r   r.   __annotations__r   rN   r2   r	   r   r   rQ   __classcell__r   r   r:   r   r$   J   s   
 "7r$   ) r#   rO   dataclassesr   typingr   r   logurur   PILr   pipecat.frames.framesr   r   r	   r
   r   r   pipecat.services.settingsr   pipecat.services.vision_servicer   r   transformersr   ModuleNotFoundErroreerror	Exceptionr   r   r$   r   r   r   r   <module>   s,    
