Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,8 @@ import io
|
|
| 16 |
import docx
|
| 17 |
from pptx import Presentation
|
| 18 |
import PyPDF2
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Disable Docker globally
|
| 21 |
os.environ["AUTOGEN_USE_DOCKER"] = "0"
|
|
@@ -23,24 +25,28 @@ os.environ["AUTOGEN_USE_DOCKER"] = "0"
|
|
| 23 |
class SupplyChainOptimizer:
|
| 24 |
def __init__(self):
|
| 25 |
# Initialize API keys from environment variables
|
| 26 |
-
self.
|
|
|
|
| 27 |
self.tavily_api_key = os.environ.get("TAVILY_API_KEY") or os.environ.get("TAVILY_KEY")
|
| 28 |
|
| 29 |
# For development/testing, allow demo mode
|
| 30 |
self.demo_mode = False
|
| 31 |
-
if not self.
|
| 32 |
print("API keys not found. Running in demo mode.")
|
| 33 |
self.demo_mode = True
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Initialize Tavily client (only if not in demo mode)
|
| 38 |
-
if not self.demo_mode:
|
| 39 |
try:
|
| 40 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"Error initializing
|
| 43 |
self.demo_mode = True
|
|
|
|
| 44 |
|
| 45 |
# Initialize agents
|
| 46 |
self._setup_agents()
|
|
@@ -50,44 +56,74 @@ class SupplyChainOptimizer:
|
|
| 50 |
self.latest_optimization = ""
|
| 51 |
self.search_results = ""
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
# Register search tool
|
| 81 |
-
register_function(
|
| 82 |
-
self.tavily_search_tool,
|
| 83 |
-
caller=self.reasoning_agent,
|
| 84 |
-
executor=self.user_proxy,
|
| 85 |
-
name="tavily_search_tool",
|
| 86 |
-
description="Conducts real-time market research using Tavily."
|
| 87 |
-
)
|
| 88 |
-
else:
|
| 89 |
-
self.reasoning_agent = None
|
| 90 |
-
self.optimization_agent = None
|
| 91 |
|
| 92 |
def tavily_search_tool(self, query: Annotated[str, "Market Research Query"]) -> Annotated[str, "Search results"]:
|
| 93 |
"""Search tool using Tavily API"""
|
|
@@ -418,18 +454,9 @@ Note: Set your API keys for AI-powered optimization with real market data
|
|
| 418 |
"""
|
| 419 |
|
| 420 |
try:
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
message=reasoning_prompt,
|
| 425 |
-
cache=cache,
|
| 426 |
-
max_turns=1
|
| 427 |
-
)
|
| 428 |
-
|
| 429 |
-
if reasoning_result and reasoning_result.chat_history:
|
| 430 |
-
self.latest_analysis = reasoning_result.chat_history[-1]['content']
|
| 431 |
-
else:
|
| 432 |
-
self.latest_analysis = "Analysis completed but no detailed response received."
|
| 433 |
|
| 434 |
except Exception as e:
|
| 435 |
self.latest_analysis = f"Analysis error: {str(e)}"
|
|
@@ -457,19 +484,9 @@ Note: Set your API keys for AI-powered optimization with real market data
|
|
| 457 |
"""
|
| 458 |
|
| 459 |
try:
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
message=optimization_prompt,
|
| 464 |
-
cache=cache,
|
| 465 |
-
max_turns=1
|
| 466 |
-
)
|
| 467 |
-
|
| 468 |
-
if optimization_result and optimization_result.chat_history:
|
| 469 |
-
self.latest_optimization = optimization_result.chat_history[-1]['content']
|
| 470 |
-
else:
|
| 471 |
-
self.latest_optimization = "Optimization completed but no detailed response received."
|
| 472 |
-
|
| 473 |
except Exception as e:
|
| 474 |
self.latest_optimization = f"Optimization error: {str(e)}"
|
| 475 |
|
|
|
|
| 16 |
import docx
|
| 17 |
from pptx import Presentation
|
| 18 |
import PyPDF2
|
| 19 |
+
import boto3
|
| 20 |
+
from botocore.exceptions import ClientError
|
| 21 |
|
| 22 |
# Disable Docker globally
|
| 23 |
os.environ["AUTOGEN_USE_DOCKER"] = "0"
|
|
|
|
| 25 |
class SupplyChainOptimizer:
|
| 26 |
def __init__(self):
|
| 27 |
# Initialize API keys from environment variables
|
| 28 |
+
self.aws_access_key = os.environ.get("AWS_ACCESS_KEY_ID")
|
| 29 |
+
self.aws_secret_key = os.environ.get("AWS_SECRET_ACCESS_KEY")
|
| 30 |
self.tavily_api_key = os.environ.get("TAVILY_API_KEY") or os.environ.get("TAVILY_KEY")
|
| 31 |
|
| 32 |
# For development/testing, allow demo mode
|
| 33 |
self.demo_mode = False
|
| 34 |
+
if not self.aws_access_key or not self.aws_secret_key or not self.tavily_api_key:
|
| 35 |
print("API keys not found. Running in demo mode.")
|
| 36 |
self.demo_mode = True
|
| 37 |
+
else:
|
| 38 |
+
# Initialize Bedrock client
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
+
self.bedrock_client = boto3.client(
|
| 41 |
+
'bedrock-runtime',
|
| 42 |
+
aws_access_key_id=self.aws_access_key,
|
| 43 |
+
aws_secret_access_key=self.aws_secret_key,
|
| 44 |
+
region_name='us-east-1' # or your preferred region
|
| 45 |
+
)
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"Error initializing Bedrock: {e}")
|
| 48 |
self.demo_mode = True
|
| 49 |
+
|
| 50 |
|
| 51 |
# Initialize agents
|
| 52 |
self._setup_agents()
|
|
|
|
| 56 |
self.latest_optimization = ""
|
| 57 |
self.search_results = ""
|
| 58 |
|
| 59 |
+
def call_claude_api(self, prompt, system_message=""):
|
| 60 |
+
"""Call Claude via AWS Bedrock"""
|
| 61 |
+
if self.demo_mode:
|
| 62 |
+
return "Demo mode response"
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
body = {
|
| 66 |
+
"anthropic_version": "bedrock-2023-05-31",
|
| 67 |
+
"max_tokens": 4000,
|
| 68 |
+
"system": system_message,
|
| 69 |
+
"messages": [
|
| 70 |
+
{
|
| 71 |
+
"role": "user",
|
| 72 |
+
"content": prompt
|
| 73 |
+
}
|
| 74 |
+
]
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
response = self.bedrock_client.invoke_model(
|
| 78 |
+
modelId="anthropic.claude-3-haiku-20240307-v1:0",
|
| 79 |
+
body=json.dumps(body)
|
| 80 |
)
|
| 81 |
+
|
| 82 |
+
response_body = json.loads(response['body'].read())
|
| 83 |
+
return response_body['content'][0]['text']
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Error calling Claude API: {str(e)}"
|
| 87 |
|
| 88 |
+
def _setup_agents(self):
|
| 89 |
+
"""Setup agents with AWS Bedrock Claude"""
|
| 90 |
+
# Keep user proxy for compatibility but won't be used with direct API calls
|
| 91 |
+
self.user_proxy = None
|
| 92 |
+
|
| 93 |
+
# Only setup if not in demo mode
|
| 94 |
+
if not self.demo_mode:
|
| 95 |
+
# Initialize Bedrock client if not already done
|
| 96 |
+
if not hasattr(self, 'bedrock_client'):
|
| 97 |
+
try:
|
| 98 |
+
self.bedrock_client = boto3.client(
|
| 99 |
+
'bedrock-runtime',
|
| 100 |
+
aws_access_key_id=self.aws_access_key,
|
| 101 |
+
aws_secret_access_key=self.aws_secret_key,
|
| 102 |
+
region_name='us-east-1' # or your preferred region
|
| 103 |
+
)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"Error initializing Bedrock: {e}")
|
| 106 |
+
self.demo_mode = True
|
| 107 |
+
self.reasoning_agent = None
|
| 108 |
+
self.optimization_agent = None
|
| 109 |
+
return
|
| 110 |
+
|
| 111 |
+
# Store system messages for direct API calls
|
| 112 |
+
self.reasoning_system_message = """You are an expert supply chain analyst. Analyze forecast data using real-time search results.
|
| 113 |
+
Evaluate if forecasts are reasonable based on current market conditions, events, and trends.
|
| 114 |
+
Provide detailed reasoning and recommendations. Format your response clearly with proper structure and avoid using asterisks for emphasis."""
|
| 115 |
+
|
| 116 |
+
self.optimization_system_message = """You are a supply chain optimization expert. Create detailed redistribution plans.
|
| 117 |
+
Consider costs, travel time, inventory levels, and demand forecasts.
|
| 118 |
+
Provide step-by-step optimization plans with clear recommendations and cost analysis. Use professional formatting without asterisks."""
|
| 119 |
+
|
| 120 |
+
# Set agents as enabled (we'll use direct API calls)
|
| 121 |
+
self.reasoning_agent = "claude-enabled"
|
| 122 |
+
self.optimization_agent = "claude-enabled"
|
| 123 |
+
else:
|
| 124 |
+
self.reasoning_agent = None
|
| 125 |
+
self.optimization_agent = None
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
def tavily_search_tool(self, query: Annotated[str, "Market Research Query"]) -> Annotated[str, "Search results"]:
|
| 129 |
"""Search tool using Tavily API"""
|
|
|
|
| 454 |
"""
|
| 455 |
|
| 456 |
try:
|
| 457 |
+
system_msg = "You are an expert supply chain analyst. Analyze forecast data using real-time search results. Evaluate if forecasts are reasonable based on current market conditions, events, and trends. Provide detailed reasoning and recommendations. Format your response clearly with proper structure."
|
| 458 |
+
|
| 459 |
+
self.latest_analysis = self.call_claude_api(reasoning_prompt, system_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
|
| 461 |
except Exception as e:
|
| 462 |
self.latest_analysis = f"Analysis error: {str(e)}"
|
|
|
|
| 484 |
"""
|
| 485 |
|
| 486 |
try:
|
| 487 |
+
system_msg = "You are a supply chain optimization expert. Create detailed redistribution plans. Consider costs, travel time, inventory levels, and demand forecasts. Provide step-by-step optimization plans with clear recommendations and cost analysis."
|
| 488 |
+
|
| 489 |
+
self.latest_optimization = self.call_claude_api(optimization_prompt, system_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
except Exception as e:
|
| 491 |
self.latest_optimization = f"Optimization error: {str(e)}"
|
| 492 |
|