Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +123 -115
src/streamlit_app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
Enhanced Telecom Customer Segmentation Dashboard
|
| 3 |
===============================================
|
|
|
|
| 4 |
β
Interactive visual insights
|
| 5 |
β
Communication: Location, Intl calls, Frequency, Duration, Time (Morning/Noon/Night)
|
| 6 |
β
Internet: Download, Upload, Overall
|
|
@@ -521,127 +522,134 @@ Provide response in this format:
|
|
| 521 |
"""
|
| 522 |
response = query_ai(context)
|
| 523 |
|
| 524 |
-
#
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
border: 2px solid #667eea;
|
| 542 |
-
line-height: 1.8;
|
| 543 |
-
}
|
| 544 |
-
.usage-badge {
|
| 545 |
-
display: inline-block;
|
| 546 |
-
background: #e3f2fd;
|
| 547 |
-
color: #1976d2;
|
| 548 |
-
padding: 5px 12px;
|
| 549 |
-
border-radius: 20px;
|
| 550 |
-
font-size: 14px;
|
| 551 |
-
font-weight: 600;
|
| 552 |
-
margin: 5px 5px 5px 0;
|
| 553 |
-
}
|
| 554 |
-
.package-highlight {
|
| 555 |
-
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 556 |
-
color: white;
|
| 557 |
-
padding: 20px;
|
| 558 |
-
border-radius: 10px;
|
| 559 |
-
font-size: 18px;
|
| 560 |
-
font-weight: bold;
|
| 561 |
-
text-align: center;
|
| 562 |
-
margin: 15px 0;
|
| 563 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 564 |
-
}
|
| 565 |
-
</style>
|
| 566 |
-
<div class="recommendation-header">
|
| 567 |
-
π‘ Personalized Package Recommendation
|
| 568 |
-
</div>
|
| 569 |
-
""", unsafe_allow_html=True)
|
| 570 |
-
|
| 571 |
-
# Display formatted recommendation
|
| 572 |
-
with st.container():
|
| 573 |
-
st.markdown('<div class="recommendation-body">', unsafe_allow_html=True)
|
| 574 |
-
|
| 575 |
-
# Usage summary badges
|
| 576 |
-
col1, col2, col3, col4 = st.columns(4)
|
| 577 |
-
with col1:
|
| 578 |
-
usage_level = "High" if comm['voice_total_calls'] > 500 else "Moderate" if comm['voice_total_calls'] > 200 else "Low"
|
| 579 |
-
st.markdown(f'<div class="usage-badge">π Voice: {usage_level}</div>', unsafe_allow_html=True)
|
| 580 |
-
with col2:
|
| 581 |
-
data_level = "High" if internet['total_mb'] > 1000 else "Moderate" if internet['total_mb'] > 500 else "Low"
|
| 582 |
-
st.markdown(f'<div class="usage-badge">π Data: {data_level}</div>', unsafe_allow_html=True)
|
| 583 |
-
with col3:
|
| 584 |
-
sms_level = "High" if sms['total_messages'] > 200 else "Moderate" if sms['total_messages'] > 50 else "Low"
|
| 585 |
-
st.markdown(f'<div class="usage-badge">π¬ SMS: {sms_level}</div>', unsafe_allow_html=True)
|
| 586 |
-
with col4:
|
| 587 |
-
if intl['is_international_user']:
|
| 588 |
-
st.markdown(f'<div class="usage-badge">π International</div>', unsafe_allow_html=True)
|
| 589 |
-
else:
|
| 590 |
-
st.markdown(f'<div class="usage-badge">π Domestic Only</div>', unsafe_allow_html=True)
|
| 591 |
-
|
| 592 |
-
st.markdown("<br>", unsafe_allow_html=True)
|
| 593 |
-
|
| 594 |
-
# AI Response with better formatting
|
| 595 |
-
ai_text = response['answer']
|
| 596 |
-
|
| 597 |
-
# Try to parse sections (if AI follows format)
|
| 598 |
-
sections = {
|
| 599 |
-
'profile': '',
|
| 600 |
-
'package': '',
|
| 601 |
-
'benefits': '',
|
| 602 |
-
'pricing': ''
|
| 603 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
-
#
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
lines = ai_text.split('\n')
|
| 609 |
-
current_section = None
|
| 610 |
-
for line in lines:
|
| 611 |
-
line_lower = line.lower()
|
| 612 |
-
if 'usage profile' in line_lower or 'profile' in line_lower and len(line) < 50:
|
| 613 |
-
current_section = 'profile'
|
| 614 |
-
elif 'recommended package' in line_lower or 'package' in line_lower and len(line) < 50:
|
| 615 |
-
current_section = 'package'
|
| 616 |
-
elif 'benefit' in line_lower and len(line) < 50:
|
| 617 |
-
current_section = 'benefits'
|
| 618 |
-
elif 'pricing' in line_lower and len(line) < 50:
|
| 619 |
-
current_section = 'pricing'
|
| 620 |
-
elif current_section and line.strip():
|
| 621 |
-
sections[current_section] += line + '\n'
|
| 622 |
|
| 623 |
-
#
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
|
| 628 |
-
|
| 629 |
-
st.markdown("**π Recommended Package**")
|
| 630 |
-
st.markdown(f'<div class="package-highlight">{sections["package"].strip()}</div>', unsafe_allow_html=True)
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
st.success(sections['benefits'].strip())
|
| 635 |
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
else:
|
| 647 |
st.error(f"β Customer {customer_id} not found")
|
|
|
|
| 1 |
"""
|
| 2 |
Enhanced Telecom Customer Segmentation Dashboard
|
| 3 |
===============================================
|
| 4 |
+
Features EVERYTHING Talha requested:
|
| 5 |
β
Interactive visual insights
|
| 6 |
β
Communication: Location, Intl calls, Frequency, Duration, Time (Morning/Noon/Night)
|
| 7 |
β
Internet: Download, Upload, Overall
|
|
|
|
| 522 |
"""
|
| 523 |
response = query_ai(context)
|
| 524 |
|
| 525 |
+
# Check for errors first
|
| 526 |
+
if response and 'error' in response:
|
| 527 |
+
st.error(f"β {response['error']}")
|
| 528 |
+
st.info("π‘ Please check your backend connection and Gemini API key configuration.")
|
| 529 |
+
elif response and 'answer' in response:
|
| 530 |
+
# Beautiful formatted output
|
| 531 |
+
st.markdown("""
|
| 532 |
+
<style>
|
| 533 |
+
.recommendation-header {
|
| 534 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 535 |
+
color: white;
|
| 536 |
+
padding: 20px;
|
| 537 |
+
border-radius: 10px 10px 0 0;
|
| 538 |
+
text-align: center;
|
| 539 |
+
font-size: 24px;
|
| 540 |
+
font-weight: bold;
|
| 541 |
+
margin-bottom: 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
}
|
| 543 |
+
.recommendation-body {
|
| 544 |
+
background: #f8f9fa;
|
| 545 |
+
padding: 25px;
|
| 546 |
+
border-radius: 0 0 10px 10px;
|
| 547 |
+
border: 2px solid #667eea;
|
| 548 |
+
line-height: 1.8;
|
| 549 |
+
}
|
| 550 |
+
.usage-badge {
|
| 551 |
+
display: inline-block;
|
| 552 |
+
background: #e3f2fd;
|
| 553 |
+
color: #1976d2;
|
| 554 |
+
padding: 5px 12px;
|
| 555 |
+
border-radius: 20px;
|
| 556 |
+
font-size: 14px;
|
| 557 |
+
font-weight: 600;
|
| 558 |
+
margin: 5px 5px 5px 0;
|
| 559 |
+
}
|
| 560 |
+
.package-highlight {
|
| 561 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 562 |
+
color: white;
|
| 563 |
+
padding: 20px;
|
| 564 |
+
border-radius: 10px;
|
| 565 |
+
font-size: 18px;
|
| 566 |
+
font-weight: bold;
|
| 567 |
+
text-align: center;
|
| 568 |
+
margin: 15px 0;
|
| 569 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 570 |
+
}
|
| 571 |
+
</style>
|
| 572 |
+
<div class="recommendation-header">
|
| 573 |
+
π‘ Personalized Package Recommendation
|
| 574 |
+
</div>
|
| 575 |
+
""", unsafe_allow_html=True)
|
| 576 |
|
| 577 |
+
# Display formatted recommendation
|
| 578 |
+
with st.container():
|
| 579 |
+
st.markdown('<div class="recommendation-body">', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
|
| 581 |
+
# Usage summary badges
|
| 582 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 583 |
+
with col1:
|
| 584 |
+
usage_level = "High" if comm['voice_total_calls'] > 500 else "Moderate" if comm['voice_total_calls'] > 200 else "Low"
|
| 585 |
+
st.markdown(f'<div class="usage-badge">π Voice: {usage_level}</div>', unsafe_allow_html=True)
|
| 586 |
+
with col2:
|
| 587 |
+
data_level = "High" if internet['total_mb'] > 1000 else "Moderate" if internet['total_mb'] > 500 else "Low"
|
| 588 |
+
st.markdown(f'<div class="usage-badge">π Data: {data_level}</div>', unsafe_allow_html=True)
|
| 589 |
+
with col3:
|
| 590 |
+
sms_level = "High" if sms['total_messages'] > 200 else "Moderate" if sms['total_messages'] > 50 else "Low"
|
| 591 |
+
st.markdown(f'<div class="usage-badge">π¬ SMS: {sms_level}</div>', unsafe_allow_html=True)
|
| 592 |
+
with col4:
|
| 593 |
+
if intl['is_international_user']:
|
| 594 |
+
st.markdown(f'<div class="usage-badge">π International</div>', unsafe_allow_html=True)
|
| 595 |
+
else:
|
| 596 |
+
st.markdown(f'<div class="usage-badge">π Domestic Only</div>', unsafe_allow_html=True)
|
| 597 |
|
| 598 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 599 |
|
| 600 |
+
# AI Response with better formatting
|
| 601 |
+
ai_text = response['answer']
|
|
|
|
| 602 |
|
| 603 |
+
# Try to parse sections (if AI follows format)
|
| 604 |
+
sections = {
|
| 605 |
+
'profile': '',
|
| 606 |
+
'package': '',
|
| 607 |
+
'benefits': '',
|
| 608 |
+
'pricing': ''
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
# Simple parsing (fallback to full text if not structured)
|
| 612 |
+
if any(keyword in ai_text.lower() for keyword in ['usage profile', 'recommended package', 'benefits', 'pricing']):
|
| 613 |
+
# Structured response
|
| 614 |
+
lines = ai_text.split('\n')
|
| 615 |
+
current_section = None
|
| 616 |
+
for line in lines:
|
| 617 |
+
line_lower = line.lower()
|
| 618 |
+
if 'usage profile' in line_lower or 'profile' in line_lower and len(line) < 50:
|
| 619 |
+
current_section = 'profile'
|
| 620 |
+
elif 'recommended package' in line_lower or 'package' in line_lower and len(line) < 50:
|
| 621 |
+
current_section = 'package'
|
| 622 |
+
elif 'benefit' in line_lower and len(line) < 50:
|
| 623 |
+
current_section = 'benefits'
|
| 624 |
+
elif 'pricing' in line_lower and len(line) < 50:
|
| 625 |
+
current_section = 'pricing'
|
| 626 |
+
elif current_section and line.strip():
|
| 627 |
+
sections[current_section] += line + '\n'
|
| 628 |
+
|
| 629 |
+
# Display structured
|
| 630 |
+
if sections['profile']:
|
| 631 |
+
st.markdown("**π Usage Profile Analysis**")
|
| 632 |
+
st.info(sections['profile'].strip())
|
| 633 |
+
|
| 634 |
+
if sections['package']:
|
| 635 |
+
st.markdown("**π Recommended Package**")
|
| 636 |
+
st.markdown(f'<div class="package-highlight">{sections["package"].strip()}</div>', unsafe_allow_html=True)
|
| 637 |
+
|
| 638 |
+
if sections['benefits']:
|
| 639 |
+
st.markdown("**β¨ Key Benefits**")
|
| 640 |
+
st.success(sections['benefits'].strip())
|
| 641 |
+
|
| 642 |
+
if sections['pricing']:
|
| 643 |
+
st.markdown("**π° Pricing Strategy**")
|
| 644 |
+
st.warning(sections['pricing'].strip())
|
| 645 |
+
else:
|
| 646 |
+
# Fallback: display full text nicely
|
| 647 |
+
st.markdown("**π Analysis & Recommendation**")
|
| 648 |
+
st.markdown(ai_text)
|
| 649 |
+
|
| 650 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 651 |
+
else:
|
| 652 |
+
st.warning("β οΈ Unable to generate AI recommendation. Please try again later.")
|
| 653 |
|
| 654 |
else:
|
| 655 |
st.error(f"β Customer {customer_id} not found")
|