Spaces:
Running
Running
File size: 2,842 Bytes
f0ca218 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
"""
Application settings loaded from environment variables.
This module handles loading and validation of environment variables
for the Likable application.
"""
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class Settings:
"""Application settings loaded from environment variables."""
def __init__(self):
"""Initialize settings from environment variables."""
self.model_id: str = os.getenv("MODEL_ID", "Qwen/Qwen2.5-Coder-32B-Instruct")
self.api_base_url: str | None = os.getenv("API_BASE_URL")
self.api_key: str | None = os.getenv("API_KEY")
# Application Settings
self.gradio_host: str = os.getenv("GRADIO_HOST", "127.0.0.1")
self.gradio_port: int = int(os.getenv("GRADIO_PORT", "7860"))
self.gradio_debug: bool = os.getenv("GRADIO_DEBUG", "false").lower() == "true"
# Validate critical settings
self._validate()
def _validate(self):
"""Validate critical settings and provide helpful error messages."""
if not self.api_key:
print("⚠️ Warning: API_KEY not set in environment variables.")
print(" The agent may not work without a valid API key.")
print(" Set it in your .env file or as an environment variable.")
print()
def get_model_config(self) -> dict:
"""Get model configuration for the agent."""
config = {"model_id": self.model_id, "api_key": self.api_key}
if self.api_base_url:
config["api_base_url"] = self.api_base_url
if self.api_key:
config["api_key"] = self.api_key
return config
def get_gradio_config(self) -> dict:
"""Get Gradio launch configuration."""
return {
"server_name": self.gradio_host,
"server_port": self.gradio_port,
"debug": self.gradio_debug,
}
def __repr__(self) -> str:
"""String representation of settings (excluding sensitive data)."""
return f"""Settings(
model_id='{self.model_id}',
api_key={'***' if self.api_key else 'None'},
api_base_url='{self.api_base_url}',
gradio_host='{self.gradio_host}',
gradio_port={self.gradio_port},
gradio_debug={self.gradio_debug}
)"""
# Global settings instance
settings = Settings()
# Convenience functions for backward compatibility
def get_api_key() -> str | None:
"""Get API key."""
return settings.api_key
def get_model_id() -> str:
"""Get model ID."""
return settings.model_id
if __name__ == "__main__":
print("Current Settings:")
print("=" * 50)
print(settings)
print()
print("Model Config:")
print(settings.get_model_config())
print()
print("Gradio Config:")
print(settings.get_gradio_config())
|