Spaces:
Paused
Paused
WaysAhead Global commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,14 @@ import numpy as np
|
|
| 4 |
import os
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import seaborn as sns
|
| 7 |
-
from
|
| 8 |
from langchain_openai import ChatOpenAI
|
| 9 |
from langchain.chains import LLMChain
|
| 10 |
from langchain.prompts import PromptTemplate
|
| 11 |
-
from dotenv import load_dotenv
|
| 12 |
from pycaret.regression import setup as setup_reg, compare_models, predict_model
|
| 13 |
from pandasai import SmartDatalake
|
| 14 |
from pandasai.llm import OpenAI
|
|
|
|
| 15 |
|
| 16 |
# β
Load Environment Variables
|
| 17 |
load_dotenv()
|
|
@@ -49,78 +49,54 @@ store_categories = {
|
|
| 49 |
"REN by Sushi Library", "Sushi Library", "Tim Hortons", "LM Exchange"]
|
| 50 |
}
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# β
Streamlit Page Configuration
|
| 53 |
st.set_page_config(page_title="AI Store Replacement & Chatbot", layout="wide")
|
| 54 |
|
| 55 |
# β
Title
|
| 56 |
-
st.title("π AI-Powered Store Replacement, AutoML Demand Prediction &
|
| 57 |
-
|
| 58 |
-
# β
Data Preview
|
| 59 |
-
st.subheader("π
|
| 60 |
-
st.dataframe(df)
|
| 61 |
-
|
| 62 |
-
# β
Store Replacement System
|
| 63 |
-
st.subheader("π¬ AI-Based Store Replacement")
|
| 64 |
-
|
| 65 |
-
# Select Mall
|
| 66 |
-
selected_mall = st.selectbox("π¬ Select a Mall:", df["Mall"].unique())
|
| 67 |
-
|
| 68 |
-
if selected_mall:
|
| 69 |
-
# Get all stores in the selected mall
|
| 70 |
-
mall_data = df[df["Mall"] == selected_mall]
|
| 71 |
-
|
| 72 |
-
# Select the store that will be replaced
|
| 73 |
-
store_to_replace = st.selectbox("πͺ Select the store that is closing:", mall_data["Store_Brand"].unique())
|
| 74 |
-
|
| 75 |
-
if store_to_replace:
|
| 76 |
-
llm = ChatOpenAI(model="gpt-4", api_key=openai_api_key)
|
| 77 |
-
|
| 78 |
-
# β
Identify Store Category
|
| 79 |
-
store_category = None
|
| 80 |
-
for category, stores in store_categories.items():
|
| 81 |
-
if store_to_replace in stores:
|
| 82 |
-
store_category = category
|
| 83 |
-
break
|
| 84 |
-
|
| 85 |
-
if not store_category:
|
| 86 |
-
store_category = "Others"
|
| 87 |
-
|
| 88 |
-
st.write(f"π **Identified Category:** {store_category}")
|
| 89 |
-
|
| 90 |
-
# β
Find Replacement Stores
|
| 91 |
-
existing_stores = set(mall_data["Store_Brand"].unique())
|
| 92 |
-
potential_replacements = [store for store in store_categories.get(store_category, []) if store not in existing_stores]
|
| 93 |
-
|
| 94 |
-
if potential_replacements:
|
| 95 |
-
st.success(f"π **Recommended Replacement Stores:**")
|
| 96 |
-
for replacement_store in potential_replacements[:3]:
|
| 97 |
-
store_data = df[df["Store_Brand"] == replacement_store]
|
| 98 |
-
|
| 99 |
-
if not store_data.empty:
|
| 100 |
-
# β
AutoML Model Selection
|
| 101 |
-
setup_reg(df, target="Annual_Sales", silent=True, verbose=False)
|
| 102 |
-
best_model = compare_models()
|
| 103 |
-
projected_sales = predict_model(best_model, data=store_data)["Label"].mean()
|
| 104 |
-
|
| 105 |
-
setup_reg(df, target="Footfall", silent=True, verbose=False)
|
| 106 |
-
best_model = compare_models()
|
| 107 |
-
projected_footfall = predict_model(best_model, data=store_data)["Label"].mean()
|
| 108 |
-
|
| 109 |
-
setup_reg(df, target="Gross_Margin", silent=True, verbose=False)
|
| 110 |
-
best_model = compare_models()
|
| 111 |
-
projected_margin = predict_model(best_model, data=store_data)["Label"].mean()
|
| 112 |
-
|
| 113 |
-
setup_reg(df, target="Conversion_Rate", silent=True, verbose=False)
|
| 114 |
-
best_model = compare_models()
|
| 115 |
-
projected_conversion = predict_model(best_model, data=store_data)["Label"].mean()
|
| 116 |
-
|
| 117 |
-
# β
Display KPIs
|
| 118 |
-
st.subheader(f"π Projected KPIs for {replacement_store}")
|
| 119 |
-
st.write(f"π **Best Model Used:** {best_model}")
|
| 120 |
-
st.write(f"π **Projected Sales:** AED {projected_sales:,.2f}")
|
| 121 |
-
st.write(f"π£ **Projected Footfall:** {projected_footfall:,.0f} visitors")
|
| 122 |
-
st.write(f"π° **Projected Margin:** AED {projected_margin:,.2f}")
|
| 123 |
-
st.write(f"π **Projected Conversion Rate:** {projected_conversion:.2f}%")
|
| 124 |
|
| 125 |
# β
AI-Powered Conversational Smart Data Lake
|
| 126 |
st.subheader("π¬ AI-Powered Data Chatbot")
|
|
@@ -136,3 +112,9 @@ if user_query:
|
|
| 136 |
st.markdown(f"π€ **AI:** {response}")
|
| 137 |
except Exception as e:
|
| 138 |
st.error(f"β AI Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import seaborn as sns
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
from langchain_openai import ChatOpenAI
|
| 9 |
from langchain.chains import LLMChain
|
| 10 |
from langchain.prompts import PromptTemplate
|
|
|
|
| 11 |
from pycaret.regression import setup as setup_reg, compare_models, predict_model
|
| 12 |
from pandasai import SmartDatalake
|
| 13 |
from pandasai.llm import OpenAI
|
| 14 |
+
from crewai import Crew, Agent, Task
|
| 15 |
|
| 16 |
# β
Load Environment Variables
|
| 17 |
load_dotenv()
|
|
|
|
| 49 |
"REN by Sushi Library", "Sushi Library", "Tim Hortons", "LM Exchange"]
|
| 50 |
}
|
| 51 |
|
| 52 |
+
# β
CrewAI Agents & Tasks
|
| 53 |
+
cleaning_agent = Agent(
|
| 54 |
+
role="Data Cleaning Agent",
|
| 55 |
+
goal="Ensure data quality by handling missing values and duplicates.",
|
| 56 |
+
backstory="An expert in data preprocessing, ensuring high-quality input for analysis.",
|
| 57 |
+
llm=ChatOpenAI(model="gpt-4"),
|
| 58 |
+
allow_delegation=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
competitor_agent = Agent(
|
| 62 |
+
role="Competitor Analysis Agent",
|
| 63 |
+
goal="Fetch competitors for the selected mall or generate AI-based fallback.",
|
| 64 |
+
llm=ChatOpenAI(model="gpt-4"),
|
| 65 |
+
allow_delegation=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
replacement_agent = Agent(
|
| 69 |
+
role="Store Replacement Agent",
|
| 70 |
+
goal="Find the best replacement stores matching the selected storeβs category.",
|
| 71 |
+
llm=ChatOpenAI(model="gpt-4"),
|
| 72 |
+
allow_delegation=True
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
prediction_agent = Agent(
|
| 76 |
+
role="KPI Prediction Agent",
|
| 77 |
+
goal="Use AutoML to predict sales, footfall, conversion rate, and margins.",
|
| 78 |
+
llm=ChatOpenAI(model="gpt-4"),
|
| 79 |
+
allow_delegation=True
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
chatbot_agent = Agent(
|
| 83 |
+
role="Conversational AI Agent",
|
| 84 |
+
goal="Answer business-related queries about the dataset.",
|
| 85 |
+
llm=ChatOpenAI(model="gpt-4"),
|
| 86 |
+
allow_delegation=True
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
crew = Crew(agents=[cleaning_agent, competitor_agent, replacement_agent, prediction_agent, chatbot_agent])
|
| 90 |
+
|
| 91 |
# β
Streamlit Page Configuration
|
| 92 |
st.set_page_config(page_title="AI Store Replacement & Chatbot", layout="wide")
|
| 93 |
|
| 94 |
# β
Title
|
| 95 |
+
st.title("π AI-Powered Store Replacement, AutoML Demand Prediction & CrewAI")
|
| 96 |
+
|
| 97 |
+
# β
Data Preview (First 10 Rows)
|
| 98 |
+
st.subheader("π Sample Data Preview - KPI Sheet")
|
| 99 |
+
st.dataframe(df.head(10))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# β
AI-Powered Conversational Smart Data Lake
|
| 102 |
st.subheader("π¬ AI-Powered Data Chatbot")
|
|
|
|
| 112 |
st.markdown(f"π€ **AI:** {response}")
|
| 113 |
except Exception as e:
|
| 114 |
st.error(f"β AI Error: {str(e)}")
|
| 115 |
+
|
| 116 |
+
# β
CrewAI Execution
|
| 117 |
+
st.subheader("π AI Task Execution with CrewAI")
|
| 118 |
+
st.write("Executing all AI agents...")
|
| 119 |
+
crew.kickoff()
|
| 120 |
+
st.success("β
CrewAI execution completed!")
|