Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,54 @@ class StreamLitResponse(ResponseParser):
|
|
| 21 |
|
| 22 |
gemini_api_key = os.environ['Gemini']
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def generateResponse(dataFrame,prompt):
|
| 25 |
llm = GoogleGemini(api_key=gemini_api_key)
|
| 26 |
pandas_agent = SmartDataframe(dataFrame,config={"llm":llm, "response_parser":StreamLitResponse})
|
|
|
|
| 21 |
|
| 22 |
gemini_api_key = os.environ['Gemini']
|
| 23 |
|
| 24 |
+
def calculate_kpis(df):
|
| 25 |
+
"""
|
| 26 |
+
Calculates key performance indicators from a given transaction dataset.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
df: Pandas DataFrame containing transaction data.
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
A JSON object containing the calculated KPIs.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# Calculate Total Revenue
|
| 36 |
+
total_revenue = df['Price'] * df['Quantity'].sum()
|
| 37 |
+
|
| 38 |
+
# Calculate Top Five Products by Revenue
|
| 39 |
+
if df['Description'].nunique() > 5:
|
| 40 |
+
top_five_products = df.groupby('Description')['Price'].sum().nlargest(5).index.tolist()
|
| 41 |
+
else:
|
| 42 |
+
top_five_product = "there are less than 5 products in this dataset"
|
| 43 |
+
|
| 44 |
+
if df['Branch'].nunique() > 1:
|
| 45 |
+
best_branch = df.groupby('Name')['Price'].sum().nlargest(1).index.tolist()
|
| 46 |
+
else:
|
| 47 |
+
best_branch = "there is only one branch in this dataset"
|
| 48 |
+
|
| 49 |
+
# Calculate Average Order Value (AOV)
|
| 50 |
+
aov = df.groupby('Receipt No_')['Price'].sum().mean()
|
| 51 |
+
|
| 52 |
+
# Calculate Customer Purchase Frequency (Requires more data for accurate calculation)
|
| 53 |
+
# Assuming 'Member Card No_' is a unique identifier for customers
|
| 54 |
+
customer_purchase_frequency = df.groupby('Name.1')['Receipt No_'].nunique().mean()
|
| 55 |
+
|
| 56 |
+
# Calculate Estimated Customer Lifetime Value (CLTV) (Requires more data for accurate calculation)
|
| 57 |
+
# Assuming a simple CLTV model based on AOV and purchase frequency
|
| 58 |
+
estimated_cltv = aov * customer_purchase_frequency * 12 # Assuming annual value
|
| 59 |
+
|
| 60 |
+
# Create JSON output
|
| 61 |
+
kpis = {
|
| 62 |
+
"total_revenue": total_revenue,
|
| 63 |
+
"top_five_products": top_five_products,
|
| 64 |
+
"average_order_value": aov,
|
| 65 |
+
"customer_purchase_frequency": customer_purchase_frequency,
|
| 66 |
+
"estimated_cltv": estimated_cltv,
|
| 67 |
+
"best_performing_branch": best_branch
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
return json.dumps(kpis, indent=4)
|
| 71 |
+
|
| 72 |
def generateResponse(dataFrame,prompt):
|
| 73 |
llm = GoogleGemini(api_key=gemini_api_key)
|
| 74 |
pandas_agent = SmartDataframe(dataFrame,config={"llm":llm, "response_parser":StreamLitResponse})
|