Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,4 +179,267 @@ def render_workflow_tab(session_state):
|
|
| 179 |
executor_active = current_agent == "executor_agent"
|
| 180 |
|
| 181 |
# Show the workflow visualization
|
| 182 |
-
with col1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
executor_active = current_agent == "executor_agent"
|
| 180 |
|
| 181 |
# Show the workflow visualization
|
| 182 |
+
with col1:
|
| 183 |
+
if understanding_active:
|
| 184 |
+
st.markdown("### 🔍 **Understanding**")
|
| 185 |
+
else:
|
| 186 |
+
st.markdown("### 🔍 Understanding")
|
| 187 |
+
st.markdown("Extracts user intent and asks clarification questions")
|
| 188 |
+
|
| 189 |
+
if "user_intent" in session_state.conversation and session_state.conversation["user_intent"]:
|
| 190 |
+
st.success("Completed")
|
| 191 |
+
elif understanding_active:
|
| 192 |
+
st.info("In Progress")
|
| 193 |
+
else:
|
| 194 |
+
st.warning("Not Started")
|
| 195 |
+
|
| 196 |
+
with col2:
|
| 197 |
+
if planning_active:
|
| 198 |
+
st.markdown("### 📋 **Planning**")
|
| 199 |
+
else:
|
| 200 |
+
st.markdown("### 📋 Planning")
|
| 201 |
+
st.markdown("Creates data pipeline plan with sources and transformations")
|
| 202 |
+
|
| 203 |
+
if "pipeline_plan" in session_state.conversation and session_state.conversation["pipeline_plan"]:
|
| 204 |
+
st.success("Completed")
|
| 205 |
+
elif planning_active:
|
| 206 |
+
st.info("In Progress")
|
| 207 |
+
elif understanding_active:
|
| 208 |
+
st.warning("Not Started")
|
| 209 |
+
else:
|
| 210 |
+
st.success("Completed")
|
| 211 |
+
|
| 212 |
+
with col3:
|
| 213 |
+
if sql_active:
|
| 214 |
+
st.markdown("### 💻 **SQL Generation**")
|
| 215 |
+
else:
|
| 216 |
+
st.markdown("### 💻 SQL Generation")
|
| 217 |
+
st.markdown("Converts plan into executable SQL queries")
|
| 218 |
+
|
| 219 |
+
if "sql_queries" in session_state.conversation and session_state.conversation["sql_queries"]:
|
| 220 |
+
st.success("Completed")
|
| 221 |
+
elif sql_active:
|
| 222 |
+
st.info("In Progress")
|
| 223 |
+
elif understanding_active or planning_active:
|
| 224 |
+
st.warning("Not Started")
|
| 225 |
+
else:
|
| 226 |
+
st.success("Completed")
|
| 227 |
+
|
| 228 |
+
with col4:
|
| 229 |
+
if executor_active:
|
| 230 |
+
st.markdown("### ⚙️ **Execution**")
|
| 231 |
+
else:
|
| 232 |
+
st.markdown("### ⚙️ Execution")
|
| 233 |
+
st.markdown("Executes queries and reports results")
|
| 234 |
+
|
| 235 |
+
if "execution_results" in session_state.conversation and session_state.conversation["execution_results"]:
|
| 236 |
+
st.success("Completed")
|
| 237 |
+
elif executor_active:
|
| 238 |
+
st.info("In Progress")
|
| 239 |
+
elif understanding_active or planning_active or sql_active:
|
| 240 |
+
st.warning("Not Started")
|
| 241 |
+
else:
|
| 242 |
+
st.success("Completed")
|
| 243 |
+
|
| 244 |
+
# Overall confidence score
|
| 245 |
+
if "confidence_scores" in session_state.conversation and "overall" in session_state.conversation["confidence_scores"]:
|
| 246 |
+
st.markdown("### Overall Pipeline Confidence")
|
| 247 |
+
score = session_state.conversation["confidence_scores"]["overall"] * 100
|
| 248 |
+
st.progress(score / 100, text=f"{score:.1f}%")
|
| 249 |
+
|
| 250 |
+
# Workflow decision points
|
| 251 |
+
if status == "complete":
|
| 252 |
+
if score > 80:
|
| 253 |
+
st.success("✅ High confidence - Pipeline can be deployed automatically")
|
| 254 |
+
else:
|
| 255 |
+
st.warning("⚠️ Medium confidence - Human review recommended before deployment")
|
| 256 |
+
|
| 257 |
+
# Add human review section for pending approval status
|
| 258 |
+
if status == "pending_approval":
|
| 259 |
+
st.markdown("### 👤 Human Review Required")
|
| 260 |
+
st.info("This pipeline requires human review before deployment")
|
| 261 |
+
|
| 262 |
+
col1, col2 = st.columns(2)
|
| 263 |
+
with col1:
|
| 264 |
+
if st.button("✅ Approve Pipeline"):
|
| 265 |
+
# Update state to approved
|
| 266 |
+
session_state.conversation["status"] = "approved"
|
| 267 |
+
# Trigger execution to continue
|
| 268 |
+
st.rerun()
|
| 269 |
+
with col2:
|
| 270 |
+
if st.button("❌ Reject Pipeline"):
|
| 271 |
+
# Update state to rejected
|
| 272 |
+
session_state.conversation["status"] = "rejected"
|
| 273 |
+
st.error("Pipeline rejected. Please provide feedback to refine the pipeline.")
|
| 274 |
+
""")
|
| 275 |
+
|
| 276 |
+
# DB Explorer UI
|
| 277 |
+
db_explorer_py = "ui/db_explorer.py"
|
| 278 |
+
if not os.path.exists(db_explorer_py):
|
| 279 |
+
with open(db_explorer_py, "w") as f:
|
| 280 |
+
f.write("""
|
| 281 |
+
import streamlit as st
|
| 282 |
+
import pandas as pd
|
| 283 |
+
|
| 284 |
+
def render_db_explorer_tab(session_state):
|
| 285 |
+
\"\"\"Render the database explorer tab in the UI.\"\"\"
|
| 286 |
+
st.subheader("Database Explorer")
|
| 287 |
+
|
| 288 |
+
# Get tables by category
|
| 289 |
+
tables = session_state.db.get_tables()
|
| 290 |
+
|
| 291 |
+
# Display tables by category
|
| 292 |
+
col1, col2 = st.columns(2)
|
| 293 |
+
|
| 294 |
+
with col1:
|
| 295 |
+
st.markdown("### Raw Data Tables")
|
| 296 |
+
for table in tables["raw_tables"]:
|
| 297 |
+
with st.expander(table):
|
| 298 |
+
sample = session_state.db.get_table_sample(table, 3)
|
| 299 |
+
st.dataframe(pd.DataFrame(sample))
|
| 300 |
+
|
| 301 |
+
st.markdown("### Staging Tables")
|
| 302 |
+
for table in tables["staging_tables"]:
|
| 303 |
+
with st.expander(table):
|
| 304 |
+
sample = session_state.db.get_table_sample(table, 3)
|
| 305 |
+
st.dataframe(pd.DataFrame(sample))
|
| 306 |
+
|
| 307 |
+
with col2:
|
| 308 |
+
st.markdown("### Analytics Ready Data")
|
| 309 |
+
for table in tables["ard_tables"]:
|
| 310 |
+
with st.expander(table):
|
| 311 |
+
sample = session_state.db.get_table_sample(table, 3)
|
| 312 |
+
st.dataframe(pd.DataFrame(sample))
|
| 313 |
+
|
| 314 |
+
st.markdown("### Data Products")
|
| 315 |
+
for table in tables["data_products"]:
|
| 316 |
+
with st.expander(table):
|
| 317 |
+
sample = session_state.db.get_table_sample(table, 3)
|
| 318 |
+
st.dataframe(pd.DataFrame(sample))
|
| 319 |
+
|
| 320 |
+
# SQL Query Executor
|
| 321 |
+
st.markdown("### Query Explorer")
|
| 322 |
+
with st.form(key="sql_form"):
|
| 323 |
+
sql_query = st.text_area("Enter SQL Query", height=100,
|
| 324 |
+
placeholder="SELECT * FROM ARD_SALES_PERFORMANCE WHERE region = 'North' LIMIT 5")
|
| 325 |
+
run_sql = st.form_submit_button("Run Query")
|
| 326 |
+
|
| 327 |
+
if run_sql and sql_query:
|
| 328 |
+
with st.spinner("Executing query..."):
|
| 329 |
+
result = session_state.db.execute_query(sql_query)
|
| 330 |
+
|
| 331 |
+
if "error" in result:
|
| 332 |
+
st.error(f"Error executing query: {result['error']}")
|
| 333 |
+
elif "data" in result:
|
| 334 |
+
st.dataframe(pd.DataFrame(result["data"]))
|
| 335 |
+
st.success(f"Query returned {len(result['data'])} rows")
|
| 336 |
+
elif "tables" in result:
|
| 337 |
+
st.write(result["tables"])
|
| 338 |
+
elif "schema" in result:
|
| 339 |
+
st.write(f"Schema for {result['table']}:")
|
| 340 |
+
st.dataframe(pd.DataFrame(result["schema"]))
|
| 341 |
+
""")
|
| 342 |
+
|
| 343 |
+
# Now import UI modules after ensuring they exist
|
| 344 |
+
ensure_ui_files_exist()
|
| 345 |
+
|
| 346 |
+
# Import UI modules
|
| 347 |
+
from ui.conversation import render_conversation_tab
|
| 348 |
+
from ui.pipeline import render_pipeline_tab
|
| 349 |
+
from ui.agent_workflow import render_workflow_tab
|
| 350 |
+
from ui.db_explorer import render_db_explorer_tab
|
| 351 |
+
|
| 352 |
+
# Import agent state
|
| 353 |
+
from agents.state import AgentState
|
| 354 |
+
|
| 355 |
+
# Load environment variables from .env file if it exists
|
| 356 |
+
load_dotenv()
|
| 357 |
+
|
| 358 |
+
def main():
|
| 359 |
+
"""Main application entry point."""
|
| 360 |
+
# Set page configuration
|
| 361 |
+
st.set_page_config(page_title="Pharmaceutical Data Agent", page_icon="💊", layout="wide")
|
| 362 |
+
|
| 363 |
+
# Sidebar for configuration
|
| 364 |
+
st.sidebar.title("Configuration")
|
| 365 |
+
|
| 366 |
+
# API key input (in a real app, use more secure methods)
|
| 367 |
+
api_key = st.sidebar.text_input(
|
| 368 |
+
"Anthropic API Key",
|
| 369 |
+
value=os.getenv("ANTHROPIC_API_KEY", ""),
|
| 370 |
+
type="password"
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
if not api_key:
|
| 374 |
+
st.warning("Please enter your Anthropic API Key in the sidebar to continue.")
|
| 375 |
+
return
|
| 376 |
+
|
| 377 |
+
# Set the API key as an environment variable
|
| 378 |
+
os.environ["ANTHROPIC_API_KEY"] = api_key
|
| 379 |
+
|
| 380 |
+
# Initialize Anthropic client using default settings
|
| 381 |
+
anthropic_client = Anthropic() # Uses API key from environment variable
|
| 382 |
+
|
| 383 |
+
# Initialize database with synthetic data
|
| 384 |
+
if "db" not in st.session_state:
|
| 385 |
+
st.session_state.db = SyntheticDatabase()
|
| 386 |
+
|
| 387 |
+
# Initialize agent graph
|
| 388 |
+
if "agent_graph" not in st.session_state or "update_state_dict" not in st.session_state:
|
| 389 |
+
st.session_state.agent_graph, st.session_state.update_state_dict = create_agent_graph(
|
| 390 |
+
anthropic_client,
|
| 391 |
+
st.session_state.db
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
# Initialize conversation state
|
| 395 |
+
if "conversation" not in st.session_state:
|
| 396 |
+
st.session_state.conversation = {
|
| 397 |
+
"messages": [],
|
| 398 |
+
"user_intent": {},
|
| 399 |
+
"data_context": {},
|
| 400 |
+
"pipeline_plan": {},
|
| 401 |
+
"sql_queries": [],
|
| 402 |
+
"execution_results": {},
|
| 403 |
+
"confidence_scores": {},
|
| 404 |
+
"status": "planning",
|
| 405 |
+
"current_agent": "understanding_agent"
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
# Initialize agent state for new runs
|
| 409 |
+
if "agent_state" not in st.session_state:
|
| 410 |
+
st.session_state.agent_state = AgentState(
|
| 411 |
+
messages=[],
|
| 412 |
+
user_intent={},
|
| 413 |
+
data_context={},
|
| 414 |
+
pipeline_plan={},
|
| 415 |
+
sql_queries=[],
|
| 416 |
+
execution_results={},
|
| 417 |
+
confidence_scores={},
|
| 418 |
+
status="planning",
|
| 419 |
+
current_agent="understanding_agent"
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# Main content area
|
| 423 |
+
st.title("Pharmaceutical Data Management Agent")
|
| 424 |
+
|
| 425 |
+
# Tabs for different views
|
| 426 |
+
tab1, tab2, tab3, tab4 = st.tabs(["Conversation", "Pipeline Details", "Agent Workflow", "Database Explorer"])
|
| 427 |
+
|
| 428 |
+
with tab1:
|
| 429 |
+
render_conversation_tab(
|
| 430 |
+
st.session_state,
|
| 431 |
+
st.session_state.agent_graph,
|
| 432 |
+
st.session_state.update_state_dict
|
| 433 |
+
)
|
| 434 |
+
|
| 435 |
+
with tab2:
|
| 436 |
+
render_pipeline_tab(st.session_state)
|
| 437 |
+
|
| 438 |
+
with tab3:
|
| 439 |
+
render_workflow_tab(st.session_state)
|
| 440 |
+
|
| 441 |
+
with tab4:
|
| 442 |
+
render_db_explorer_tab(st.session_state)
|
| 443 |
+
|
| 444 |
+
if __name__ == "__main__":
|
| 445 |
+
main()
|