File size: 2,717 Bytes
e105e80
 
 
 
 
 
 
 
4b828b4
e105e80
 
 
 
 
 
4b828b4
e105e80
4b828b4
e105e80
 
 
 
 
 
 
4b828b4
e105e80
4b828b4
e105e80
4b828b4
e105e80
 
 
 
 
 
 
 
 
 
1535508
e105e80
1535508
e105e80
d3e6b6b
e105e80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b828b4
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
import os
from typing import Final
from pydantic import BaseModel, Field

from dotenv import load_dotenv
load_dotenv()

class AppConfig(BaseModel):
    API_BASE_URL: str = Field(default=os.getenv("API_BASE_URL", "https://1251722089-fabl8rzpod.ap-singapore.tencentscf.com"), description="Backend API base URL")
    API_AUTH_TOKEN: str = Field(default=os.getenv("API_AUTH_TOKEN", ""), description="API authentication token - REQUIRED")
    API_TIMEOUT: int = Field(default=int(os.getenv("API_TIMEOUT", "120")), description="API request timeout in seconds")

    def model_post_init(self, __context) -> None:
    
        if not self.API_AUTH_TOKEN:
            raise ValueError("❌ API_AUTH_TOKEN environment variable must be set")

        # Simplified forbidden token check
        forbidden_tokens = {
            "",
            "your_secret_token_here",
            "your_api_token_here"
        }

        if self.API_AUTH_TOKEN in forbidden_tokens:
            raise ValueError(f"❌ Forbidden placeholder token: {self.API_AUTH_TOKEN}")

        # Simplified: only require basic length
        if len(self.API_AUTH_TOKEN) < 8:
            raise ValueError("❌ Authentication token must be at least 8 characters long")
    
    # Polling Configuration
    POLL_INTERVAL: float = Field(default=3.0, description="Status polling interval in seconds")
    MAX_POLL_ATTEMPTS: int = Field(default=900, description="Maximum polling attempts (45 minutes for new workflow)")
    
    # UI Configuration
    APP_TITLE: str = Field(default="AI Image Generator", description="Application title")
    APP_DESCRIPTION: str = Field(default="Robust AI image generation with clean state management", description="App description")

    # Server Configuration
    SERVER_HOST: str = Field(default=os.getenv("SERVER_HOST", "127.0.0.1"), description="Server bind address")
    SERVER_PORT: int = Field(default=int(os.getenv("SERVER_PORT", "7860")), description="Server port")
    ENABLE_SHARE: bool = Field(default=os.getenv("ENABLE_SHARE", "false").lower() == "true", description="Enable Gradio public sharing")



    # Logging Configuration
    LOG_LEVEL: str = Field(default="WARNING", description="Logging level")
    ENABLE_DEBUG_LOGGING: bool = Field(default=False, description="Enable detailed debug logging")

CONFIG: Final[AppConfig] = AppConfig()

def get_api_headers() -> dict[str, str]:

    return {
        "Content-Type": "application/json",
        "X-API-Auth": CONFIG.API_AUTH_TOKEN
    }

def get_api_url(endpoint: str) -> str:
    """Construct full API URL from endpoint"""
    base_url = str(CONFIG.API_BASE_URL).rstrip('/')
    endpoint_clean = endpoint.lstrip('/')
    return f"{base_url}/{endpoint_clean}"