PD03 commited on
Commit
6b719d8
Β·
verified Β·
1 Parent(s): c09c828

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +19 -20
src/streamlit_app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import os
3
- from smolagents import CodeAgent, HfApiModel, tool
4
  import json
5
  from datetime import datetime
6
  import random
@@ -12,12 +12,22 @@ st.set_page_config(
12
  layout="wide"
13
  )
14
 
15
- # Initialize Hugging Face model
16
  @st.cache_resource
17
  def get_model():
18
- return HfApiModel(model_id="HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Tool 1: Query SAP Purchase Requisition
21
  @tool
22
  def query_sap_pr(pr_number: str) -> str:
23
  """
@@ -39,7 +49,6 @@ def query_sap_pr(pr_number: str) -> str:
39
  }
40
  return json.dumps(mock_data, indent=2)
41
 
42
- # Tool 2: Check Vendor Financial Health
43
  @tool
44
  def check_vendor_solvency(vendor_name: str) -> str:
45
  """
@@ -61,7 +70,6 @@ def check_vendor_solvency(vendor_name: str) -> str:
61
  }
62
  return json.dumps(mock_data, indent=2)
63
 
64
- # Tool 3: Get Vendor Performance History
65
  @tool
66
  def get_vendor_performance(vendor_name: str) -> str:
67
  """
@@ -83,7 +91,6 @@ def get_vendor_performance(vendor_name: str) -> str:
83
  }
84
  return json.dumps(mock_data, indent=2)
85
 
86
- # Tool 4: Compare Material Rates
87
  @tool
88
  def compare_material_rates(material: str) -> str:
89
  """
@@ -113,7 +120,6 @@ def compare_material_rates(material: str) -> str:
113
  }
114
  return json.dumps(mock_data, indent=2)
115
 
116
- # Tool 5: Optimize Vendor Selection
117
  @tool
118
  def optimize_vendor_selection(requirements: str) -> str:
119
  """
@@ -137,7 +143,6 @@ def optimize_vendor_selection(requirements: str) -> str:
137
  }
138
  return json.dumps(mock_data, indent=2)
139
 
140
- # Tool 6: Create SAP Purchase Order
141
  @tool
142
  def create_purchase_order(vendor: str, amount: str) -> str:
143
  """
@@ -163,7 +168,7 @@ def create_purchase_order(vendor: str, amount: str) -> str:
163
  }
164
  return json.dumps(mock_data, indent=2)
165
 
166
- # Initialize Agent - FIXED: Removed max_iterations
167
  @st.cache_resource
168
  def get_agent():
169
  tools = [
@@ -175,18 +180,15 @@ def get_agent():
175
  create_purchase_order
176
  ]
177
 
178
- # Fixed: No max_iterations parameter
179
  return CodeAgent(
180
  tools=tools,
181
- model=get_model()
182
  )
183
 
184
  def main():
185
- # Header
186
  st.title("πŸ€– AI Procurement Agent")
187
- st.markdown("*Demo powered by Hugging Face SmolAgents*")
188
 
189
- # Add info about the demo
190
  with st.expander("ℹ️ About this Demo"):
191
  st.markdown("""
192
  This AI agent can help automate procurement workflows by:
@@ -214,7 +216,7 @@ def main():
214
  )
215
 
216
  st.markdown("---")
217
- st.markdown("**πŸš€ Deployed on Hugging Face Spaces**")
218
 
219
  # Main content
220
  col1, col2 = st.columns([1, 1])
@@ -262,8 +264,8 @@ def main():
262
  with st.expander("πŸ“‹ Full Agent Response", expanded=True):
263
  st.markdown(st.session_state['agent_response'])
264
 
 
265
  def process_full_workflow(pr_number):
266
- """Process complete procurement workflow"""
267
  with st.spinner("πŸ€– AI Agent is working on your procurement request..."):
268
  prompt = f"""
269
  Execute a complete procurement workflow for PR {pr_number}:
@@ -287,7 +289,6 @@ def process_full_workflow(pr_number):
287
  st.error(f"Error: {str(e)}")
288
 
289
  def evaluate_vendor(vendor_name):
290
- """Evaluate specific vendor"""
291
  with st.spinner(f"πŸ€– Evaluating {vendor_name}..."):
292
  prompt = f"""
293
  Conduct a comprehensive evaluation of vendor '{vendor_name}':
@@ -307,7 +308,6 @@ def evaluate_vendor(vendor_name):
307
  st.error(f"Error: {str(e)}")
308
 
309
  def analyze_prices(material):
310
- """Analyze material prices"""
311
  with st.spinner(f"πŸ€– Analyzing prices for {material}..."):
312
  prompt = f"""
313
  Perform price analysis for '{material}':
@@ -327,7 +327,6 @@ def analyze_prices(material):
327
  st.error(f"Error: {str(e)}")
328
 
329
  def run_custom_query(query):
330
- """Run custom user query"""
331
  with st.spinner("πŸ€– Processing your custom query..."):
332
  try:
333
  agent = get_agent()
 
1
  import streamlit as st
2
  import os
3
+ from smolagents import CodeAgent, LiteLLMModel, tool
4
  import json
5
  from datetime import datetime
6
  import random
 
12
  layout="wide"
13
  )
