import streamlit as st import json from datetime import datetime import uuid import os import time # Import your agent function (assuming it's in the same directory or properly imported) # from your_agent_file import call_agent # Mock function for demonstration - replace with your actual call_agent function def call_agent(query): """ Mock function - replace this with your actual call_agent function """ # This is a mock response for demonstration mock_response = [ {"id": 1, "task": "Fetch DEM data for the specified region", "tool": "DEMFetcher"}, {"id": 2, "task": "Extract drainage networks from available data", "tool": "DrainageExtractor"}, {"id": 3, "task": "Analyze hydrological flow patterns", "tool": "HydrologyAnalyzer"}, {"id": 4, "task": "Generate flood risk assessment maps", "tool": "LLM Reasoning"} ] # Mock state object class MockState: def __init__(self): self.query = query self.response = mock_response self.output_files_path = [f"outputs/geospatial_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:6]}.json"] return MockState() # Sample analysis data - Replace with your actual image URLs and captions SAMPLE_ANALYSES = [ { "image_url": "output.png", "caption": "Flood vulnerability analysis of Chennai during monsoon season", "query": "Create a flood risk assessment map for Chennai during monsoon season" }, { "image_url": "https://via.placeholder.com/800x600/2196F3/FFFFFF?text=Urban+Heat+Island+Mumbai", "caption": "Urban heat island analysis of Mumbai metropolitan area", "query": "Analyze urban heat island effect in Mumbai using satellite imagery" }, { "image_url": "https://via.placeholder.com/800x600/FF9800/FFFFFF?text=Deforestation+Amazon", "caption": "Deforestation monitoring in Amazon rainforest", "query": "Monitor deforestation patterns in Amazon rainforest using multi-temporal satellite data" }, { "image_url": "https://via.placeholder.com/800x600/9C27B0/FFFFFF?text=Crop+Yield+Punjab", "caption": "Crop yield prediction analysis for Punjab agricultural regions", "query": "Predict crop yield for wheat cultivation in Punjab using satellite data" }, { "image_url": "https://via.placeholder.com/800x600/F44336/FFFFFF?text=Wildfire+California", "caption": "Wildfire risk assessment and spread modeling", "query": "Create wildfire risk maps and predict fire spread patterns for California" } ] # Page configuration st.set_page_config( page_title="Geospatial AI Task Planner", page_icon="🌍", layout="wide", initial_sidebar_state="collapsed" ) # Custom CSS for modern design st.markdown(""" """, unsafe_allow_html=True) # Initialize session state if 'messages' not in st.session_state: st.session_state.messages = [] if 'generated_json' not in st.session_state: st.session_state.generated_json = None if 'processing' not in st.session_state: st.session_state.processing = False if 'current_slide' not in st.session_state: st.session_state.current_slide = 0 if 'last_slide_change' not in st.session_state: st.session_state.last_slide_change = time.time() # Header with logos and title st.markdown("""

Bharatvarsha Hackathon 2025-26

Geospatial AI Task Planner

5: # 5 seconds st.session_state.current_slide = (st.session_state.current_slide + 1) % len(SAMPLE_ANALYSES) st.session_state.last_slide_change = current_time # Create two columns using st.columns col1, col2 = st.columns([1, 1], gap="medium") # Left column - Sample Analysis Slideshow with col1: st.markdown("""

Sample Analysis

""", unsafe_allow_html=True) # Display current query current_analysis = SAMPLE_ANALYSES[st.session_state.current_slide] st.markdown(f"""
Query:

{current_analysis['query']}

