Spaces:
Running
Running
added api key tab
Browse files
ui/ui.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
from datetime import datetime
|
3 |
import sys
|
4 |
import threading
|
|
|
5 |
from agents.orchestrator import ClimateRiskOrchestrator
|
6 |
from tools.mapping_utils import (
|
7 |
COUNTRIES_AND_CITIES,
|
@@ -65,6 +66,56 @@ class ClimateRiskUI:
|
|
65 |
show_business = profile_type == "Business Owner"
|
66 |
return gr.Dropdown(visible=show_business)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def analyze_with_dropdown(
|
69 |
self,
|
70 |
country,
|
@@ -567,10 +618,97 @@ class ClimateRiskUI:
|
|
567 |
elem_id="nl_rec_box",
|
568 |
)
|
569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
570 |
# CSS pour les cadres custom
|
571 |
gr.HTML("""
|
572 |
<style>
|
573 |
-
#risk_summary_box, #recommendations_box, #nl_risk_box, #nl_rec_box {
|
574 |
border: 2px solid #007aff;
|
575 |
border-radius: 13px;
|
576 |
background: #fafdff;
|
|
|
2 |
from datetime import datetime
|
3 |
import sys
|
4 |
import threading
|
5 |
+
import os
|
6 |
from agents.orchestrator import ClimateRiskOrchestrator
|
7 |
from tools.mapping_utils import (
|
8 |
COUNTRIES_AND_CITIES,
|
|
|
66 |
show_business = profile_type == "Business Owner"
|
67 |
return gr.Dropdown(visible=show_business)
|
68 |
|
69 |
+
def validate_and_update_api_key(self, api_key, nasa_key=""):
|
70 |
+
"""Validate and update API keys in environment variables."""
|
71 |
+
status_messages = []
|
72 |
+
|
73 |
+
# Validate Anthropic API key
|
74 |
+
if api_key and api_key.strip():
|
75 |
+
if api_key.startswith('sk-ant-'):
|
76 |
+
os.environ['ANTHROPIC_API_KEY'] = api_key.strip()
|
77 |
+
status_messages.append("β
Anthropic API key updated successfully!")
|
78 |
+
|
79 |
+
# Try to reinitialize the model with new key
|
80 |
+
try:
|
81 |
+
from config import model
|
82 |
+
# This would require reloading the model, but for now just update env
|
83 |
+
status_messages.append("βΉοΈ Restart the application to use the new API key.")
|
84 |
+
except Exception as e:
|
85 |
+
status_messages.append(f"β οΈ API key saved but model reload failed: {str(e)}")
|
86 |
+
else:
|
87 |
+
status_messages.append("β Invalid Anthropic API key format. Should start with 'sk-ant-'")
|
88 |
+
else:
|
89 |
+
status_messages.append("β οΈ Please enter a valid Anthropic API key")
|
90 |
+
|
91 |
+
# Validate NASA FIRMS API key (optional)
|
92 |
+
if nasa_key and nasa_key.strip():
|
93 |
+
os.environ['NASA_FIRMS_MAP_KEY'] = nasa_key.strip()
|
94 |
+
status_messages.append("β
NASA FIRMS API key updated successfully!")
|
95 |
+
|
96 |
+
return "\n".join(status_messages)
|
97 |
+
|
98 |
+
def get_current_api_status(self):
|
99 |
+
"""Get current API key status."""
|
100 |
+
anthropic_key = os.getenv('ANTHROPIC_API_KEY', '')
|
101 |
+
nasa_key = os.getenv('NASA_FIRMS_MAP_KEY', '')
|
102 |
+
|
103 |
+
status = []
|
104 |
+
|
105 |
+
if anthropic_key and anthropic_key != 'your-anthropic-api-key-here':
|
106 |
+
masked_key = anthropic_key[:8] + "..." + anthropic_key[-4:] if len(anthropic_key) > 12 else "***"
|
107 |
+
status.append(f"π **Anthropic API Key:** {masked_key} (configured)")
|
108 |
+
else:
|
109 |
+
status.append("β **Anthropic API Key:** Not configured")
|
110 |
+
|
111 |
+
if nasa_key and nasa_key != 'your-nasa-firms-api-key-here':
|
112 |
+
masked_nasa = nasa_key[:8] + "..." + nasa_key[-4:] if len(nasa_key) > 12 else "***"
|
113 |
+
status.append(f"π°οΈ **NASA FIRMS Key:** {masked_nasa} (configured)")
|
114 |
+
else:
|
115 |
+
status.append("βΉοΈ **NASA FIRMS Key:** Not configured (optional)")
|
116 |
+
|
117 |
+
return "\n".join(status)
|
118 |
+
|
119 |
def analyze_with_dropdown(
|
120 |
self,
|
121 |
country,
|
|
|
618 |
elem_id="nl_rec_box",
|
619 |
)
|
620 |
|
621 |
+
with gr.TabItem("βοΈ Settings"):
|
622 |
+
with gr.Row():
|
623 |
+
with gr.Column():
|
624 |
+
gr.Markdown("""
|
625 |
+
## π API Configuration
|
626 |
+
|
627 |
+
Configure your API keys here to enable full functionality of CAVA-AI.
|
628 |
+
""")
|
629 |
+
|
630 |
+
# Current API status
|
631 |
+
api_status_display = gr.Markdown(
|
632 |
+
self.get_current_api_status(),
|
633 |
+
label="Current API Status",
|
634 |
+
elem_id="api_status_box"
|
635 |
+
)
|
636 |
+
|
637 |
+
# API Key inputs
|
638 |
+
anthropic_api_input = gr.Textbox(
|
639 |
+
label="Anthropic API Key",
|
640 |
+
placeholder="sk-ant-...",
|
641 |
+
type="password",
|
642 |
+
info="Required for AI analysis. Get one at: https://console.anthropic.com/"
|
643 |
+
)
|
644 |
+
|
645 |
+
nasa_api_input = gr.Textbox(
|
646 |
+
label="NASA FIRMS API Key (Optional)",
|
647 |
+
placeholder="Your NASA FIRMS Map Key",
|
648 |
+
type="password",
|
649 |
+
info="Optional for enhanced wildfire data. Get one at: https://firms.modaps.eosdis.nasa.gov/api/"
|
650 |
+
)
|
651 |
+
|
652 |
+
# Update button
|
653 |
+
update_keys_btn = gr.Button(
|
654 |
+
"π Update API Keys",
|
655 |
+
variant="primary",
|
656 |
+
size="lg"
|
657 |
+
)
|
658 |
+
|
659 |
+
# Status message
|
660 |
+
update_status = gr.Markdown(
|
661 |
+
"",
|
662 |
+
label="Update Status",
|
663 |
+
elem_id="update_status_box"
|
664 |
+
)
|
665 |
+
|
666 |
+
with gr.Column():
|
667 |
+
gr.Markdown("""
|
668 |
+
## π API Key Information
|
669 |
+
|
670 |
+
### Anthropic API Key (Required)
|
671 |
+
- **Purpose**: Powers the AI agents for climate risk analysis
|
672 |
+
- **Format**: Starts with `sk-ant-`
|
673 |
+
- **Get Key**: [Anthropic Console](https://console.anthropic.com/)
|
674 |
+
- **Pricing**: Pay per token usage
|
675 |
+
|
676 |
+
### NASA FIRMS API Key (Optional)
|
677 |
+
- **Purpose**: Enhanced wildfire detection data
|
678 |
+
- **Format**: Alphanumeric string
|
679 |
+
- **Get Key**: [NASA FIRMS](https://firms.modaps.eosdis.nasa.gov/api/)
|
680 |
+
- **Cost**: Free with registration
|
681 |
+
|
682 |
+
### π Security Notes
|
683 |
+
- API keys are stored in environment variables
|
684 |
+
- Keys are masked in the interface for security
|
685 |
+
- Restart the application after updating keys
|
686 |
+
- Never share your API keys publicly
|
687 |
+
|
688 |
+
### π οΈ Troubleshooting
|
689 |
+
- Ensure API keys are valid and active
|
690 |
+
- Check your Anthropic account billing status
|
691 |
+
- Verify network connectivity for API calls
|
692 |
+
""")
|
693 |
+
|
694 |
+
# Connect the update button
|
695 |
+
update_keys_btn.click(
|
696 |
+
fn=self.validate_and_update_api_key,
|
697 |
+
inputs=[anthropic_api_input, nasa_api_input],
|
698 |
+
outputs=[update_status]
|
699 |
+
)
|
700 |
+
|
701 |
+
# Refresh status when tab is accessed
|
702 |
+
update_keys_btn.click(
|
703 |
+
fn=self.get_current_api_status,
|
704 |
+
inputs=[],
|
705 |
+
outputs=[api_status_display]
|
706 |
+
)
|
707 |
+
|
708 |
# CSS pour les cadres custom
|
709 |
gr.HTML("""
|
710 |
<style>
|
711 |
+
#risk_summary_box, #recommendations_box, #nl_risk_box, #nl_rec_box, #api_status_box, #update_status_box {
|
712 |
border: 2px solid #007aff;
|
713 |
border-radius: 13px;
|
714 |
background: #fafdff;
|