PD03 commited on
Commit
99c9b10
Β·
verified Β·
1 Parent(s): de28582

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +96 -156
src/streamlit_app.py CHANGED
@@ -1,164 +1,104 @@
1
- # app.py (Updated sections)
2
  import streamlit as st
3
  import pandas as pd
 
4
  import plotly.express as px
5
  import plotly.graph_objects as go
 
6
  from streamlit_option_menu import option_menu
7
- from data_generator import generate_synthetic_procurement_data
8
- from ai_agents import LLMPoweredProcurementAgent, IntelligentRiskAgent
9
  import time
 
 
 
 
 
 
10
 
11
- # Add API key configuration
12
- if 'OPENAI_API_KEY' not in st.secrets:
13
- st.error("⚠️ OpenAI API key not found in secrets. Please add your OpenAI API key to continue.")
14
- st.stop()
 
 
 
15
 
16
- # Initialize LLM-powered agents
17
- @st.cache_resource
18
- def initialize_llm_agents():
19
- analytics_agent = LLMPoweredProcurementAgent(st.session_state.po_df, st.session_state.spend_df)
20
- risk_agent = IntelligentRiskAgent(st.session_state.spend_df)
21
- return analytics_agent, risk_agent
22
-
23
- analytics_agent, risk_agent = initialize_llm_agents()
24
-
25
- # Add new navigation option for AI Chat
26
- with st.sidebar:
27
- selected = option_menu(
28
- "Navigation",
29
- ["🏠 Dashboard", "πŸ€– AI Insights", "πŸ’¬ AI Chat", "πŸ“Š Analytics", "⚠️ Risk Management", "🎯 Recommendations"],
30
- icons=['house', 'robot', 'chat', 'graph-up', 'exclamation-triangle', 'target'],
31
- menu_icon="cast",
32
- default_index=0,
33
- )
34
-
35
- # Updated Dashboard with LLM-generated insights
36
- if selected == "🏠 Dashboard":
37
- # ... existing dashboard code ...
38
-
39
- # Add LLM-generated executive summary
40
- st.markdown("### 🧠 AI Executive Summary")
41
-
42
- with st.spinner('πŸ€– AI is analyzing your procurement data...'):
43
- executive_summary = analytics_agent.generate_executive_summary()
44
-
45
- st.markdown(f"""
46
- <div class="ai-insight">
47
- <h4>πŸ“Š Intelligent Analysis</h4>
48
- <p>{executive_summary}</p>
49
- </div>
50
- """, unsafe_allow_html=True)
51
-
52
- # New AI Chat Interface
53
- elif selected == "πŸ’¬ AI Chat":
54
- st.markdown("### πŸ’¬ Chat with Your Procurement Data")
55
-
56
- st.markdown("""
57
- <div class="ai-insight">
58
- <h4>πŸ€– Intelligent Procurement Assistant</h4>
59
- <p>Ask me anything about your procurement data! I can help you understand trends, analyze vendor performance, identify opportunities, and more.</p>
60
- </div>
61
- """, unsafe_allow_html=True)
62
-
63
- # Chat interface
64
- if "messages" not in st.session_state:
65
- st.session_state.messages = [
66
- {"role": "assistant", "content": "Hello! I'm your AI procurement analyst. What would you like to know about your procurement data?"}
67
- ]
68
-
69
- # Display chat messages
70
- for message in st.session_state.messages:
71
- with st.chat_message(message["role"]):
72
- st.markdown(message["content"])
73
-
74
- # Chat input
75
- if prompt := st.chat_input("Ask about your procurement data..."):
76
- # Add user message to chat history
77
- st.session_state.messages.append({"role": "user", "content": prompt})
78
- with st.chat_message("user"):
79
- st.markdown(prompt)
80
-
81
- # Generate AI response
82
- with st.chat_message("assistant"):
83
- with st.spinner("πŸ€– Analyzing your question..."):
84
- response = analytics_agent.chat_with_data(prompt)
85
- st.markdown(response)
86
-
87
- # Add assistant response to chat history
88
- st.session_state.messages.append({"role": "assistant", "content": response})
89
-
90
- # Suggested questions
91
- st.markdown("#### πŸ’‘ Try asking:")
92
-
93
- col1, col2, col3 = st.columns(3)
94
-
95
- with col1:
96
- if st.button("πŸ“Š What are my spending trends?"):
97
- st.rerun()
98
-
99
- with col2:
100
- if st.button("πŸ† Who are my best vendors?"):
101
- st.rerun()
102
-
103
- with col3:
104
- if st.button("⚠️ What risks should I worry about?"):
105
- st.rerun()
106
-
107
- # Enhanced AI Insights with LLM
108
- elif selected == "πŸ€– AI Insights":
109
- st.markdown("### 🧠 Advanced AI Analysis")
110
-
111
- # Vendor deep-dive analysis
112
- st.markdown("#### πŸ” Vendor Performance Deep Dive")
113
-
114
- selected_vendor = st.selectbox(
115
- "Select vendor for AI analysis:",
116
- options=st.session_state.po_df['vendor'].unique()
117
- )
118
-
119
- if st.button("πŸš€ Generate AI Analysis", type="primary"):
120
- with st.spinner(f'πŸ€– AI is performing deep analysis of {selected_vendor}...'):
121
- vendor_analysis = analytics_agent.analyze_vendor_performance(selected_vendor)
122
-
123
- st.markdown(f"""
124
- <div class="ai-insight">
125
- <h4>🎯 AI Analysis: {selected_vendor}</h4>
126
- <div style="white-space: pre-line;">{vendor_analysis}</div>
127
- </div>
128
- """, unsafe_allow_html=True)
129
-
130
- # Anomaly insights with LLM
131
- st.markdown("#### 🚨 Intelligent Anomaly Analysis")
132
-
133
- # Get anomalies from your existing detection logic
134
- anomalies = [] # Your existing anomaly detection code here
135
-
136
- with st.spinner('πŸ” AI is analyzing anomalies...'):
137
- anomaly_insights = analytics_agent.generate_anomaly_insights(anomalies)
138
-
139
- st.markdown(f"""
140
- <div class="alert alert-warning">
141
- <h4>πŸ€– AI Anomaly Assessment</h4>
142
- <div style="white-space: pre-line;">{anomaly_insights}</div>
143
- </div>
144
- """, unsafe_allow_html=True)
145
-
146
- # Enhanced Risk Management with LLM
147
- elif selected == "⚠️ Risk Management":
148
- st.markdown("### πŸ›‘οΈ AI-Powered Risk Intelligence")
149
-
150
- # ... existing risk metrics ...
151
-
152
- # LLM-generated risk report
153
- st.markdown("#### πŸ“‹ Comprehensive Risk Assessment")
154
-
155
- if st.button("πŸš€ Generate AI Risk Report", type="primary"):
156
- with st.spinner('πŸ€– AI is generating comprehensive risk assessment...'):
157
- risk_report = risk_agent.generate_risk_report()
158
-
159
- st.markdown(f"""
160
- <div class="ai-insight">
161
- <h4>πŸ›‘οΈ Strategic Risk Analysis</h4>
162
- <div style="white-space: pre-line;">{risk_report}</div>
163
- </div>
164
- """, unsafe_allow_html=True)
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import numpy as np
4
  import plotly.express as px