14
 
15
+ # Initialize OpenAI model for Spaces
16
  @st.cache_resource
17
  def get_model():
18
+ api_key = os.environ.get("OPENAI_API_KEY")
19
+ if not api_key:
20
+ st.error("⚠️ OpenAI API Key not found in Spaces secrets!")
21
+ st.info("Add OPENAI_API_KEY to your Spaces secrets in Settings.")
22
+ st.stop()
23
+
24
+ return LiteLLMModel(
25
+ model_id="gpt-4",
26
+ api_key=api_key,
27
+ temperature=0.2
28
+ )
29
 
30
+ # [All your tool definitions remain exactly the same]
31
  @tool
32
  def query_sap_pr(pr_number: str) -> str:
33
  """
 
49
  }
50
  return json.dumps(mock_data, indent=2)
51
 
 
52
  @tool
53
  def check_vendor_solvency(vendor_name: str) -> str:
54
  """
 
70
  }
71
  return json.dumps(mock_data, indent=2)
72
 
 
73
  @tool
74
  def get_vendor_performance(vendor_name: str) -> str:
75
  """
 
91
  }
92
  return json.dumps(mock_data, indent=2)
93
 
 
94
  @tool
95
  def compare_material_rates(material: str) -> str:
96
  """
 
120
  }
121
  return json.dumps(mock_data, indent=2)
122
 
 
123
  @tool
124
  def optimize_vendor_selection(requirements: str) -> str:
125
  """
 
143
  }
144
  return json.dumps(mock_data, indent=2)
145
 
 
146
  @tool
147
  def create_purchase_order(vendor: str, amount: str) -> str:
148
  """
 
168
  }
169
  return json.dumps(mock_data, indent=2)
170
 
171
+ # Initialize Agent with OpenAI - FIXED: Removed max_iterations
172
  @st.cache_resource
173
  def get_agent():
174
  tools = [
 
180
  create_purchase_order
181
  ]
182
 
 
183
  return CodeAgent(
184
  tools=tools,
185
+ model=get_model() # Using OpenAI via LiteLLM
186
  )
187
 
188
  def main():
 
189
  st.title("πŸ€– AI Procurement Agent")
190
+ st.markdown("*Demo powered by Hugging Face SmolAgents + OpenAI GPT-4*")
191
 
 
192
  with st.expander("ℹ️ About this Demo"):
193
  st.markdown("""
194
  This AI agent can help automate procurement workflows by:
 
216
  )
217
 
218
  st.markdown("---")
219
+ st.markdown("**πŸš€ Using OpenAI GPT-4**")
220
 
221
  # Main content
222
  col1, col2 = st.columns([1, 1])
 
264
  with st.expander("πŸ“‹ Full Agent Response", expanded=True):
265
  st.markdown(st.session_state['agent_response'])
266
 
267
+ # [Include all the workflow functions exactly as before]
268
  def process_full_workflow(pr_number):
 
269
  with st.spinner("πŸ€– AI Agent is working on your procurement request..."):
270
  prompt = f"""
271
  Execute a complete procurement workflow for PR {pr_number}:
 
289
  st.error(f"Error: {str(e)}")
290
 
291
  def evaluate_vendor(vendor_name):
 
292
  with st.spinner(f"πŸ€– Evaluating {vendor_name}..."):
293
  prompt = f"""
294
  Conduct a comprehensive evaluation of vendor '{vendor_name}':
 
308
  st.error(f"Error: {str(e)}")
309
 
310
  def analyze_prices(material):
 
311
  with st.spinner(f"πŸ€– Analyzing prices for {material}..."):
312
  prompt = f"""
313
  Perform price analysis for '{material}':
 
327
  st.error(f"Error: {str(e)}")
328
 
329
  def run_custom_query(query):
 
330
  with st.spinner("πŸ€– Processing your custom query..."):
331
  try:
332
  agent = get_agent()