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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
from plotly.subplots import make_subplots
|
| 7 |
+
import json
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
import os
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
import re
|
| 12 |
+
|
| 13 |
+
# Initialize Grok API client
|
| 14 |
+
client = OpenAI(
|
| 15 |
+
api_key=os.environ.get("XAI_API_KEY", "your-xai-api-key-here"),
|
| 16 |
+
base_url="https://api.x.ai/v1"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Load financial data
|
| 20 |
+
df = pd.read_csv('zero_b_financial_data.csv')
|
| 21 |
+
|
| 22 |
+
# Define system prompt for Zero B CFO
|
| 23 |
+
SYSTEM_PROMPT = """You are a highly experienced CFO of Zero B, a leading consumer goods water purification company operating as part of Ion Exchange India Limited.
|
| 24 |
+
|
| 25 |
+
Key Information About Zero B:
|
| 26 |
+
- Zero B operates in the residential water purifier market in India
|
| 27 |
+
- Primary products: RO systems (45% revenue), Water Softeners (30%), UV systems (15%), Others (10%)
|
| 28 |
+
- Distribution channels: Direct (35%), Retail (40%), E-commerce (25%)
|
| 29 |
+
- Monthly revenue range: ₹110-175 crores
|
| 30 |
+
- Gross margin: ~40% | Operating margin: ~17% | Net margin: ~13%
|
| 31 |
+
- Key metrics tracked: Weekly sales reconciliation, margin trends, channel performance, product mix
|
| 32 |
+
|
| 33 |
+
You have 15+ years of experience in finance and closely work with the CEO on strategic initiatives.
|
| 34 |
+
|
| 35 |
+
When analyzing data:
|
| 36 |
+
1. Provide crisp, pointed answers first
|
| 37 |
+
2. Then offer detailed breakdown if asked
|
| 38 |
+
3. Use actual data from Zero B's financial records
|
| 39 |
+
4. Make reasonable assumptions where data is unavailable
|
| 40 |
+
5. Focus on actionable insights
|
| 41 |
+
6. Flag any concerning trends immediately
|
| 42 |
+
7. Always relate findings back to business impact
|
| 43 |
+
|
| 44 |
+
Important Guidelines:
|
| 45 |
+
- ONLY analyze Zero B's financial data and similar water purifier market competitors
|
| 46 |
+
- Limit answers to financial metrics and business implications
|
| 47 |
+
- Do not provide information outside Zero B's operations
|
| 48 |
+
- Be specific with numbers and percentages
|
| 49 |
+
- Provide week-on-week or month-on-month comparisons
|
| 50 |
+
- Identify root causes of variances
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
def load_data():
|
| 54 |
+
"""Load and process financial data"""
|
| 55 |
+
df_load = pd.read_csv('zero_b_financial_data.csv')
|
| 56 |
+
df_load['Date'] = pd.to_datetime(df_load['Date'], format='%d-%m-%Y')
|
| 57 |
+
return df_load.sort_values('Date')
|
| 58 |
+
|
| 59 |
+
def create_ytd_chart():
|
| 60 |
+
"""Create YTD monthly plan vs actual sales chart"""
|
| 61 |
+
df_chart = load_data()
|
| 62 |
+
|
| 63 |
+
fig = go.Figure()
|
| 64 |
+
|
| 65 |
+
fig.add_trace(go.Scatter(
|
| 66 |
+
x=df_chart['Month'],
|
| 67 |
+
y=df_chart['Budget Sales (₹ Cr)'],
|
| 68 |
+
mode='lines+markers',
|
| 69 |
+
name='Budget',
|
| 70 |
+
line=dict(color='#1f77b4', width=3),
|
| 71 |
+
marker=dict(size=8),
|
| 72 |
+
))
|
| 73 |
+
|
| 74 |
+
fig.add_trace(go.Scatter(
|
| 75 |
+
x=df_chart['Month'],
|
| 76 |
+
y=df_chart['Actual Sales (₹ Cr)'],
|
| 77 |
+
mode='lines+markers',
|
| 78 |
+
name='Actual',
|
| 79 |
+
line=dict(color='#ff7f0e', width=3),
|
| 80 |
+
marker=dict(size=8),
|
| 81 |
+
))
|
| 82 |
+
|
| 83 |
+
fig.update_layout(
|
| 84 |
+
title='<b>YTD Monthly Plan vs Actual Sales</b>',
|
| 85 |
+
xaxis_title='Month',
|
| 86 |
+
yaxis_title='Sales (₹ Cr)',
|
| 87 |
+
hovermode='x unified',
|
| 88 |
+
template='plotly_white',
|
| 89 |
+
height=300,
|
| 90 |
+
font=dict(size=11),
|
| 91 |
+
margin=dict(l=50, r=50, t=50, b=50),
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
return fig
|
| 95 |
+
|
| 96 |
+
def create_product_mix_chart():
|
| 97 |
+
"""Create product mix chart"""
|
| 98 |
+
df_chart = load_data()
|
| 99 |
+
latest_month = df_chart.iloc[-1]
|
| 100 |
+
|
| 101 |
+
products = ['RO Systems', 'Water Softeners', 'UV Systems', 'Others']
|
| 102 |
+
values = [
|
| 103 |
+
latest_month['RO Sales (₹ Cr)'],
|
| 104 |
+
latest_month['Softener Sales (₹ Cr)'],
|
| 105 |
+
latest_month['UV Sales (₹ Cr)'],
|
| 106 |
+
latest_month['Others Sales (₹ Cr)']
|
| 107 |
+
]
|
| 108 |
+
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']
|
| 109 |
+
|
| 110 |
+
fig = go.Figure(data=[go.Pie(labels=products, values=values, marker=dict(colors=colors))])
|
| 111 |
+
|
| 112 |
+
fig.update_layout(
|
| 113 |
+
title=f'<b>Product Mix - {latest_month["Month"]}</b>',
|
| 114 |
+
height=300,
|
| 115 |
+
font=dict(size=11),
|
| 116 |
+
margin=dict(l=50, r=50, t=50, b=50),
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
return fig
|
| 120 |
+
|
| 121 |
+
def create_channel_mix_chart():
|
| 122 |
+
"""Create channel mix chart"""
|
| 123 |
+
df_chart = load_data()
|
| 124 |
+
latest_month = df_chart.iloc[-1]
|
| 125 |
+
|
| 126 |
+
channels = ['Direct', 'Retail', 'E-commerce']
|
| 127 |
+
values = [
|
| 128 |
+
latest_month['Direct Channel (₹ Cr)'],
|
| 129 |
+
latest_month['Retail Channel (₹ Cr)'],
|
| 130 |
+
latest_month['E-commerce Channel (₹ Cr)']
|
| 131 |
+
]
|
| 132 |
+
colors = ['#9467bd', '#8c564b', '#e377c2']
|
| 133 |
+
|
| 134 |
+
fig = go.Figure(data=[go.Pie(labels=channels, values=values, marker=dict(colors=colors))])
|
| 135 |
+
|
| 136 |
+
fig.update_layout(
|
| 137 |
+
title=f'<b>Channel Mix - {latest_month["Month"]}</b>',
|
| 138 |
+
height=300,
|
| 139 |
+
font=dict(size=11),
|
| 140 |
+
margin=dict(l=50, r=50, t=50, b=50),
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
return fig
|
| 144 |
+
|
| 145 |
+
def create_margin_view():
|
| 146 |
+
"""Create margin view per product"""
|
| 147 |
+
products = ['RO Systems', 'Water Softeners', 'UV Systems', 'Others']
|
| 148 |
+
margins = [42, 38, 40, 35]
|
| 149 |
+
|
| 150 |
+
fig = go.Figure(data=[
|
| 151 |
+
go.Bar(
|
| 152 |
+
x=products,
|
| 153 |
+
y=margins,
|
| 154 |
+
marker=dict(color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']),
|
| 155 |
+
text=[f'{m}%' for m in margins],
|
| 156 |
+
textposition='auto',
|
| 157 |
+
)
|
| 158 |
+
])
|
| 159 |
+
|
| 160 |
+
fig.update_layout(
|
| 161 |
+
title='<b>Gross Margin % by Product</b>',
|
| 162 |
+
xaxis_title='Product',
|
| 163 |
+
yaxis_title='Gross Margin %',
|
| 164 |
+
height=300,
|
| 165 |
+
template='plotly_white',
|
| 166 |
+
font=dict(size=11),
|
| 167 |
+
margin=dict(l=50, r=50, t=50, b=50),
|
| 168 |
+
showlegend=False,
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
return fig
|
| 172 |
+
|
| 173 |
+
def generate_kpi_html():
|
| 174 |
+
"""Generate HTML for KPI cards"""
|
| 175 |
+
df_dash = load_data()
|
| 176 |
+
latest = df_dash.iloc[-1]
|
| 177 |
+
prev = df_dash.iloc[-2] if len(df_dash) > 1 else latest
|
| 178 |
+
|
| 179 |
+
sales_growth = ((latest['Actual Sales (₹ Cr)'] - prev['Actual Sales (₹ Cr)']) / prev['Actual Sales (₹ Cr)'] * 100)
|
| 180 |
+
budget_variance = ((latest['Actual Sales (₹ Cr)'] - latest['Budget Sales (₹ Cr)']) / latest['Budget Sales (₹ Cr)'] * 100)
|
| 181 |
+
|
| 182 |
+
html = f"""
|
| 183 |
+
<div style="display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; margin-bottom: 15px; padding: 10px;">
|
| 184 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 15px; border-radius: 8px; color: white; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
| 185 |
+
<p style="font-size: 11px; opacity: 0.9; margin: 0;">CURRENT MONTH SALES</p>
|
| 186 |
+
<p style="font-size: 20px; font-weight: bold; margin: 5px 0 0 0;">₹{latest['Actual Sales (₹ Cr)']:.1f} Cr</p>
|
| 187 |
+
</div>
|
| 188 |
+
<div style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); padding: 15px; border-radius: 8px; color: white; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
| 189 |
+
<p style="font-size: 11px; opacity: 0.9; margin: 0;">GROSS MARGIN</p>
|
| 190 |
+
<p style="font-size: 20px; font-weight: bold; margin: 5px 0 0 0;">{latest['Gross Margin %']:.1f}%</p>
|
| 191 |
+
<p style="font-size: 10px; opacity: 0.9; margin: 5px 0 0 0;">₹{latest['Gross Margin (₹ Cr)']:.1f} Cr</p>
|
| 192 |
+
</div>
|
| 193 |
+
<div style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); padding: 15px; border-radius: 8px; color: white; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
| 194 |
+
<p style="font-size: 11px; opacity: 0.9; margin: 0;">EBITDA MARGIN</p>
|
| 195 |
+
<p style="font-size: 20px; font-weight: bold; margin: 5px 0 0 0;">{latest['EBITDA Margin %']:.1f}%</p>
|
| 196 |
+
</div>
|
| 197 |
+
<div style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%); padding: 15px; border-radius: 8px; color: white; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
| 198 |
+
<p style="font-size: 11px; opacity: 0.9; margin: 0;">BUDGET VARIANCE</p>
|
| 199 |
+
<p style="font-size: 20px; font-weight: bold; margin: 5px 0 0 0;">{'↑' if budget_variance > 0 else '↓'} {abs(budget_variance):.1f}%</p>
|
| 200 |
+
<p style="font-size: 10px; opacity: 0.9; margin: 5px 0 0 0;">{'On Track' if abs(budget_variance) < 5 else 'Needs Review'}</p>
|
| 201 |
+
</div>
|
| 202 |
+
<div style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%); padding: 15px; border-radius: 8px; color: white; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
| 203 |
+
<p style="font-size: 11px; opacity: 0.9; margin: 0;">MoM GROWTH</p>
|
| 204 |
+
<p style="font-size: 20px; font-weight: bold; margin: 5px 0 0 0;">{'↑' if sales_growth > 0 else '↓'} {abs(sales_growth):.1f}%</p>
|
| 205 |
+
</div>
|
| 206 |
+
</div>
|
| 207 |
+
"""
|
| 208 |
+
|
| 209 |
+
return html
|
| 210 |
+
|
| 211 |
+
def query_llm(user_question: str) -> str:
|
| 212 |
+
"""Query Grok LLM with financial context"""
|
| 213 |
+
|
| 214 |
+
df_context = load_data()
|
| 215 |
+
latest_data = df_context.iloc[-1].to_dict()
|
| 216 |
+
|
| 217 |
+
# Create context with financial data
|
| 218 |
+
financial_context = f"""
|
| 219 |
+
Current Financial Snapshot (Latest Month: {latest_data['Month']}):
|
| 220 |
+
- Sales: ₹{latest_data['Actual Sales (₹ Cr)']} Cr (Budget: ₹{latest_data['Budget Sales (₹ Cr)']} Cr)
|
| 221 |
+
- Product Mix: RO ₹{latest_data['RO Sales (₹ Cr)']} Cr | Softener ₹{latest_data['Softener Sales (₹ Cr)']} Cr | UV ₹{latest_data['UV Sales (₹ Cr)']} Cr
|
| 222 |
+
- Channel Mix: Direct ₹{latest_data['Direct Channel (₹ Cr)']} Cr | Retail ₹{latest_data['Retail Channel (₹ Cr)']} Cr | E-com ₹{latest_data['E-commerce Channel (₹ Cr)']} Cr
|
| 223 |
+
- Gross Margin: {latest_data['Gross Margin %']}% (₹{latest_data['Gross Margin (₹ Cr)']} Cr)
|
| 224 |
+
- EBITDA Margin: {latest_data['EBITDA Margin %']}% | EBIT Margin: {latest_data['EBIT Margin %']}% | PAT Margin: {latest_data['PAT Margin %']}%
|
| 225 |
+
- Working Capital: Inventory Days {latest_data['Inventory Days']}, Receivables Days {latest_data['Receivables Days']}, Payables Days {latest_data['Payables Days']}
|
| 226 |
+
|
| 227 |
+
Recent Trends (Last 3 Months):
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
for idx in range(max(0, len(df_context)-3), len(df_context)):
|
| 231 |
+
row = df_context.iloc[idx]
|
| 232 |
+
financial_context += f"- {row['Month']}: Sales ₹{row['Actual Sales (₹ Cr)']} Cr | PAT ₹{row['PAT (₹ Cr)']} Cr | Margin {row['PAT Margin %']}%\n"
|
| 233 |
+
|
| 234 |
+
user_prompt = f"""{financial_context}
|
| 235 |
+
|
| 236 |
+
User Question: {user_question}
|
| 237 |
+
|
| 238 |
+
Please provide a crisp, data-driven answer focusing on:
|
| 239 |
+
1. Direct answer to the question (1-2 sentences)
|
| 240 |
+
2. Key numbers and trends supporting the answer
|
| 241 |
+
3. Any concerns or opportunities identified
|
| 242 |
+
4. Recommended action (if applicable)
|
| 243 |
+
"""
|
| 244 |
+
|
| 245 |
+
try:
|
| 246 |
+
response = client.chat.completions.create(
|
| 247 |
+
model="grok-beta",
|
| 248 |
+
messages=[
|
| 249 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 250 |
+
{"role": "user", "content": user_prompt}
|
| 251 |
+
],
|
| 252 |
+
temperature=0.3,
|
| 253 |
+
max_tokens=500
|
| 254 |
+
)
|
| 255 |
+
return response.choices[0].message.content
|
| 256 |
+
except Exception as e:
|
| 257 |
+
return f"Error querying LLM: {str(e)}. Please ensure XAI_API_KEY environment variable is set."
|
| 258 |
+
|
| 259 |
+
def process_chat(question: str, chat_history: list) -> tuple:
|
| 260 |
+
"""Process user question and generate response"""
|
| 261 |
+
|
| 262 |
+
if not question.strip():
|
| 263 |
+
return chat_history, ""
|
| 264 |
+
|
| 265 |
+
# Generate LLM response
|
| 266 |
+
llm_response = query_llm(question)
|
| 267 |
+
|
| 268 |
+
# Update chat history
|
| 269 |
+
chat_history.append((question, llm_response))
|
| 270 |
+
|
| 271 |
+
return chat_history, ""
|
| 272 |
+
|
| 273 |
+
def create_app():
|
| 274 |
+
"""Create Gradio interface with vertical split (Top 70% / Bottom 30%)"""
|
| 275 |
+
|
| 276 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Zero B AI CFO Dashboard", css="""
|
| 277 |
+
.dashboard-section { height: 100%; overflow-y: auto; }
|
| 278 |
+
.chat-section { height: 100%; overflow-y: auto; }
|
| 279 |
+
#component-0 { height: 100vh; }
|
| 280 |
+
""") as demo:
|
| 281 |
+
gr.Markdown("""
|
| 282 |
+
# 🎯 Zero B AI CFO Dashboard
|
| 283 |
+
### Financial Intelligence for Consumer Goods Excellence
|
| 284 |
+
""")
|
| 285 |
+
|
| 286 |
+
# TOP 70% - DASHBOARD SECTION
|
| 287 |
+
with gr.Group(elem_classes="dashboard-section"):
|
| 288 |
+
# KPI Cards
|
| 289 |
+
kpi_html = gr.HTML()
|
| 290 |
+
|
| 291 |
+
# Charts in grid
|
| 292 |
+
with gr.Row():
|
| 293 |
+
chart1 = gr.Plot(label="YTD Sales")
|
| 294 |
+
chart2 = gr.Plot(label="Product Mix")
|
| 295 |
+
|
| 296 |
+
with gr.Row():
|
| 297 |
+
chart3 = gr.Plot(label="Channel Mix")
|
| 298 |
+
chart4 = gr.Plot(label="Margins")
|
| 299 |
+
|
| 300 |
+
# Narrative section
|
| 301 |
+
with gr.Row():
|
| 302 |
+
with gr.Column(scale=1):
|
| 303 |
+
narrative = gr.Markdown(value="### 📊 Dashboard Ready
|
| 304 |
+
Select questions from the chat panel to see targeted analysis.")
|
| 305 |
+
|
| 306 |
+
gr.Markdown("---") # Divider
|
| 307 |
+
|
| 308 |
+
# BOTTOM 30% - CHAT SECTION
|
| 309 |
+
with gr.Group(elem_classes="chat-section"):
|
| 310 |
+
gr.Markdown("### 💬 Ask Zero B AI CFO")
|
| 311 |
+
|
| 312 |
+
# Chat interface
|
| 313 |
+
chat_state = gr.State([])
|
| 314 |
+
chatbot = gr.Chatbot(label="Conversation", height=250)
|
| 315 |
+
|
| 316 |
+
with gr.Row():
|
| 317 |
+
question_input = gr.Textbox(
|
| 318 |
+
label="Your Question",
|
| 319 |
+
placeholder="E.g., Why did sales drop? What's impacting margins? How's e-commerce performing?",
|
| 320 |
+
lines=2,
|
| 321 |
+
scale=4
|
| 322 |
+
)
|
| 323 |
+
submit_btn = gr.Button("🔍 Analyze", variant="primary", scale=1)
|
| 324 |
+
|
| 325 |
+
with gr.Row():
|
| 326 |
+
clear_btn = gr.Button("🔄 Clear Chat", scale=1)
|
| 327 |
+
|
| 328 |
+
# Default dashboard on load
|
| 329 |
+
def load_dashboard():
|
| 330 |
+
df_load = load_data()
|
| 331 |
+
kpi_html_content = generate_kpi_html()
|
| 332 |
+
|
| 333 |
+
return (
|
| 334 |
+
kpi_html_content,
|
| 335 |
+
create_ytd_chart(),
|
| 336 |
+
create_product_mix_chart(),
|
| 337 |
+
create_channel_mix_chart(),
|
| 338 |
+
create_margin_view(),
|
| 339 |
+
"### 📊 Dashboard Ready\n\nAsk me about:\n- Sales trends and budget variance\n- Product performance and margins\n- Channel-wise analysis\n- Profitability drivers",
|
| 340 |
+
[]
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
# Submit handler
|
| 344 |
+
def handle_submit(question, chat_history):
|
| 345 |
+
updated_history, _ = process_chat(question, chat_history)
|
| 346 |
+
|
| 347 |
+
# Generate updated narrative based on question
|
| 348 |
+
if question.strip():
|
| 349 |
+
df_load = load_data()
|
| 350 |
+
latest = df_load.iloc[-1]
|
| 351 |
+
|
| 352 |
+
narrative_text = f"""
|
| 353 |
+
### 💡 AI Analysis Result
|
| 354 |
+
|
| 355 |
+
**Question:** {question}
|
| 356 |
+
|
| 357 |
+
**Key Metrics (Latest Month: {latest['Month']})**
|
| 358 |
+
- Sales: ₹{latest['Actual Sales (₹ Cr)']:.1f} Cr
|
| 359 |
+
- Gross Margin: {latest['Gross Margin %']:.1f}%
|
| 360 |
+
- EBITDA Margin: {latest['EBITDA Margin %']:.1f}%
|
| 361 |
+
- Channel Mix: Direct ₹{latest['Direct Channel (₹ Cr)']:.1f}Cr | Retail ₹{latest['Retail Channel (₹ Cr)']:.1f}Cr | E-com ₹{latest['E-commerce Channel (₹ Cr)']:.1f}Cr
|
| 362 |
+
"""
|
| 363 |
+
else:
|
| 364 |
+
narrative_text = "Please enter a question to get AI-powered analysis."
|
| 365 |
+
|
| 366 |
+
return (
|
| 367 |
+
updated_history,
|
| 368 |
+
"",
|
| 369 |
+
narrative_text
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
# Clear handler
|
| 373 |
+
def handle_clear():
|
| 374 |
+
dashboard_data = load_dashboard()
|
| 375 |
+
return (
|
| 376 |
+
[],
|
| 377 |
+
"",
|
| 378 |
+
dashboard_data[5]
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
# Event handlers
|
| 382 |
+
submit_btn.click(
|
| 383 |
+
handle_submit,
|
| 384 |
+
inputs=[question_input, chat_state],
|
| 385 |
+
outputs=[chatbot, question_input, narrative]
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
clear_btn.click(
|
| 389 |
+
handle_clear,
|
| 390 |
+
outputs=[chatbot, question_input, narrative]
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
# Load dashboard on startup
|
| 394 |
+
demo.load(
|
| 395 |
+
load_dashboard,
|
| 396 |
+
outputs=[kpi_html, chart1, chart2, chart3, chart4, narrative, chat_state]
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
return demo
|
| 400 |
+
|
| 401 |
+
if __name__ == "__main__":
|
| 402 |
+
app = create_app()
|
| 403 |
+
app.launch(
|
| 404 |
+
share=True,
|
| 405 |
+
server_name="0.0.0.0",
|
| 406 |
+
server_port=7860,
|
| 407 |
+
debug=True
|
| 408 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.26.0
|
| 2 |
+
pandas==2.1.4
|
| 3 |
+
numpy==1.26.3
|
| 4 |
+
plotly==5.18.0
|
| 5 |
+
openai==1.13.3
|
| 6 |
+
python-dotenv==1.0.0
|
zero_b_financial_data.csv
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Month,Date,Actual Sales (₹ Cr),Budget Sales (₹ Cr),RO Sales (₹ Cr),Softener Sales (₹ Cr),UV Sales (₹ Cr),Others Sales (₹ Cr),Direct Channel (₹ Cr),Retail Channel (₹ Cr),E-commerce Channel (₹ Cr),COGS (₹ Cr),Gross Margin (₹ Cr),Gross Margin %,Operating Expenses (₹ Cr),EBITDA (₹ Cr),EBITDA Margin %,Depreciation (₹ Cr),EBIT (₹ Cr),EBIT Margin %,Tax (₹ Cr),PAT (₹ Cr),PAT Margin %,Inventory Days,Receivables Days,Payables Days
|
| 2 |
+
April 2024,01-04-2024,131.53,131.53,59.19,39.46,19.73,13.15,46.03,52.61,32.88,79.18,52.35,39.8,26.31,26.04,19.8,2.63,23.41,17.8,5.85,17.56,13.35,29.6,26.3,38.0
|
| 3 |
+
May 2024,01-05-2024,126.39,133.5,56.87,37.92,18.96,12.64,44.24,50.55,31.6,76.09,50.3,39.8,25.28,25.02,19.8,2.53,22.5,17.8,5.62,16.87,13.35,29.3,28.2,36.5
|
| 4 |
+
June 2024,01-06-2024,126.18,135.5,56.78,37.85,18.93,12.62,44.16,50.47,31.54,75.96,50.22,39.8,25.24,24.98,19.8,2.52,22.46,17.8,5.61,16.84,13.35,31.6,24.1,34.1
|
| 5 |
+
July 2024,01-07-2024,135.98,137.53,61.19,40.79,20.4,13.6,47.59,54.39,34.0,81.86,54.12,39.8,27.2,26.92,19.8,2.72,24.2,17.8,6.05,18.15,13.35,24.3,21.6,33.9
|
| 6 |
+
August 2024,01-08-2024,124.7,139.59,56.12,37.41,18.71,12.47,43.65,49.88,31.18,75.07,49.63,39.8,24.94,24.69,19.8,2.49,22.2,17.8,5.55,16.65,13.35,30.9,23.2,32.2
|
| 7 |
+
September 2024,01-09-2024,154.18,141.68,69.38,46.25,23.13,15.42,53.96,61.67,38.55,92.82,61.36,39.8,30.84,30.53,19.8,3.08,27.44,17.8,6.86,20.58,13.35,29.3,25.1,32.2
|
| 8 |
+
October 2024,01-10-2024,110.85,143.81,49.88,33.26,16.63,11.09,38.8,44.34,27.71,66.73,44.12,39.8,22.17,21.95,19.8,2.22,19.73,17.8,4.93,14.8,13.35,30.3,22.7,35.8
|
| 9 |
+
November 2024,01-11-2024,112.14,145.97,50.46,33.64,16.82,11.21,39.25,44.86,28.03,67.51,44.63,39.8,22.43,22.2,19.8,2.24,19.96,17.8,4.99,14.97,13.35,29.1,23.8,38.7
|
| 10 |
+
December 2024,01-12-2024,119.57,148.16,53.81,35.87,17.94,11.96,41.85,47.83,29.89,71.98,47.59,39.8,23.91,23.68,19.8,2.39,21.28,17.8,5.32,15.96,13.35,26.8,26.6,32.6
|
| 11 |
+
January 2025,01-01-2025,123.63,150.38,55.63,37.09,18.54,12.36,43.27,49.45,30.91,74.43,49.21,39.8,24.73,24.48,19.8,2.47,22.01,17.8,5.5,16.5,13.35,24.1,22.3,35.4
|
| 12 |
+
February 2025,01-02-2025,130.8,152.64,58.86,39.24,19.62,13.08,45.78,52.32,32.7,78.74,52.06,39.8,26.16,25.9,19.8,2.62,23.28,17.8,5.82,17.46,13.35,30.5,24.8,34.4
|
| 13 |
+
March 2025,01-03-2025,110.57,154.93,49.76,33.17,16.59,11.06,38.7,44.23,27.64,66.56,44.01,39.8,22.11,21.89,19.8,2.21,19.68,17.8,4.92,14.76,13.35,27.8,24.1,37.1
|
| 14 |
+
April 2025,01-04-2025,158.34,157.25,71.25,47.5,23.75,15.83,55.42,63.33,39.58,95.32,63.02,39.8,31.67,31.35,19.8,3.17,28.18,17.8,7.05,21.14,13.35,24.7,25.6,34.2
|
| 15 |
+
May 2025,01-05-2025,147.93,159.61,66.57,44.38,22.19,14.79,51.78,59.17,36.98,89.05,58.88,39.8,29.59,29.29,19.8,2.96,26.33,17.8,6.58,19.75,13.35,31.8,27.1,36.9
|
| 16 |
+
June 2025,01-06-2025,148.05,162.0,66.62,44.41,22.21,14.8,51.82,59.22,37.01,89.12,58.92,39.8,29.61,29.31,19.8,2.96,26.35,17.8,6.59,19.76,13.35,29.1,25.7,37.0
|
| 17 |
+
July 2025,01-07-2025,154.83,164.43,69.67,46.45,23.22,15.48,54.19,61.93,38.71,93.21,61.62,39.8,30.97,30.66,19.8,3.1,27.56,17.8,6.89,20.67,13.35,29.4,22.8,32.6
|
| 18 |
+
August 2025,01-08-2025,173.91,166.9,78.26,52.17,26.09,17.39,60.87,69.57,43.48,104.7,69.22,39.8,34.78,34.44,19.8,3.48,30.96,17.8,7.74,23.22,13.35,34.1,24.9,37.0
|
| 19 |
+
September 2025,01-09-2025,170.39,169.4,76.68,51.12,25.56,17.04,59.64,68.16,42.6,102.58,67.82,39.8,34.08,33.74,19.8,3.41,30.33,17.8,7.58,22.75,13.35,28.1,25.7,38.1
|
| 20 |
+
October 2025,01-10-2025,138.3,171.94,62.24,41.49,20.75,13.83,48.41,55.32,34.58,83.26,55.04,39.8,27.66,27.38,19.8,2.77,24.62,17.8,6.15,18.46,13.35,34.7,19.8,36.6
|