Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
import sys
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
-
|
| 5 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
import chainlit as cl
|
| 10 |
|
| 11 |
from src.components.model_nlp_intent import predict_intent
|
|
@@ -13,9 +10,11 @@ from src.components.model_nlp_ner import extract_entities_pipeline
|
|
| 13 |
from src.components.model_risk_predictor import predict_risk
|
| 14 |
from src.components.recommendation_engine import generate_recommendation
|
| 15 |
|
|
|
|
| 16 |
@cl.on_chat_start
|
| 17 |
async def welcome():
|
| 18 |
-
|
|
|
|
| 19 |
content=(
|
| 20 |
"# π Welcome to AI-Powered Supply Chain Risk Advisor\n\n"
|
| 21 |
"I provide **real-time risk analysis** and **mitigation strategies** "
|
|
@@ -30,118 +29,144 @@ async def welcome():
|
|
| 30 |
"- \"Are there weather problems affecting shipments to Germany?\"\n"
|
| 31 |
"- \"Risk level for Mumbai to Singapore route?\"\n\n"
|
| 32 |
"**Ask me anything about your supply chain risks!** π"
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
|
| 38 |
@cl.on_message
|
| 39 |
async def handle_message(msg: cl.Message):
|
|
|
|
| 40 |
query = msg.content
|
| 41 |
-
session = cl.user_session
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
intent_result = predict_intent(query)
|
| 45 |
-
intent = intent_result["intent"]
|
| 46 |
-
confidence = intent_result["confidence"]
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
entities = extract_entities_pipeline(query)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
region = None
|
| 53 |
-
origin = None
|
| 54 |
-
destination = None
|
| 55 |
-
|
| 56 |
-
if entities.get("location"):
|
| 57 |
-
locations = entities["location"]
|
| 58 |
-
if isinstance(locations, list) and len(locations) > 0:
|
| 59 |
-
region = locations[0]
|
| 60 |
-
|
| 61 |
-
if len(locations) > 1:
|
| 62 |
-
origin = locations[0]
|
| 63 |
-
destination = locations[1]
|
| 64 |
-
else:
|
| 65 |
-
region = locations
|
| 66 |
-
|
| 67 |
-
if not region:
|
| 68 |
-
region = "Mumbai"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
incidents = []
|
| 72 |
-
event_type = None
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
content=response,
|
| 137 |
-
author="Supply Chain Risk Analysis"
|
| 138 |
-
).send()
|
| 139 |
-
|
| 140 |
-
# Send Alert Level
|
| 141 |
-
alert_emoji = "π¨" if risk_score >= 0.7 else "β οΈ" if risk_score >= 0.4 else "β
"
|
| 142 |
-
await cl.Message(
|
| 143 |
-
content=f"{alert_emoji} **Alert Level:** {advice['action'].upper()}",
|
| 144 |
-
author="Alert Level"
|
| 145 |
-
).send()
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 1 |
import sys
|
| 2 |
from pathlib import Path
|
| 3 |
|
|
|
|
| 4 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import chainlit as cl
|
| 7 |
|
| 8 |
from src.components.model_nlp_intent import predict_intent
|
|
|
|
| 10 |
from src.components.model_risk_predictor import predict_risk
|
| 11 |
from src.components.recommendation_engine import generate_recommendation
|
| 12 |
|
| 13 |
+
|
| 14 |
@cl.on_chat_start
|
| 15 |
async def welcome():
|
| 16 |
+
"""Welcome message displayed when chat starts"""
|
| 17 |
+
welcome_msg = cl.Message(
|
| 18 |
content=(
|
| 19 |
"# π Welcome to AI-Powered Supply Chain Risk Advisor\n\n"
|
| 20 |
"I provide **real-time risk analysis** and **mitigation strategies** "
|
|
|
|
| 29 |
"- \"Are there weather problems affecting shipments to Germany?\"\n"
|
| 30 |
"- \"Risk level for Mumbai to Singapore route?\"\n\n"
|
| 31 |
"**Ask me anything about your supply chain risks!** π"
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
await welcome_msg.send()
|
| 35 |
|
| 36 |
|
| 37 |
@cl.on_message
|
| 38 |
async def handle_message(msg: cl.Message):
|
| 39 |
+
"""Handle incoming messages and provide risk analysis"""
|
| 40 |
query = msg.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Show a loading message
|
| 43 |
+
loading_msg = cl.Message(content="π Analyzing your query...")
|
| 44 |
+
await loading_msg.send()
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# Predict intent
|
| 48 |
+
intent_result = predict_intent(query)
|
| 49 |
+
intent = intent_result["intent"]
|
| 50 |
+
confidence = intent_result["confidence"]
|
| 51 |
+
|
| 52 |
+
# Extract entities
|
| 53 |
+
entities = extract_entities_pipeline(query)
|
| 54 |
+
|
| 55 |
+
# Extract location information
|
| 56 |
+
region = None
|
| 57 |
+
origin = None
|
| 58 |
+
destination = None
|
| 59 |
+
|
| 60 |
+
if entities.get("location"):
|
| 61 |
+
locations = entities["location"]
|
| 62 |
+
if isinstance(locations, list) and len(locations) > 0:
|
| 63 |
+
region = locations[0]
|
| 64 |
+
|
| 65 |
+
if len(locations) > 1:
|
| 66 |
+
origin = locations[0]
|
| 67 |
+
destination = locations[1]
|
| 68 |
+
else:
|
| 69 |
+
region = locations
|
| 70 |
+
|
| 71 |
+
if not region:
|
| 72 |
+
region = "Mumbai"
|
| 73 |
+
|
| 74 |
+
# Extract event information
|
| 75 |
+
incidents = []
|
| 76 |
+
event_type = None
|
| 77 |
+
|
| 78 |
+
if entities.get("event"):
|
| 79 |
+
events = entities["event"]
|
| 80 |
+
if isinstance(events, list):
|
| 81 |
+
incidents = events
|
| 82 |
+
event_type = events[0] if events else None
|
| 83 |
+
else:
|
| 84 |
+
incidents = [events]
|
| 85 |
+
event_type = events
|
| 86 |
+
|
| 87 |
+
# Predict risk
|
| 88 |
+
risk_score = predict_risk(
|
| 89 |
+
region=region,
|
| 90 |
+
days=5,
|
| 91 |
+
origin=origin,
|
| 92 |
+
destination=destination,
|
| 93 |
+
event_type=event_type,
|
| 94 |
+
incidents=incidents
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Generate recommendation
|
| 98 |
+
recent_incidents = incidents if incidents else ["port strike", "supplier outage"]
|
| 99 |
+
weather_alert = "Typhoon warning" if region.lower() == "shanghai" else None
|
| 100 |
+
|
| 101 |
+
advice = generate_recommendation(
|
| 102 |
+
risk_score=risk_score,
|
| 103 |
+
region=region,
|
| 104 |
+
recent_incidents=recent_incidents,
|
| 105 |
+
weather_alert=weather_alert,
|
| 106 |
+
intent=intent
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Determine risk level
|
| 110 |
+
if risk_score >= 0.7:
|
| 111 |
+
risk_emoji = "π΄"
|
| 112 |
+
risk_level = "High"
|
| 113 |
+
elif risk_score >= 0.4:
|
| 114 |
+
risk_emoji = "π‘"
|
| 115 |
+
risk_level = "Medium"
|
| 116 |
else:
|
| 117 |
+
risk_emoji = "π’"
|
| 118 |
+
risk_level = "Low"
|
| 119 |
+
|
| 120 |
+
# Build response
|
| 121 |
+
response = (
|
| 122 |
+
f"### π Supply Chain Risk Analysis\n\n"
|
| 123 |
+
f"**Region:** {region}\n"
|
| 124 |
+
f"**Intent:** {intent} (Confidence: {confidence:.2%})\n"
|
| 125 |
+
f"**Entities:** {entities}\n"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if origin and destination:
|
| 129 |
+
response += f"**Route:** {origin} β {destination}\n"
|
| 130 |
+
|
| 131 |
+
if incidents:
|
| 132 |
+
response += f"**β οΈ Detected Events:** {', '.join(incidents)}\n"
|
| 133 |
+
|
| 134 |
+
response += f"**Risk Score:** {risk_emoji} **{risk_level}** ({risk_score:.2f})\n\n"
|
| 135 |
+
response += f"**π‘ Recommendation:**\n{advice['message']}\n"
|
| 136 |
+
|
| 137 |
+
# Remove loading message
|
| 138 |
+
await loading_msg.remove()
|
| 139 |
+
|
| 140 |
+
# Send main response
|
| 141 |
+
await cl.Message(content=response).send()
|
| 142 |
+
|
| 143 |
+
# Send alert level
|
| 144 |
+
alert_emoji = "π¨" if risk_score >= 0.7 else "β οΈ" if risk_score >= 0.4 else "β
"
|
| 145 |
+
await cl.Message(
|
| 146 |
+
content=f"{alert_emoji} **Alert Level:** {advice['action'].upper()}"
|
| 147 |
+
).send()
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
# Log the error for debugging
|
| 151 |
+
print(f"Error processing query: {str(e)}")
|
| 152 |
+
import traceback
|
| 153 |
+
traceback.print_exc()
|
| 154 |
+
|
| 155 |
+
# Remove loading message if it exists
|
| 156 |
+
try:
|
| 157 |
+
await loading_msg.remove()
|
| 158 |
+
except:
|
| 159 |
+
pass
|
| 160 |
+
|
| 161 |
+
# Send user-friendly error message
|
| 162 |
+
await cl.Message(
|
| 163 |
+
content=(
|
| 164 |
+
f"β **Error:** An error occurred while processing your request.\n\n"
|
| 165 |
+
f"**Details:** {str(e)}\n\n"
|
| 166 |
+
f"Please try:\n"
|
| 167 |
+
f"- Rephrasing your question\n"
|
| 168 |
+
f"- Being more specific about locations\n"
|
| 169 |
+
f"- Asking a different question\n\n"
|
| 170 |
+
f"Example: \"What is the risk level for shipments from Mumbai to Singapore?\""
|
| 171 |
+
)
|
| 172 |
+
).send()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|