File size: 10,781 Bytes
c922f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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()