Spaces:
Running
Running
Andy Lee
commited on
Commit
Β·
6c8b7ac
1
Parent(s):
4d37e51
fix: unique streamlit key
Browse files
app.py
CHANGED
|
@@ -19,8 +19,85 @@ from langchain_openai import ChatOpenAI
|
|
| 19 |
from langchain_anthropic import ChatAnthropic
|
| 20 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# --- Helper function ---
|
| 24 |
def get_available_datasets():
|
| 25 |
"""Get list of available datasets"""
|
| 26 |
datasets_dir = Path("datasets")
|
|
@@ -38,6 +115,21 @@ def get_available_datasets():
|
|
| 38 |
return datasets if datasets else ["default"]
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# --- Page UI Setup ---
|
| 42 |
st.set_page_config(page_title="MapCrunch AI Agent", layout="wide")
|
| 43 |
st.title("πΊοΈ MapCrunch AI Agent")
|
|
@@ -45,20 +137,46 @@ st.caption(
|
|
| 45 |
"An AI agent that explores and identifies geographic locations through multi-step interaction."
|
| 46 |
)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# --- Sidebar for Configuration ---
|
| 49 |
with st.sidebar:
|
| 50 |
st.header("βοΈ Agent Configuration")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Dataset selection
|
| 58 |
available_datasets = get_available_datasets()
|
| 59 |
dataset_choice = st.selectbox("Select Dataset", available_datasets)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
steps_per_sample = st.slider(
|
| 63 |
"Max Exploration Steps per Sample", min_value=3, max_value=20, value=10
|
| 64 |
)
|
|
@@ -94,9 +212,17 @@ if start_button:
|
|
| 94 |
# Prepare the environment
|
| 95 |
test_samples = golden_labels[:num_samples_to_run]
|
| 96 |
|
| 97 |
-
config =
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Initialize helpers and result lists
|
| 102 |
benchmark_helper = MapGuesserBenchmark(dataset_name=dataset_choice)
|
|
@@ -110,8 +236,15 @@ if start_button:
|
|
| 110 |
|
| 111 |
# Initialize the bot outside the loop to reuse the browser instance for efficiency
|
| 112 |
with st.spinner("Initializing browser and AI model..."):
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# Main loop to iterate through all selected test samples
|
| 117 |
for i, sample in enumerate(test_samples):
|
|
@@ -136,94 +269,91 @@ if start_button:
|
|
| 136 |
# --- Inner agent exploration loop ---
|
| 137 |
history = []
|
| 138 |
final_guess = None
|
| 139 |
-
step_history_container = st.container()
|
| 140 |
|
| 141 |
for step in range(steps_per_sample):
|
| 142 |
step_num = step + 1
|
|
|
|
|
|
|
| 143 |
reasoning_placeholder.info(
|
| 144 |
f"π€ Thinking... (Step {step_num}/{steps_per_sample})"
|
| 145 |
)
|
| 146 |
action_placeholder.empty()
|
| 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 |
-
with st.expander("π Context Being Considered", expanded=False):
|
| 188 |
-
st.text_area(
|
| 189 |
-
"History Context:", history_text, height=100, disabled=True
|
| 190 |
-
)
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
"reasoning": "β οΈ Response parsing failed. Using default recovery action.",
|
| 204 |
-
}
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
if len(response.content) > 500
|
| 212 |
-
else response.content
|
| 213 |
-
)
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
|
|
|
| 217 |
|
| 218 |
-
|
| 219 |
-
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
col_reason, col_raw = st.columns([2, 1])
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
st.info(decision.get("reasoning", "N/A"))
|
| 228 |
|
| 229 |
if action == "GUESS":
|
|
@@ -232,71 +362,43 @@ if start_button:
|
|
| 232 |
if lat and lon:
|
| 233 |
st.success(f"π **Final Guess:** {lat:.4f}, {lon:.4f}")
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
history[-1]["raw_response"],
|
| 240 |
-
height=200,
|
| 241 |
-
disabled=True,
|
| 242 |
-
key=f"raw_response_{step_num}",
|
| 243 |
-
)
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
st.image(
|
| 251 |
-
screenshot_bytes, caption=f"Step {step_num} View", width=200
|
| 252 |
-
)
|
| 253 |
-
with hist_col2:
|
| 254 |
-
st.write(f"**Action:** {action}")
|
| 255 |
-
st.write(
|
| 256 |
-
f"**Reasoning:** {decision.get('reasoning', 'N/A')[:150]}..."
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
-
# Force a GUESS on the last step
|
| 260 |
-
if step_num == steps_per_sample and action != "GUESS":
|
| 261 |
-
st.warning("β° Max steps reached. Forcing a GUESS action.")
|
| 262 |
-
action = "GUESS"
|
| 263 |
-
# Force coordinates if missing
|
| 264 |
-
if not decision.get("action_details", {}).get("lat"):
|
| 265 |
-
st.error("β AI didn't provide coordinates. Using fallback guess.")
|
| 266 |
-
decision["action_details"] = {
|
| 267 |
-
"action": "GUESS",
|
| 268 |
-
"lat": 0.0,
|
| 269 |
-
"lon": 0.0,
|
| 270 |
-
}
|
| 271 |
-
|
| 272 |
-
# Act
|
| 273 |
-
if action == "GUESS":
|
| 274 |
-
lat, lon = (
|
| 275 |
-
decision.get("action_details", {}).get("lat"),
|
| 276 |
-
decision.get("action_details", {}).get("lon"),
|
| 277 |
-
)
|
| 278 |
-
if lat is not None and lon is not None:
|
| 279 |
-
final_guess = (lat, lon)
|
| 280 |
-
else:
|
| 281 |
-
st.error(
|
| 282 |
-
"β GUESS action was missing coordinates. Guess failed for this sample."
|
| 283 |
)
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
# --- End of single sample run, calculate and display results ---
|
| 302 |
true_coords = {"lat": sample.get("lat"), "lng": sample.get("lng")}
|
|
|
|
| 19 |
from langchain_anthropic import ChatAnthropic
|
| 20 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
|
| 22 |
+
from hf_chat import HuggingFaceChat
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def setup_api_keys():
|
| 26 |
+
"""Setup API keys from Streamlit secrets and show status"""
|
| 27 |
+
key_status = {}
|
| 28 |
+
|
| 29 |
+
# OpenAI
|
| 30 |
+
openai_key = st.secrets.get("OPENAI_API_KEY", "")
|
| 31 |
+
if openai_key:
|
| 32 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
| 33 |
+
key_status["OpenAI"] = "β
Available"
|
| 34 |
+
else:
|
| 35 |
+
key_status["OpenAI"] = "β Missing"
|
| 36 |
+
|
| 37 |
+
# Anthropic
|
| 38 |
+
anthropic_key = st.secrets.get("ANTHROPIC_API_KEY", "")
|
| 39 |
+
if anthropic_key:
|
| 40 |
+
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
| 41 |
+
key_status["Anthropic"] = "β
Available"
|
| 42 |
+
else:
|
| 43 |
+
key_status["Anthropic"] = "β Missing"
|
| 44 |
+
|
| 45 |
+
# Google
|
| 46 |
+
google_key = st.secrets.get("GOOGLE_API_KEY", "")
|
| 47 |
+
if google_key:
|
| 48 |
+
os.environ["GOOGLE_API_KEY"] = google_key
|
| 49 |
+
key_status["Google"] = "β
Available"
|
| 50 |
+
else:
|
| 51 |
+
key_status["Google"] = "β Missing"
|
| 52 |
+
|
| 53 |
+
# HuggingFace
|
| 54 |
+
hf_key = st.secrets.get("HUGGINGFACE_API_KEY", "")
|
| 55 |
+
if hf_key:
|
| 56 |
+
os.environ["HUGGINGFACE_API_KEY"] = hf_key
|
| 57 |
+
key_status["HuggingFace"] = "β
Available"
|
| 58 |
+
else:
|
| 59 |
+
key_status["HuggingFace"] = "β Missing"
|
| 60 |
+
|
| 61 |
+
return key_status
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_available_models(key_status):
|
| 65 |
+
"""Get available models based on API key status"""
|
| 66 |
+
available_models = {}
|
| 67 |
+
|
| 68 |
+
for model_id, config in MODELS_CONFIG.items():
|
| 69 |
+
api_key_env = config["api_key_env"]
|
| 70 |
+
|
| 71 |
+
# Check if required API key is available
|
| 72 |
+
if (
|
| 73 |
+
api_key_env == "OPENAI_API_KEY"
|
| 74 |
+
and "OpenAI" in key_status
|
| 75 |
+
and "β
" in key_status["OpenAI"]
|
| 76 |
+
):
|
| 77 |
+
available_models[model_id] = config
|
| 78 |
+
elif (
|
| 79 |
+
api_key_env == "ANTHROPIC_API_KEY"
|
| 80 |
+
and "Anthropic" in key_status
|
| 81 |
+
and "β
" in key_status["Anthropic"]
|
| 82 |
+
):
|
| 83 |
+
available_models[model_id] = config
|
| 84 |
+
elif (
|
| 85 |
+
api_key_env == "GOOGLE_API_KEY"
|
| 86 |
+
and "Google" in key_status
|
| 87 |
+
and "β
" in key_status["Google"]
|
| 88 |
+
):
|
| 89 |
+
available_models[model_id] = config
|
| 90 |
+
elif (
|
| 91 |
+
api_key_env == "HUGGINGFACE_API_KEY"
|
| 92 |
+
and "HuggingFace" in key_status
|
| 93 |
+
and "β
" in key_status["HuggingFace"]
|
| 94 |
+
):
|
| 95 |
+
if HuggingFaceChat is not None: # Only if wrapper is available
|
| 96 |
+
available_models[model_id] = config
|
| 97 |
+
|
| 98 |
+
return available_models
|
| 99 |
+
|
| 100 |
|
|
|
|
| 101 |
def get_available_datasets():
|
| 102 |
"""Get list of available datasets"""
|
| 103 |
datasets_dir = Path("datasets")
|
|
|
|
| 115 |
return datasets if datasets else ["default"]
|
| 116 |
|
| 117 |
|
| 118 |
+
def get_model_class(model_config):
|
| 119 |
+
"""Get the appropriate model class based on config"""
|
| 120 |
+
class_name = model_config["class"]
|
| 121 |
+
if class_name == "ChatOpenAI":
|
| 122 |
+
return ChatOpenAI
|
| 123 |
+
elif class_name == "ChatAnthropic":
|
| 124 |
+
return ChatAnthropic
|
| 125 |
+
elif class_name == "ChatGoogleGenerativeAI":
|
| 126 |
+
return ChatGoogleGenerativeAI
|
| 127 |
+
elif class_name == "HuggingFaceChat":
|
| 128 |
+
return HuggingFaceChat
|
| 129 |
+
else:
|
| 130 |
+
raise ValueError(f"Unknown model class: {class_name}")
|
| 131 |
+
|
| 132 |
+
|
| 133 |
# --- Page UI Setup ---
|
| 134 |
st.set_page_config(page_title="MapCrunch AI Agent", layout="wide")
|
| 135 |
st.title("πΊοΈ MapCrunch AI Agent")
|
|
|
|
| 137 |
"An AI agent that explores and identifies geographic locations through multi-step interaction."
|
| 138 |
)
|
| 139 |
|
| 140 |
+
# Setup API keys and check status
|
| 141 |
+
key_status = setup_api_keys()
|
| 142 |
+
available_models = get_available_models(key_status)
|
| 143 |
+
|
| 144 |
# --- Sidebar for Configuration ---
|
| 145 |
with st.sidebar:
|
| 146 |
st.header("βοΈ Agent Configuration")
|
| 147 |
|
| 148 |
+
# Show API key status
|
| 149 |
+
with st.expander("π API Key Status", expanded=False):
|
| 150 |
+
for provider, status in key_status.items():
|
| 151 |
+
st.text(f"{provider}: {status}")
|
| 152 |
+
|
| 153 |
+
if not any("β
" in status for status in key_status.values()):
|
| 154 |
+
st.error(
|
| 155 |
+
"β οΈ No API keys configured! Please set up API keys in HF Spaces secrets."
|
| 156 |
+
)
|
| 157 |
+
st.info(
|
| 158 |
+
"Add these secrets in your Space settings:\n- OPENAI_API_KEY\n- ANTHROPIC_API_KEY\n- GOOGLE_API_KEY\n- HUGGINGFACE_API_KEY"
|
| 159 |
+
)
|
| 160 |
|
| 161 |
# Dataset selection
|
| 162 |
available_datasets = get_available_datasets()
|
| 163 |
dataset_choice = st.selectbox("Select Dataset", available_datasets)
|
| 164 |
|
| 165 |
+
# Model selection (only show available models)
|
| 166 |
+
if not available_models:
|
| 167 |
+
st.error("β No models available! Please configure API keys.")
|
| 168 |
+
st.stop()
|
| 169 |
+
|
| 170 |
+
model_options = {
|
| 171 |
+
model_id: f"{model_id} - {config['description']}"
|
| 172 |
+
for model_id, config in available_models.items()
|
| 173 |
+
}
|
| 174 |
+
model_choice = st.selectbox(
|
| 175 |
+
"Select AI Model",
|
| 176 |
+
list(model_options.keys()),
|
| 177 |
+
format_func=lambda x: model_options[x],
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
steps_per_sample = st.slider(
|
| 181 |
"Max Exploration Steps per Sample", min_value=3, max_value=20, value=10
|
| 182 |
)
|
|
|
|
| 212 |
# Prepare the environment
|
| 213 |
test_samples = golden_labels[:num_samples_to_run]
|
| 214 |
|
| 215 |
+
config = available_models.get(model_choice)
|
| 216 |
+
if not config:
|
| 217 |
+
st.error(f"Model {model_choice} is not available!")
|
| 218 |
+
st.stop()
|
| 219 |
+
|
| 220 |
+
try:
|
| 221 |
+
model_class = get_model_class(config)
|
| 222 |
+
model_instance_name = config["model_name"]
|
| 223 |
+
except Exception as e:
|
| 224 |
+
st.error(f"Failed to load model class: {e}")
|
| 225 |
+
st.stop()
|
| 226 |
|
| 227 |
# Initialize helpers and result lists
|
| 228 |
benchmark_helper = MapGuesserBenchmark(dataset_name=dataset_choice)
|
|
|
|
| 236 |
|
| 237 |
# Initialize the bot outside the loop to reuse the browser instance for efficiency
|
| 238 |
with st.spinner("Initializing browser and AI model..."):
|
| 239 |
+
try:
|
| 240 |
+
# Note: Must run in headless mode on HF Spaces
|
| 241 |
+
bot = GeoBot(
|
| 242 |
+
model=model_class, model_name=model_instance_name, headless=True
|
| 243 |
+
)
|
| 244 |
+
except Exception as e:
|
| 245 |
+
st.error(f"Failed to initialize model: {e}")
|
| 246 |
+
st.info("This might be due to API key issues or model unavailability.")
|
| 247 |
+
st.stop()
|
| 248 |
|
| 249 |
# Main loop to iterate through all selected test samples
|
| 250 |
for i, sample in enumerate(test_samples):
|
|
|
|
| 269 |
# --- Inner agent exploration loop ---
|
| 270 |
history = []
|
| 271 |
final_guess = None
|
|
|
|
| 272 |
|
| 273 |
for step in range(steps_per_sample):
|
| 274 |
step_num = step + 1
|
| 275 |
+
unique_step_id = f"sample_{i}_step_{step_num}" # Unique identifier
|
| 276 |
+
|
| 277 |
reasoning_placeholder.info(
|
| 278 |
f"π€ Thinking... (Step {step_num}/{steps_per_sample})"
|
| 279 |
)
|
| 280 |
action_placeholder.empty()
|
| 281 |
|
| 282 |
+
try:
|
| 283 |
+
# Observe and label arrows
|
| 284 |
+
bot.controller.label_arrows_on_screen()
|
| 285 |
+
screenshot_bytes = bot.controller.take_street_view_screenshot()
|
| 286 |
|
| 287 |
+
# Current view
|
| 288 |
+
image_placeholder.image(
|
| 289 |
+
screenshot_bytes,
|
| 290 |
+
caption=f"π Step {step_num} - What AI Sees Now",
|
| 291 |
+
use_column_width=True,
|
| 292 |
+
)
|
| 293 |
|
| 294 |
+
# Update history
|
| 295 |
+
current_step_data = {
|
| 296 |
+
"image_b64": bot.pil_to_base64(
|
| 297 |
+
Image.open(BytesIO(screenshot_bytes))
|
| 298 |
+
),
|
| 299 |
+
"action": "N/A",
|
| 300 |
+
"screenshot_bytes": screenshot_bytes,
|
| 301 |
+
"step_num": step_num,
|
| 302 |
+
}
|
| 303 |
+
history.append(current_step_data)
|
| 304 |
|
| 305 |
+
# Think
|
| 306 |
+
available_actions = bot.controller.get_available_actions()
|
| 307 |
+
history_text = "\n".join(
|
| 308 |
+
[f"Step {j + 1}: {h['action']}" for j, h in enumerate(history[:-1])]
|
| 309 |
+
)
|
| 310 |
+
if not history_text:
|
| 311 |
+
history_text = "No history yet. This is the first step."
|
| 312 |
|
| 313 |
+
prompt = AGENT_PROMPT_TEMPLATE.format(
|
| 314 |
+
remaining_steps=steps_per_sample - step,
|
| 315 |
+
history_text=history_text,
|
| 316 |
+
available_actions=json.dumps(available_actions),
|
| 317 |
+
)
|
| 318 |
|
| 319 |
+
# Show what AI is considering
|
| 320 |
+
with reasoning_placeholder:
|
| 321 |
+
st.info("π§ **AI is analyzing the situation...**")
|
| 322 |
+
with st.expander("π Available Actions", expanded=False):
|
| 323 |
+
st.json(available_actions)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
+
# Only show context if there's meaningful history
|
| 326 |
+
if len(history) > 1:
|
| 327 |
+
with st.expander("π Previous Steps", expanded=False):
|
| 328 |
+
for j, h in enumerate(history[:-1]):
|
| 329 |
+
st.write(f"Step {j + 1}: {h.get('action', 'N/A')}")
|
| 330 |
|
| 331 |
+
message = bot._create_message_with_history(
|
| 332 |
+
prompt, [h["image_b64"] for h in history]
|
| 333 |
+
)
|
| 334 |
|
| 335 |
+
# Get AI response
|
| 336 |
+
response = bot.model.invoke(message)
|
| 337 |
+
decision = bot._parse_agent_response(response)
|
|
|
|
|
|
|
| 338 |
|
| 339 |
+
if not decision: # Fallback
|
| 340 |
+
decision = {
|
| 341 |
+
"action_details": {"action": "PAN_RIGHT"},
|
| 342 |
+
"reasoning": "β οΈ Response parsing failed. Using default recovery action.",
|
| 343 |
+
}
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
+
action = decision.get("action_details", {}).get("action")
|
| 346 |
+
history[-1]["action"] = action
|
| 347 |
+
history[-1]["reasoning"] = decision.get("reasoning", "N/A")
|
| 348 |
|
| 349 |
+
# Display AI's decision
|
| 350 |
+
reasoning_placeholder.success("β
**AI Decision Made!**")
|
| 351 |
|
| 352 |
+
with action_placeholder:
|
| 353 |
+
st.success(f"π― **AI Action:** `{action}`")
|
|
|
|
| 354 |
|
| 355 |
+
# Show reasoning in expandable section
|
| 356 |
+
with st.expander("π§ AI's Reasoning", expanded=True):
|
| 357 |
st.info(decision.get("reasoning", "N/A"))
|
| 358 |
|
| 359 |
if action == "GUESS":
|
|
|
|
| 362 |
if lat and lon:
|
| 363 |
st.success(f"π **Final Guess:** {lat:.4f}, {lon:.4f}")
|
| 364 |
|
| 365 |
+
# Force a GUESS on the last step
|
| 366 |
+
if step_num == steps_per_sample and action != "GUESS":
|
| 367 |
+
st.warning("β° Max steps reached. Forcing a GUESS action.")
|
| 368 |
+
action = "GUESS"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
# Act
|
| 371 |
+
if action == "GUESS":
|
| 372 |
+
lat, lon = (
|
| 373 |
+
decision.get("action_details", {}).get("lat"),
|
| 374 |
+
decision.get("action_details", {}).get("lon"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
)
|
| 376 |
+
if lat is not None and lon is not None:
|
| 377 |
+
final_guess = (lat, lon)
|
| 378 |
+
else:
|
| 379 |
+
st.error(
|
| 380 |
+
"β GUESS action was missing coordinates. Guess failed for this sample."
|
| 381 |
+
)
|
| 382 |
+
break # End exploration for the current sample
|
| 383 |
+
|
| 384 |
+
elif action == "MOVE_FORWARD":
|
| 385 |
+
with st.spinner("πΆ Moving forward..."):
|
| 386 |
+
bot.controller.move("forward")
|
| 387 |
+
elif action == "MOVE_BACKWARD":
|
| 388 |
+
with st.spinner("π Moving backward..."):
|
| 389 |
+
bot.controller.move("backward")
|
| 390 |
+
elif action == "PAN_LEFT":
|
| 391 |
+
with st.spinner("β¬
οΈ Panning left..."):
|
| 392 |
+
bot.controller.pan_view("left")
|
| 393 |
+
elif action == "PAN_RIGHT":
|
| 394 |
+
with st.spinner("β‘οΈ Panning right..."):
|
| 395 |
+
bot.controller.pan_view("right")
|
| 396 |
+
|
| 397 |
+
time.sleep(1) # A brief pause between steps
|
| 398 |
+
|
| 399 |
+
except Exception as e:
|
| 400 |
+
st.error(f"Error in step {step_num}: {e}")
|
| 401 |
+
break
|
| 402 |
|
| 403 |
# --- End of single sample run, calculate and display results ---
|
| 404 |
true_coords = {"lat": sample.get("lat"), "lng": sample.get("lng")}
|