deps.py 1.93 KB
Newer Older
PengGao's avatar
PengGao committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from pathlib import Path
from typing import Optional
from urllib.parse import urlparse

import httpx
from loguru import logger

from ..services import DistributedInferenceService, FileService, ImageGenerationService, VideoGenerationService


class ServiceContainer:
    _instance: Optional["ServiceContainer"] = None

    def __init__(self):
        self.file_service: Optional[FileService] = None
        self.inference_service: Optional[DistributedInferenceService] = None
        self.video_service: Optional[VideoGenerationService] = None
        self.image_service: Optional[ImageGenerationService] = None
        self.max_queue_size: int = 10

    @classmethod
    def get_instance(cls) -> "ServiceContainer":
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    def initialize(self, cache_dir: Path, inference_service: DistributedInferenceService, max_queue_size: int = 10):
        self.file_service = FileService(cache_dir)
        self.inference_service = inference_service
        self.video_service = VideoGenerationService(self.file_service, inference_service)
        self.image_service = ImageGenerationService(self.file_service, inference_service)
        self.max_queue_size = max_queue_size


def get_services() -> ServiceContainer:
    return ServiceContainer.get_instance()


async def validate_url_async(url: str) -> bool:
    if not url or not url.startswith("http"):
        return True

    try:
        parsed_url = urlparse(url)
        if not parsed_url.scheme or not parsed_url.netloc:
            return False

        timeout = httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0)
        async with httpx.AsyncClient(verify=False, timeout=timeout) as client:
            response = await client.head(url, follow_redirects=True)
            return response.status_code < 400
    except Exception as e:
        logger.warning(f"URL validation failed for {url}: {str(e)}")
        return False