""", unsafe_allow_html=True) # Slideshow container st.markdown("""
""", unsafe_allow_html=True) # Generate slides for i, analysis in enumerate(SAMPLE_ANALYSES): active_class = "active" if i == st.session_state.current_slide else "" st.markdown(f"""
Sample Analysis
{analysis['caption']}
""", unsafe_allow_html=True) # Navigation buttons col_prev, col_next = st.columns([1, 1]) with col_prev: if st.button("◀", key="prev_slide"): st.session_state.current_slide = (st.session_state.current_slide - 1) % len(SAMPLE_ANALYSES) st.session_state.last_slide_change = time.time() st.rerun() with col_next: if st.button("▶", key="next_slide"): st.session_state.current_slide = (st.session_state.current_slide + 1) % len(SAMPLE_ANALYSES) st.session_state.last_slide_change = time.time() st.rerun() # Slide indicators st.markdown("""
""", unsafe_allow_html=True) indicator_cols = st.columns(len(SAMPLE_ANALYSES)) for i, col in enumerate(indicator_cols): with col: active_class = "active" if i == st.session_state.current_slide else "" if st.button("●", key=f"indicator_{i}"): st.session_state.current_slide = i st.session_state.last_slide_change = time.time() st.rerun() st.markdown("""
""", unsafe_allow_html=True) # Right column - Chat section with col2: # Chat header st.markdown("""
💬 AI Task Planner Chat
""", unsafe_allow_html=True) # Chat messages container st.markdown('
', unsafe_allow_html=True) # Display chat messages for message in st.session_state.messages: if message["role"] == "user": st.markdown(f"""
You: {message["content"]}
""", unsafe_allow_html=True) else: # AI message if message["content"] == "task_plan": # Render task plan using native Streamlit components st.markdown("""
🤖 AI Task Planner:
""", unsafe_allow_html=True) # Use Streamlit's native components for better rendering st.markdown("""
""", unsafe_allow_html=True) for task in message["tasks"]: st.markdown(f"""
Task {task['id']}: {task['task']}
{task['tool']}
""", unsafe_allow_html=True) st.markdown("""
📄 Task Plan JSON Generated Successfully!

View the complete JSON structure below and download it for your records.
""", unsafe_allow_html=True) else: # This is plain text content st.markdown(f"""
🤖 AI Task Planner:

{message["content"]}
""", unsafe_allow_html=True) # If no messages, show welcome message if not st.session_state.messages: st.markdown("""
🤖 Welcome to Geospatial AI Task Planner!

I'm here to help you break down complex geospatial analysis tasks into manageable steps.

Simply describe your geospatial analysis needs in the text area below, and I'll create a detailed task plan for you.
""", unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) # Input section st.markdown('
', unsafe_allow_html=True) # Text input for query user_input = st.text_area( "Enter your geospatial analysis query:", height=120, placeholder="e.g., Create a flood risk assessment map for Mumbai during monsoon season...", key="user_input", disabled=st.session_state.processing ) # Submit button if st.button("🚀 Generate Task Plan", type="primary", use_container_width=True, disabled=st.session_state.processing): if user_input.strip(): # Set processing state st.session_state.processing = True # Add user message st.session_state.messages.append({"role": "user", "content": user_input}) # Show processing message with spinner with st.spinner("🔄 Generating your geospatial task plan..."): try: # Call your agent result = call_agent(user_input) # Create JSON structure task_plan_json = { "query": user_input, "timestamp": datetime.now().isoformat(), "task_id": str(uuid.uuid4()), "tasks": result.response, "output_files": result.output_files_path, "status": "completed" } # Store the tasks in session state for rendering st.session_state.generated_json = task_plan_json # Instead of storing HTML, store the task data st.session_state.messages.append({ "role": "assistant", "content": "task_plan", "tasks": result.response }) # Show success message st.success("✅ Task plan generated successfully!") except Exception as e: error_msg = f"❌ Error processing request: {str(e)}" st.session_state.messages.append({"role": "assistant", "content": error_msg}) st.error(error_msg) st.session_state.generated_json = None # Reset processing state st.session_state.processing = False # Rerun to update the interface st.rerun() else: st.warning("Please enter a query before submitting.") # Display JSON and download button if JSON is generated if st.session_state.generated_json: st.markdown("### 📋 Generated Task Plan JSON") # Display JSON in a formatted container json_str = json.dumps(st.session_state.generated_json, indent=2) st.markdown(f'
{json_str}
', unsafe_allow_html=True) # Download button filename = f"geospatial_task_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json" st.download_button( label="📥 Download Task Plan JSON", data=json_str, file_name=filename, mime="application/json", use_container_width=True ) st.markdown('
', unsafe_allow_html=True) # Clear chat button (at the bottom) if st.session_state.messages: col_clear1, col_clear2 = st.columns([1, 1]) with col_clear1: if st.button("🗑️ Clear Chat", key="clear_chat", use_container_width=True): st.session_state.messages = [] st.session_state.generated_json = None st.session_state.processing = False st.rerun() with col_clear2: if st.session_state.generated_json and st.button("🔄 Generate New Plan", key="new_plan", use_container_width=True): st.session_state.generated_json = None st.rerun()