chat / app.py
rejig-ai's picture
Enhance user profiles and improve interface output formatting
527f6dc
"""
Main application entry point.
"""
import os
import logging
from dotenv import load_dotenv
from ui.chat_interface import create_chat_interface
# Load environment variables
load_dotenv()
# Configure logging
from logging_config import setup_logging
# Set up logging with configuration from environment
log_level = os.getenv("LOG_LEVEL", "DEBUG")
max_log_size_mb = int(os.getenv("MAX_LOG_SIZE_MB", "10"))
log_backup_count = int(os.getenv("LOG_BACKUP_COUNT", "5"))
# Initialize logging
log_filename = setup_logging(
log_level=log_level,
max_file_size_mb=max_log_size_mb,
backup_count=log_backup_count
)
# Validate API key
if not os.getenv("OPENAI_API_KEY"):
raise ValueError("OPENAI_API_KEY environment variable is not set.")
# Create and launch the interface
if __name__ == "__main__":
demo = create_chat_interface()
demo.queue(max_size=20)
# Get the environment or default to development
env = os.getenv("ENVIRONMENT", "development")
# Launch with appropriate settings for HF Spaces
auth_username = os.getenv("GRADIO_AUTH_USERNAME")
auth_password = os.getenv("GRADIO_AUTH_PASSWORD")
# Check if running on HF Spaces
is_hf_spaces = os.getenv("SPACE_ID") is not None
launch_kwargs = {
"share": False,
"ssr_mode": False, # Disable experimental SSR mode for stability
}
# Configure for environment
if is_hf_spaces:
# HF Spaces configuration
launch_kwargs.update({
"server_name": "0.0.0.0",
"server_port": 7860,
})
else:
# Local development configuration
launch_kwargs.update({
"server_name": "127.0.0.1",
"server_port": 7860,
})
# Only add auth if credentials are provided via environment variables
if auth_username and auth_password:
launch_kwargs["auth"] = (auth_username, auth_password)
demo.launch(**launch_kwargs)