jens-l commited on
Commit
8922991
Β·
1 Parent(s): 97b5442

feat: implement model initialization based on environment API keys

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +66 -0
README.md CHANGED
@@ -30,4 +30,4 @@ A powerful AI coding assistant that can create and preview Gradio applications i
30
 
31
  ## Docker Deployment
32
 
33
- This Space runs a simplified Docker configuration optimized for Hugging Face Spaces, with the main Gradio application running directly on port 7860.
 
30
 
31
  ## Docker Deployment
32
 
33
+ This Space runs a simplified Docker configuration optimized for Hugging Face Spaces, with the main Gradio application running directly on port 7860.
app.py CHANGED
@@ -427,6 +427,68 @@ def get_default_provider():
427
  return "Anthropic"
428
 
429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
  def save_api_key(provider, api_key):
431
  """Save API key to environment variable and update model accordingly."""
432
  if not api_key.strip():
@@ -917,6 +979,10 @@ if __name__ == "__main__":
917
 
918
  from kiss_agent import KISSAgent
919
 
 
 
 
 
920
  agent = KISSAgent()
921
 
922
  # Start the preview app automatically when the main app starts
 
427
  return "Anthropic"
428
 
429
 
430
+ def initialize_model_from_environment():
431
+ """Initialize the model configuration based on available API keys in environment."""
432
+ default_provider = get_default_provider()
433
+ available_providers = get_available_providers()
434
+ current_model_id = os.getenv("MODEL_ID", "")
435
+
436
+ print(f"πŸ” Available providers: {available_providers}")
437
+ print(f"πŸ” Default provider: {default_provider}")
438
+ print(f"πŸ” Current MODEL_ID: {current_model_id if current_model_id else 'NOT_SET'}")
439
+
440
+ # If we have available providers, check if current model matches the
441
+ # default provider
442
+ if available_providers:
443
+ expected_model = get_default_model_for_provider(default_provider)
444
+
445
+ # If MODEL_ID doesn't match the expected model for available provider,
446
+ # override it
447
+ if current_model_id != expected_model:
448
+ print(
449
+ f"πŸ”§ MODEL_ID mismatch: expected {expected_model} for "
450
+ f"{default_provider}, got {current_model_id}"
451
+ )
452
+ print("πŸ”§ Configuring based on available providers...")
453
+
454
+ # Set the model for the default provider
455
+ if default_provider != "Hugging Face":
456
+ env_var_map = {
457
+ "Anthropic": "ANTHROPIC_API_KEY",
458
+ "OpenAI": "OPENAI_API_KEY",
459
+ "SambaNova": "SAMBANOVA_API_KEY",
460
+ "Mistral": "MISTRAL_API_KEY",
461
+ }
462
+
463
+ env_var_name = env_var_map.get(default_provider)
464
+ api_key = os.getenv(env_var_name)
465
+
466
+ print(
467
+ f"πŸ”§ Checking {default_provider} with key: "
468
+ f"{'SET' if api_key else 'NOT_SET'}"
469
+ )
470
+
471
+ if api_key:
472
+ os.environ["API_KEY"] = api_key
473
+ os.environ["MODEL_ID"] = expected_model
474
+ print(
475
+ f"πŸ”§ Auto-configured model: {default_provider}"
476
+ f"-> {expected_model}"
477
+ )
478
+ print("πŸ”§ Set API_KEY and MODEL_ID environment variables")
479
+ return True
480
+ else:
481
+ print(f"❌ No API key found for {default_provider}")
482
+ else:
483
+ print("πŸ”§ Default provider is Hugging Face, keeping current model")
484
+ else:
485
+ print(f"βœ… MODEL_ID matches expected model for {default_provider}")
486
+ else:
487
+ print("ℹ️ No available providers detected, keeping current model")
488
+
489
+ return False
490
+
491
+
492
  def save_api_key(provider, api_key):
493
  """Save API key to environment variable and update model accordingly."""
494
  if not api_key.strip():
 
979
 
980
  from kiss_agent import KISSAgent
981
 
982
+ # Initialize model configuration based on available API keys
983
+ print("πŸ” Checking for available API keys...")
984
+ initialize_model_from_environment()
985
+
986
  agent = KISSAgent()
987
 
988
  # Start the preview app automatically when the main app starts