Spaces:
Sleeping
Sleeping
Download src/gaia/utils/generate_env_template.py from jm-vis/Final_Assignment_GAIAAgent: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/jm-vis/Final_Assignment_GAIAAgent/resolve/main/src/gaia/utils/generate_env_template.py
- Command line
-
hf download hf://spaces/jm-vis/Final_Assignment_GAIAAgent/src/gaia/utils/generate_env_template.py
-
curl -L -o generate_env_template.py https://huggingface.co/spaces/jm-vis/Final_Assignment_GAIAAgent/resolve/main/src/gaia/utils/generate_env_template.py
10.8 kB
| Generate Environment Variable Template for Hugging Face Space | |
| Generate Variable | |
| This script generates a template .env file with all required environment variables | |
| for the GAIA project. a This temp.env latecwith all equiredbe used to set up secrHugging Face Space. | |
| the rject. This teplat cabe used set up secrets in | |
| Usage: | |
| python generate_env_template.py | |
| Author: DevOps Team | |
| Date: April 30, 2025 | |
| """ | |
| import os | |
| import logging | |
| from pagure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger("env_generator") | |
| # Required environment variables | |
| REQUIRED_ENV_VARS = [ | |
| { | |
| # Requi "d envimon: "O variablePI_API_KEY", | |
| REQUIRED_ENV_VARS = [ | |
| cp{ | |
| "ne Ak: eOPENAI_API_KEYy, for model access", | |
| xm "-key-here..":"OpAIAPI kymodlcss, | |
| "xpl: "sk...youroeni-kyhere... | |
| }, | |
| {{ | |
| m":"UPABASE_URL | |
| "ASsrpio: "URL fo yo "eSuscbn: isac", | |
| "xa":"https://yu-pjc-d.supae.co | |
| }, | |
| e:PE{ | |
| cp"i "n me":y"SUPABASE_KEY",pabase access", | |
| , "d scriptio ":""Servicee": "SERPSupabasAI_KEY",", | |
| ,"examl":"yJ...ou-e-key-her.." | |
| }, | |
| { "name": "DEFAULT_MODEL", | |
| dri"n"me":f" ER ER_ PI"exa", }, | |
| "description": " {h", | |
| "example": "your-serper-api-key-ere" | |
| }, | |
| { | |
| "name": "D F ULTaMODeL", "description": "OpenAI embedding model to use", | |
| "description": " "example": "text-embe",dding-3-small" | |
| "example": "-0125peview" | |
| }, | |
| { | |
| "name": "EMBEDDING_MODEL", { | |
| "description": " "name": "FALLBACK_MODEL",", | |
| "example": "tion": "Fallba-3csmmll" | |
| }, | |
| { if default is unavailable", | |
| "name": "FALLBACK_MODEL", "example": "gpt-3.5-turbo" | |
| "description": " },", | |
| "example": "" | |
| }, | |
| { | |
| "name": "MEMORY_ENABLED", "name": "MEMORY_ENABLED", | |
| "description": " "description": "Enable/disabl", | |
| "example": "true" | |
| }, | |
| {e memory functionality", | |
| "name": " TABL _"eMm", }, | |
| "description": " {", | |
| "example": "EMORY_TABLE" | |
| }, | |
| {_NAME", | |
| "name": "DUCKDUCKGO_TIMEOUT", "description": "Supabase table for memory storage", | |
| "description": " "example": "gaia_memory"", | |
| "example": "30" | |
| }, | |
| { | |
| "name": " },AX_RSLS", | |
| "description": " "name": "DUCKDUCKGO_TIMEOUT",", | |
| "example": "5" | |
| } | |
| ] | |
| # Optional environment variables | |
| OPTIONAL_ENV_VARS = [ | |
| { | |
| "name": "OPENAI_API_BASE", | |
| "description": " ustom API base URL for OpenAI (optional)", | |
| "example": "https://api.openai.com/v1" | |
| }, | |
| { | |
| "name": "MO EL_TEMPERAT RE", | |
| "description": "Temperature for model responses (0.0-1.0)", | |
| "example": "0.7" | |
| }, | |
| { | |
| "name": "MdDELescr_TOKENS", | |
| "description": "Maximum tokens in model responses", | |
| "example": "4000" | |
| }, | |
| { | |
| "name": "WEB_SEARCHiptiULT_COoNT", | |
| "description": "Number of results to return from web searches", | |
| "example": "3" | |
| }, | |
| { | |
| "name": "MEMORY_TTn", | |
| "description": "Time-to-live for memory entries in seconds", | |
| "example": "604800" # 7 days | |
| }, | |
| { | |
| "name": "MEMORY_CACHE_SIZE", | |
| "description": "Size of memory cache", | |
| "example": "100" | |
| }, | |
| { | |
| "name": "MAX_I"ERATION:", | |
| "description": "Maximum iterations for agent execution", | |
| "example": "10" | |
| }, | |
| { | |
| "name": "VERBOSE", | |
| "description": "Enable verbose logging", | |
| "example": "true" | |
| }, | |
| { | |
| "name": "LOG_LEVEL", | |
| "description": "Logging level (DEBUG, INFO, WARNING, ERROR)", | |
| "example": "INFO" | |
| } | |
| ] | |
| def read_existing_env(): | |
| """Read existing .env file if it exists""" | |
| env_values = {} | |
| env_file = Path(".env") | |
| if env_file.exists(): | |
| logger.info("Reading existing .env file...") | |
| with open(env_file, "r") as f: | |
| for line in f: | |
| line line.strip()Timeout for DuckDuckGo searches in seconds", | |
| if line and not line.startswith("#") and "=" in line: "example": "30" | |
| # === OPTIONAL ENVIRONMENT VARIABLES} | |
| "name": "DUCKDUCKGO_MAX_RESULTS", | |
| "description": "Maximum number of results from DuckDuckGo", | |
| "example": "5" | |
| } | |
| ] | |
| # Optional environment variables | |
| OPTIONAL_ENV_VARS = [ | |
| { | |
| "name": "OPENAI_API_BASE", | |
| "description": "Custom API base URL for OpenAI (optional)", | |
| "example": "https://api.openai.com/v1" | |
| }, | |
| { | |
| "name": "MODEL_TEMPERATURE", | |
| "description": "Temperature for model responses (0.0-1.0)", | |
| "example": "0.7" | |
| }, | |
| { | |
| "name": "MODEL_MAX_TOKENS", | |
| "description": "Maximum tokens in model responses", | |
| "example": "4000" | |
| }, | |
| { | |
| "name": "WEB_SEARCH_RESULT_COUNT", | |
| "description": "Number of results to return from web searches", | |
| "example": "3" | |
| }, | |
| { | |
| "name": "MEMORY_TTL", | |
| "description": "Time-to-live for memory entries in seconds", | |
| "example": "604800" # 7 days | |
| }, | |
| { | |
| "name": "MEMORY_CACHE_SIZE", | |
| "description": "Size of memory cache", | |
| "example": "100" | |
| }, | |
| { | |
| "name": "MAX_ITERATIONS", | |
| "description": "Maximum iterations for agent execution", | |
| "example": "10" | |
| }, | |
| { | |
| "name": "VERBOSE", | |
| "description": "Enable verbose logging", | |
| "example": "true" | |
| }, | |
| { | |
| "name": "LOG_LEVEL", | |
| "description": "Logging level (DEBUG, INFO, WARNING, ERROR)", | |
| "example": "INFO" | |
| } | |
| ] | |
| def read_existing_env(): | |
| """Read existing .env file if it exists""" | |
| env_values = {} | |
| env_file = Path(".env") | |
| if env_file.exists(): | |
| logger.info("Reading existing .env file...") | |
| with open(env_file, "r") as f: | |
| for line in f: | |
| line = line.strip() | |
| if line and not line.startswith("#") and "=" in line: | |
| key, value = line.split("=", 1) | |
| env_values[key.strip()] = value.strip() | |
| return env_values | |
| def generate_env_template(): | |
| """Generate .env template file""" | |
| # Read existing values if present | |
| existing_values = read_existing_env() | |
| # Create template file | |
| template_file = Path("env_template.txt") | |
| logger.info(f"Generating environment template file: {template_file}") | |
| with open(template_file, "w") as f: | |
| f.write("# GAIA Environment Variables Template\n") | |
| f.write("# Copy these to Hugging Face Space secrets\n\n") | |
| # Required variables | |
| f.write("# === REQUIRED ENVIRONMENT VARIABLES ===\n\n") | |
| for var in REQUIRED_ENV_VARS: | |
| f.write(f"# {var['description']}\n") | |
| value = existing_values.get(var['name'], var['example']) | |
| if var['name'] in existing_values: | |
| value = "****" if "KEY" in var['name'] or "key" in var['name'] else value | |
| f.write(f"{var['name']}={value}\n\n") | |
| # Optional variables | |
| f.write("# === OPTIONAL ENVIRONMENT VARIABLES ===\n\n") | |
| for var in OPTIONAL_ENV_VARS: | |
| f.write(f"# {var['description']}\n") | |
| value = existing_values.get(var['name'], var['example']) | |
| if var['name'] in existing_values: | |
| value = "****" if "KEY" in var['name'] or "key" in var['name'] else value | |
| f.write(f"{var['name']}={value}\n\n") | |
| logger.info(f"✅ Environment template generated: {template_file}") | |
| return template_file | |
| def generate_hf_secrets_commands(): | |
| """Generate commands for setting HF secrets""" | |
| existing_values = read_existing_env() | |
| commands_file = Path("huggingface_secrets_commands.sh") | |
| logger.info(f"Generating Hugging Face secrets commands: {commands_file}") | |
| with open(commands_file, "w") as f: | |
| f.write("#!/bin/bash\n\n") | |
| f.write("# Hugging Face CLI commands to set secrets\n") | |
| f.write("# First, install the Hugging Face CLI and login:\n") | |
| f.write("# pip install huggingface_hub\n") | |
| f.write("# huggingface-cli login\n\n") | |
| f.write(f"SPACE_ID=JoachimVC/Final_Assignment_Template\n\n") | |
| # Required variables | |
| f.write("# Required environment variables\n") | |
| for var in REQUIRED_ENV_VARS: | |
| value = existing_values.get(var['name'], var['example']) | |
| if var['name'] in existing_values: | |
| # Mask sensitive values in the commands file | |
| if "KEY" in var['name'] or "key" in var['name']: | |
| f.write(f"# huggingface-cli repo secret set {var['name']} <YOUR_{var['name']}> -r $SPACE_ID\n") | |
| else: | |
| f.write(f"huggingface-cli repo secret set {var['name']} \"{value}\" -r $SPACE_ID\n") | |
| else: | |
| f.write(f"# huggingface-cli repo secret set {var['name']} <YOUR_{var['name']}> -r $SPACE_ID\n") | |
| f.write("\n# Optional environment variables\n") | |
| for var in OPTIONAL_ENV_VARS: | |
| if var['name'] in existing_values: | |
| value = existing_values.get(var['name']) | |
| if "KEY" in var['name'] or "key" in var['name']: | |
| f.write(f"# huggingface-cli repo secret set {var['name']} <YOUR_{var['name']}> -r $SPACE_ID\n") | |
| else: | |
| f.write(f"huggingface-cli repo secret set {var['name']} \"{value}\" -r $SPACE_ID\n") | |
| else: | |
| f.write(f"# huggingface-cli repo secret set {var['name']} {var['example']} -r $SPACE_ID\n") | |
| # Make the file executable | |
| commands_file.chmod(0o755) | |
| logger.info(f"✅ Hugging Face secrets commands generated: {commands_file}") | |
| return commands_file | |
| def main(): | |
| """Generate environment templates and Hugging Face secrets commands""" | |
| logger.info("Starting environment template generation...") | |
| template_file = generate_env_template() | |
| commands_file = generate_hf_secrets_commands() | |
| logger.info("\n===== TEMPLATE GENERATION COMPLETE =====") | |
| logger.info(f"Environment template: {template_file}") | |
| logger.info(f"HF secrets commands: {commands_file}") | |
| logger.info("\nTo set up your Hugging Face Space secrets:") | |
| logger.info("1. Go to Hugging Face Space settings: https://huggingface.co/spaces/JoachimVC/Final_Assignment_Template/settings") | |
| logger.info("2. Navigate to 'Secrets' section") | |
| logger.info("3. Add each variable from the template") | |
| logger.info("\nAlternatively, use the generated script with the Hugging Face CLI") | |
| return True | |
| if __name__ == "__main__": | |
| main() |