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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import time
|
| 8 |
+
from agent import ProcurementRLAgent
|
| 9 |
+
from tools import (
|
| 10 |
+
InventoryTool, ExternalRiskTool, ContractTool,
|
| 11 |
+
POExecutionTool, HumanNotificationTool
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Page configuration
|
| 15 |
+
st.set_page_config(
|
| 16 |
+
page_title="Agentic AI Procurement Demo",
|
| 17 |
+
page_icon="π€",
|
| 18 |
+
layout="wide",
|
| 19 |
+
initial_sidebar_state="expanded"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Custom CSS for better styling
|
| 23 |
+
st.markdown("""
|
| 24 |
+
<style>
|
| 25 |
+
.main-header {
|
| 26 |
+
font-size: 2.5rem;
|
| 27 |
+
color: #1f77b4;
|
| 28 |
+
text-align: center;
|
| 29 |
+
margin-bottom: 2rem;
|
| 30 |
+
}
|
| 31 |
+
.decision-box {
|
| 32 |
+
background-color: #f0f8ff;
|
| 33 |
+
border-left: 5px solid #1f77b4;
|
| 34 |
+
padding: 1rem;
|
| 35 |
+
margin: 1rem 0;
|
| 36 |
+
}
|
| 37 |
+
.agent-thinking {
|
| 38 |
+
background-color: #fff3cd;
|
| 39 |
+
border: 1px solid #ffeaa7;
|
| 40 |
+
padding: 1rem;
|
| 41 |
+
border-radius: 5px;
|
| 42 |
+
}
|
| 43 |
+
.success-box {
|
| 44 |
+
background-color: #d4edda;
|
| 45 |
+
border-left: 5px solid #28a745;
|
| 46 |
+
padding: 1rem;
|
| 47 |
+
margin: 1rem 0;
|
| 48 |
+
}
|
| 49 |
+
.warning-box {
|
| 50 |
+
background-color: #f8d7da;
|
| 51 |
+
border-left: 5px solid #dc3545;
|
| 52 |
+
padding: 1rem;
|
| 53 |
+
margin: 1rem 0;
|
| 54 |
+
}
|
| 55 |
+
</style>
|
| 56 |
+
""", unsafe_allow_html=True)
|
| 57 |
+
|
| 58 |
+
# Initialize tools
|
| 59 |
+
@st.cache_resource
|
| 60 |
+
def initialize_agent():
|
| 61 |
+
tools = [
|
| 62 |
+
InventoryTool(),
|
| 63 |
+
ExternalRiskTool(),
|
| 64 |
+
ContractTool(),
|
| 65 |
+
POExecutionTool(),
|
| 66 |
+
HumanNotificationTool()
|
| 67 |
+
]
|
| 68 |
+
return ProcurementRLAgent(tools=tools)
|
| 69 |
+
|
| 70 |
+
agent = initialize_agent()
|
| 71 |
+
|
| 72 |
+
# Title
|
| 73 |
+
st.markdown('<h1 class="main-header">π€ Agentic AI Procurement Assistant</h1>', unsafe_allow_html=True)
|
| 74 |
+
|
| 75 |
+
# Sidebar for demo controls
|
| 76 |
+
with st.sidebar:
|
| 77 |
+
st.header("Demo Controls")
|
| 78 |
+
|
| 79 |
+
# Sample scenarios
|
| 80 |
+
scenarios = {
|
| 81 |
+
"Standard Office Supplies": {
|
| 82 |
+
"PR_ID": "PR-12345",
|
| 83 |
+
"Item": "Ergonomic Office Chairs",
|
| 84 |
+
"Category": "Office Supplies",
|
| 85 |
+
"Quantity": 25,
|
| 86 |
+
"Urgency": "Medium",
|
| 87 |
+
"Budget": 12500.0,
|
| 88 |
+
"Current_Inventory": 5,
|
| 89 |
+
"Contract_Status": "Valid",
|
| 90 |
+
"External_Disruption": False,
|
| 91 |
+
"Supplier_History": "Good"
|
| 92 |
+
},
|
| 93 |
+
"Urgent IT Equipment": {
|
| 94 |
+
"PR_ID": "PR-67890",
|
| 95 |
+
"Item": "Enterprise Laptops",
|
| 96 |
+
"Category": "IT Equipment",
|
| 97 |
+
"Quantity": 50,
|
| 98 |
+
"Urgency": "High",
|
| 99 |
+
"Budget": 75000.0,
|
| 100 |
+
"Current_Inventory": 10,
|
| 101 |
+
"Contract_Status": "Expired",
|
| 102 |
+
"External_Disruption": False,
|
| 103 |
+
"Supplier_History": "Excellent"
|
| 104 |
+
},
|
| 105 |
+
"Disrupted Raw Materials": {
|
| 106 |
+
"PR_ID": "PR-11111",
|
| 107 |
+
"Item": "Steel Components",
|
| 108 |
+
"Category": "Raw Materials",
|
| 109 |
+
"Quantity": 100,
|
| 110 |
+
"Urgency": "High",
|
| 111 |
+
"Budget": 50000.0,
|
| 112 |
+
"Current_Inventory": 0,
|
| 113 |
+
"Contract_Status": "Valid",
|
| 114 |
+
"External_Disruption": True,
|
| 115 |
+
"Supplier_History": "Average"
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
selected_scenario = st.selectbox("Select Demo Scenario:", list(scenarios.keys()))
|
| 120 |
+
pr_data = scenarios[selected_scenario]
|
| 121 |
+
|
| 122 |
+
st.header("Model Performance")
|
| 123 |
+
|
| 124 |
+
# Load historical data for metrics
|
| 125 |
+
try:
|
| 126 |
+
with open("demo_space/historical_procurement_data.json", "r") as f:
|
| 127 |
+
historical_data = json.load(f)
|
| 128 |
+
|
| 129 |
+
df = pd.DataFrame(historical_data)
|
| 130 |
+
avg_delivery = df['delivery_performance'].mean()
|
| 131 |
+
avg_quality = df['quality_score'].mean()
|
| 132 |
+
|
| 133 |
+
st.metric("Avg Delivery Performance", f"{avg_delivery:.2%}")
|
| 134 |
+
st.metric("Avg Quality Score", f"{avg_quality:.2%}")
|
| 135 |
+
st.metric("Training Samples", len(historical_data))
|
| 136 |
+
except FileNotFoundError:
|
| 137 |
+
st.warning("Historical data not found")
|
| 138 |
+
|
| 139 |
+
# Main content area
|
| 140 |
+
col1, col2 = st.columns([1, 1])
|
| 141 |
+
|
| 142 |
+
with col1:
|
| 143 |
+
st.subheader("π Purchase Requisition Details")
|
| 144 |
+
|
| 145 |
+
# Display PR information in a nice format
|
| 146 |
+
pr_display = {
|
| 147 |
+
"PR ID": pr_data["PR_ID"],
|
| 148 |
+
"Item": pr_data["Item"],
|
| 149 |
+
"Category": pr_data["Category"],
|
| 150 |
+
"Quantity": pr_data["Quantity"],
|
| 151 |
+
"Urgency": pr_data["Urgency"],
|
| 152 |
+
"Budget": f"${pr_data['Budget']:,.2f}",
|
| 153 |
+
"Current Inventory": pr_data["Current_Inventory"],
|
| 154 |
+
"Contract Status": pr_data["Contract_Status"],
|
| 155 |
+
"External Disruption": "β οΈ Yes" if pr_data["External_Disruption"] else "β
No",
|
| 156 |
+
"Supplier History": pr_data["Supplier_History"]
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
for key, value in pr_display.items():
|
| 160 |
+
st.write(f"**{key}:** {value}")
|
| 161 |
+
|
| 162 |
+
with col2:
|
| 163 |
+
st.subheader("π― Agent Decision Engine")
|
| 164 |
+
|
| 165 |
+
if st.button("π Execute Agent Decision", type="primary"):
|
| 166 |
+
# Show thinking process
|
| 167 |
+
with st.spinner("Agent analyzing procurement request..."):
|
| 168 |
+
progress_bar = st.progress(0)
|
| 169 |
+
status_text = st.empty()
|
| 170 |
+
|
| 171 |
+
# Simulate agent thinking process
|
| 172 |
+
thinking_steps = [
|
| 173 |
+
"Analyzing purchase requisition...",
|
| 174 |
+
"Checking inventory levels...",
|
| 175 |
+
"Assessing external risks...",
|
| 176 |
+
"Validating contract status...",
|
| 177 |
+
"Computing optimal action...",
|
| 178 |
+
"Preparing recommendation..."
|
| 179 |
+
]
|
| 180 |
+
|
| 181 |
+
for i, step in enumerate(thinking_steps):
|
| 182 |
+
status_text.text(step)
|
| 183 |
+
progress_bar.progress((i + 1) / len(thinking_steps))
|
| 184 |
+
time.sleep(0.5)
|
| 185 |
+
|
| 186 |
+
# Get agent decision
|
| 187 |
+
decision_result = agent.decide(pr_data)
|
| 188 |
+
|
| 189 |
+
# Clear progress indicators
|
| 190 |
+
progress_bar.empty()
|
| 191 |
+
status_text.empty()
|
| 192 |
+
|
| 193 |
+
# Display decision
|
| 194 |
+
if decision_result["action_type"] == "AUTO_EXECUTE":
|
| 195 |
+
st.markdown(f"""
|
| 196 |
+
<div class="success-box">
|
| 197 |
+
<h4>β
Automatic Execution</h4>
|
| 198 |
+
<p><strong>Action:</strong> {decision_result['action']}</p>
|
| 199 |
+
<p><strong>Reason:</strong> {decision_result['reason']}</p>
|
| 200 |
+
<p><strong>Confidence:</strong> {decision_result['confidence']:.1%}</p>
|
| 201 |
+
</div>
|
| 202 |
+
""", unsafe_allow_html=True)
|
| 203 |
+
else:
|
| 204 |
+
st.markdown(f"""
|
| 205 |
+
<div class="warning-box">
|
| 206 |
+
<h4>β οΈ Human Review Required</h4>
|
| 207 |
+
<p><strong>Issue:</strong> {decision_result['action']}</p>
|
| 208 |
+
<p><strong>Reason:</strong> {decision_result['reason']}</p>
|
| 209 |
+
<p><strong>Recommended Action:</strong> {decision_result.get('recommendation', 'Review and approve manually')}</p>
|
| 210 |
+
</div>
|
| 211 |
+
""", unsafe_allow_html=True)
|
| 212 |
+
|
| 213 |
+
# Show tool execution logs
|
| 214 |
+
st.subheader("π Agent Analysis Log")
|
| 215 |
+
for log_entry in decision_result["logs"]:
|
| 216 |
+
st.text(f"[{log_entry['timestamp']}] {log_entry['tool']}: {log_entry['result']}")
|
| 217 |
+
|
| 218 |
+
# Performance Dashboard
|
| 219 |
+
st.subheader("π Model Performance Dashboard")
|
| 220 |
+
|
| 221 |
+
col3, col4, col5 = st.columns(3)
|
| 222 |
+
|
| 223 |
+
try:
|
| 224 |
+
with open("demo_space/historical_procurement_data.json", "r") as f:
|
| 225 |
+
historical_data = json.load(f)
|
| 226 |
+
|
| 227 |
+
df = pd.DataFrame(historical_data)
|
| 228 |
+
|
| 229 |
+
with col3:
|
| 230 |
+
# Delivery performance by urgency
|
| 231 |
+
delivery_by_urgency = df.groupby('urgency')['delivery_performance'].mean().reset_index()
|
| 232 |
+
fig1 = px.bar(delivery_by_urgency, x='urgency', y='delivery_performance',
|
| 233 |
+
title='Delivery Performance by Urgency',
|
| 234 |
+
color='delivery_performance', color_continuous_scale='RdYlGn')
|
| 235 |
+
st.plotly_chart(fig1, use_container_width=True)
|
| 236 |
+
|
| 237 |
+
with col4:
|
| 238 |
+
# Cost distribution by category
|
| 239 |
+
fig2 = px.box(df, x='category', y='cost', title='Cost Distribution by Category')
|
| 240 |
+
fig2.update_xaxis(tickangle=45)
|
| 241 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 242 |
+
|
| 243 |
+
with col5:
|
| 244 |
+
# Quality vs Delivery Performance
|
| 245 |
+
fig3 = px.scatter(df, x='delivery_performance', y='quality_score',
|
| 246 |
+
color='urgency', size='cost',
|
| 247 |
+
title='Quality vs Delivery Performance',
|
| 248 |
+
hover_data=['supplier'])
|
| 249 |
+
st.plotly_chart(fig3, use_container_width=True)
|
| 250 |
+
|
| 251 |
+
except FileNotFoundError:
|
| 252 |
+
st.info("Historical data not available for performance dashboard")
|
| 253 |
+
|
| 254 |
+
# Footer
|
| 255 |
+
st.markdown("---")
|
| 256 |
+
st.markdown("**Powered by:** Reinforcement Learning, smolagents, and Streamlit | **Demo Version:** 1.0")
|