5
  import plotly.graph_objects as go
6
+ from plotly.subplots import make_subplots
7
  from streamlit_option_menu import option_menu
 
 
8
  import time
9
+ from faker import Faker
10
+ from datetime import datetime, timedelta
11
+ import random
12
+ import openai
13
+ import json
14
+ from typing import Dict, List, Any
15
 
16
+ # Page configuration
17
+ st.set_page_config(
18
+ page_title="SAP S/4HANA Agentic AI Procurement Analytics",
19
+ page_icon="πŸ€–",
20
+ layout="wide",
21
+ initial_sidebar_state="expanded"
22
+ )
23
 
24
+ # Custom CSS (same as before)
25
+ st.markdown("""
26
+ <style>
27
+ /* Main theme colors */
28
+ :root {
29
+ --primary-color: #0066cc;
30
+ --secondary-color: #f0f8ff;
31
+ --accent-color: #ff6b35;
32
+ --success-color: #28a745;
33
+ --warning-color: #ffc107;
34
+ --danger-color: #dc3545;
35
+ }
36
+
37
+ /* Hide Streamlit branding */
38
+ #MainMenu {visibility: hidden;}
39
+ footer {visibility: hidden;}
40
+ header {visibility: hidden;}
41
+
42
+ /* Custom header styling */
43
+ .main-header {
44
+ background: linear-gradient(90deg, #0066cc, #004c99);
45
+ padding: 1rem;
46
+ border-radius: 10px;
47
+ margin-bottom: 2rem;
48
+ color: white;
49
+ text-align: center;
50
+ }
51
+
52
+ /* Metric cards styling */
53
+ .metric-card {
54
+ background: white;
55
+ padding: 1.5rem;
56
+ border-radius: 10px;
57
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
58
+ border-left: 4px solid var(--primary-color);
59
+ margin-bottom: 1rem;
60
+ }
61
+
62
+ /* AI insights styling */
63
+ .ai-insight {
64
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
65
+ color: white;
66
+ padding: 1rem;
67
+ border-radius: 10px;
68
+ margin: 1rem 0;
69
+ }
70
+
71
+ /* Alert styling */
72
+ .alert {
73
+ padding: 1rem;
74
+ border-radius: 8px;
75
+ margin: 1rem 0;
76
+ border-left: 4px solid;
77
+ }
78
+
79
+ .alert-success {
80
+ background-color: #d4edda;
81
+ border-color: var(--success-color);
82
+ color: #155724;
83
+ }
84
+
85
+ .alert-warning {
86
+ background-color: #fff3cd;
87
+ border-color: var(--warning-color);
88
+ color: #856404;
89
+ }
90
+
91
+ .alert-danger {
92
+ background-color: #f8d7da;
93
+ border-color: var(--danger-color);
94
+ color: #721c24;
95
+ }
96
+
97
+ /* Button styling */
98
+ .stButton > button {
99
+ background: linear-gradient(90deg, #0066cc, #004c99);
100
+ color: white;
101
+ border: none;
102
+ border-radius: 8px;
103
+ padding: 0.5rem 1rem;
104
+ font