Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -329,78 +329,169 @@ def detect_outliers(df):
|
|
| 329 |
return outliers
|
| 330 |
|
| 331 |
def generate_ai_summary(model, df, stats, outliers):
|
|
|
|
| 332 |
if not model:
|
| 333 |
return "AI analysis unavailable - API key not configured"
|
|
|
|
| 334 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
materials = [k for k in stats.keys() if k != '_total_']
|
| 336 |
-
context_parts = [
|
| 337 |
-
"# Production Data Analysis Context",
|
| 338 |
-
f"## Overview",
|
| 339 |
-
f"- Total Production: {stats['_total_']['total']:,.0f} kg",
|
| 340 |
-
f"- Production Period: {stats['_total_']['work_days']} working days",
|
| 341 |
-
f"- Daily Average: {stats['_total_']['daily_avg']:,.0f} kg",
|
| 342 |
-
f"- Materials Tracked: {len(materials)}",
|
| 343 |
-
"",
|
| 344 |
-
"## Material Breakdown:"
|
| 345 |
-
]
|
| 346 |
for material in materials:
|
| 347 |
info = stats[material]
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
trend_direction = "increasing" if daily_data.iloc[-1] > daily_data.iloc[0] else "decreasing"
|
| 351 |
-
volatility = daily_data.std() / daily_data.mean() * 100
|
| 352 |
-
context_parts.extend([
|
| 353 |
-
"",
|
| 354 |
-
"## Trend Analysis:",
|
| 355 |
-
f"- Overall trend: {trend_direction}",
|
| 356 |
-
f"- Production volatility: {volatility:.1f}% coefficient of variation",
|
| 357 |
-
f"- Peak production: {daily_data.max():,.0f} kg",
|
| 358 |
-
f"- Lowest production: {daily_data.min():,.0f} kg"
|
| 359 |
-
])
|
| 360 |
-
total_outliers = sum(info['count'] for info in outliers.values())
|
| 361 |
-
context_parts.extend([
|
| 362 |
-
"",
|
| 363 |
-
"## Quality Control:",
|
| 364 |
-
f"- Total outliers detected: {total_outliers}",
|
| 365 |
-
f"- Materials with quality issues: {sum(1 for info in outliers.values() if info['count'] > 0)}"
|
| 366 |
-
])
|
| 367 |
-
if 'shift' in df.columns:
|
| 368 |
-
shift_stats = df.groupby('shift')['weight_kg'].sum()
|
| 369 |
-
context_parts.extend([
|
| 370 |
-
"",
|
| 371 |
-
"## Shift Performance:",
|
| 372 |
-
f"- Day shift: {shift_stats.get('day', 0):,.0f} kg",
|
| 373 |
-
f"- Night shift: {shift_stats.get('night', 0):,.0f} kg"
|
| 374 |
-
])
|
| 375 |
-
context_text = "\n".join(context_parts)
|
| 376 |
prompt = f"""
|
| 377 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
-
|
|
|
|
| 380 |
|
| 381 |
-
|
|
|
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
-
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
-
|
| 396 |
-
β’ Recommendation Y: [Your recommendation, e.g., "Investigate the root causes of the 11 outliers..."] We recommend using the platform's interactive charts to drill down into the specific dates identified by the 'Quality Check' module.
|
| 397 |
|
| 398 |
-
|
| 399 |
"""
|
|
|
|
| 400 |
response = model.generate_content(prompt)
|
| 401 |
return response.text
|
|
|
|
| 402 |
except Exception as e:
|
| 403 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
def query_ai(model, stats, question, df=None):
|
| 406 |
if not model:
|
|
|
|
| 329 |
return outliers
|
| 330 |
|
| 331 |
def generate_ai_summary(model, df, stats, outliers):
|
| 332 |
+
"""Generate contractor value demonstration for management"""
|
| 333 |
if not model:
|
| 334 |
return "AI analysis unavailable - API key not configured"
|
| 335 |
+
|
| 336 |
try:
|
| 337 |
+
total_production = stats['_total_']['total']
|
| 338 |
+
work_days = stats['_total_']['work_days']
|
| 339 |
+
daily_avg = stats['_total_']['daily_avg']
|
| 340 |
+
|
| 341 |
+
# Calculate our technical achievements
|
| 342 |
+
total_outliers = sum(info['count'] for info in outliers.values())
|
| 343 |
+
data_quality = max(0, 100 - (total_outliers / len(df)) * 100)
|
| 344 |
+
|
| 345 |
+
context = f"""
|
| 346 |
+
CONTRACTOR DELIVERABLES REPORT - Nilsen Service & Consulting
|
| 347 |
+
|
| 348 |
+
Technical Implementation:
|
| 349 |
+
- Successfully processed {len(df):,} production records
|
| 350 |
+
- Monitoring system deployed covering {work_days} work days
|
| 351 |
+
- Real-time analytics for {total_production:,.0f} kg production data
|
| 352 |
+
- Data quality assurance: {data_quality:.1f}% accuracy achieved
|
| 353 |
+
|
| 354 |
+
System Capabilities Delivered:
|
| 355 |
+
- Multi-material tracking: {len([k for k in stats.keys() if k != '_total_'])} material types
|
| 356 |
+
- Automated outlier detection: {total_outliers} anomalies identified
|
| 357 |
+
- Production trend analysis with forecasting
|
| 358 |
+
- Interactive dashboards with AI insights
|
| 359 |
+
|
| 360 |
+
Material Coverage:
|
| 361 |
+
"""
|
| 362 |
materials = [k for k in stats.keys() if k != '_total_']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
for material in materials:
|
| 364 |
info = stats[material]
|
| 365 |
+
context += f"- {material.title()}: {info['total']:,.0f} kg monitored ({info['percentage']:.1f}% coverage)\n"
|
| 366 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
prompt = f"""
|
| 368 |
+
{context}
|
| 369 |
+
|
| 370 |
+
As Nilsen Service & Consulting's AI system, demonstrate the value we've delivered to management.
|
| 371 |
+
|
| 372 |
+
**OUR TECHNICAL ACHIEVEMENTS**
|
| 373 |
+
Highlight what our team has successfully implemented and delivered.
|
| 374 |
|
| 375 |
+
**BUSINESS VALUE PROVIDED**
|
| 376 |
+
Explain the operational improvements and insights our monitoring system enables.
|
| 377 |
|
| 378 |
+
**SYSTEM CAPABILITIES**
|
| 379 |
+
Showcase the advanced features and reliability of our solution.
|
| 380 |
|
| 381 |
+
Focus on our professional competence, technical excellence, and delivered value. Show why our monitoring system is essential for their operations. Maximum 300 words.
|
| 382 |
+
"""
|
| 383 |
|
| 384 |
+
response = model.generate_content(prompt)
|
| 385 |
+
return response.text
|
| 386 |
+
|
| 387 |
+
except Exception as e:
|
| 388 |
+
return f"AI analysis error: {str(e)}"
|
| 389 |
+
|
| 390 |
+
def query_ai(model, stats, question, df=None):
|
| 391 |
+
"""AI responses showcasing our system capabilities"""
|
| 392 |
+
if not model:
|
| 393 |
+
return "AI assistant not available"
|
| 394 |
+
|
| 395 |
+
try:
|
| 396 |
+
context = f"""
|
| 397 |
+
NILSEN SERVICE & CONSULTING - PRODUCTION MONITORING SYSTEM
|
| 398 |
|
| 399 |
+
Our System Performance:
|
| 400 |
+
- Total Production Monitored: {stats['_total_']['total']:,.0f} kg
|
| 401 |
+
- Daily Processing Capacity: {stats['_total_']['daily_avg']:,.0f} kg average
|
| 402 |
+
- Operational Coverage: {stats['_total_']['work_days']} work days
|
| 403 |
+
- Data Accuracy: Professional-grade monitoring with real-time analytics
|
| 404 |
|
| 405 |
+
Our Technical Solutions:
|
| 406 |
+
"""
|
| 407 |
+
materials = [k for k in stats.keys() if k != '_total_']
|
| 408 |
+
for material in materials:
|
| 409 |
+
info = stats[material]
|
| 410 |
+
context += f"- {material.title()}: {info['percentage']:.1f}% production coverage\n"
|
| 411 |
+
|
| 412 |
+
prompt = f"""
|
| 413 |
+
{context}
|
| 414 |
|
| 415 |
+
Question: {question}
|
|
|
|
| 416 |
|
| 417 |
+
Respond as Nilsen Service & Consulting's AI system. Emphasize our technical expertise, system reliability, and the professional insights we provide. Show how our monitoring solution delivers superior operational intelligence. Keep under 120 words.
|
| 418 |
"""
|
| 419 |
+
|
| 420 |
response = model.generate_content(prompt)
|
| 421 |
return response.text
|
| 422 |
+
|
| 423 |
except Exception as e:
|
| 424 |
+
return f"System response unavailable: {str(e)}"
|
| 425 |
+
|
| 426 |
+
def render_ai_section(df, stats, model):
|
| 427 |
+
"""Showcase our AI capabilities and technical achievements"""
|
| 428 |
+
|
| 429 |
+
if model:
|
| 430 |
+
st.markdown('<div class="section-header">π Our AI-Powered Solution</div>', unsafe_allow_html=True)
|
| 431 |
+
|
| 432 |
+
# Demonstrate our capabilities
|
| 433 |
+
st.markdown("### π― **Nilsen Service & Consulting - Technical Achievements**")
|
| 434 |
+
outliers = detect_outliers(df)
|
| 435 |
+
with st.spinner("Demonstrating our AI capabilities..."):
|
| 436 |
+
ai_summary = generate_ai_summary(model, df, stats, outliers)
|
| 437 |
+
st.success(ai_summary)
|
| 438 |
+
|
| 439 |
+
# Show our system's intelligence
|
| 440 |
+
st.markdown("### π§ **Experience Our Advanced Analytics**")
|
| 441 |
+
st.markdown("*See how our AI system provides professional insights for your operations:*")
|
| 442 |
+
|
| 443 |
+
demo_questions = [
|
| 444 |
+
"How does our monitoring system ensure production quality?",
|
| 445 |
+
"What operational advantages does our platform provide?",
|
| 446 |
+
"How does our AI deliver cost savings and efficiency gains?"
|
| 447 |
+
]
|
| 448 |
+
|
| 449 |
+
cols = st.columns(len(demo_questions))
|
| 450 |
+
for i, question in enumerate(demo_questions):
|
| 451 |
+
with cols[i]:
|
| 452 |
+
if st.button(f"βΆοΈ {question}", key=f"demo_q_{i}"):
|
| 453 |
+
with st.spinner("Our AI analyzing..."):
|
| 454 |
+
answer = query_ai(model, stats, question, df)
|
| 455 |
+
st.info(f"**Nilsen AI Response:** {answer}")
|
| 456 |
+
|
| 457 |
+
# Interactive demonstration
|
| 458 |
+
st.markdown("### πΌ **Test Our AI Intelligence**")
|
| 459 |
+
col1, col2 = st.columns([3, 1])
|
| 460 |
+
|
| 461 |
+
with col1:
|
| 462 |
+
custom_question = st.text_input("Ask our AI system about production management:",
|
| 463 |
+
placeholder="e.g., 'How can your system improve our operational efficiency?'",
|
| 464 |
+
key="demo_question")
|
| 465 |
+
with col2:
|
| 466 |
+
st.markdown("<br>", unsafe_allow_html=True) # Add spacing
|
| 467 |
+
if st.button("π **See Our AI in Action**", key="demo_btn", type="primary"):
|
| 468 |
+
if custom_question:
|
| 469 |
+
with st.spinner("Nilsen AI processing..."):
|
| 470 |
+
answer = query_ai(model, stats, custom_question, df)
|
| 471 |
+
st.success(f"**Your Question:** {custom_question}")
|
| 472 |
+
st.info(f"**Our AI Solution:** {answer}")
|
| 473 |
+
else:
|
| 474 |
+
st.warning("Please enter a question to see our AI capabilities")
|
| 475 |
+
|
| 476 |
+
# Value proposition
|
| 477 |
+
st.markdown("---")
|
| 478 |
+
st.markdown("""
|
| 479 |
+
<div style="background: linear-gradient(135deg, #1E40AF15, #05966925); padding: 1rem; border-radius: 8px; border: 1px solid #1E40AF;">
|
| 480 |
+
<h4>π Why Choose Nilsen Service & Consulting?</h4>
|
| 481 |
+
<ul>
|
| 482 |
+
<li>β
<strong>Advanced AI Integration:</strong> Cutting-edge analytics for your operations</li>
|
| 483 |
+
<li>β
<strong>Professional Reliability:</strong> Enterprise-grade monitoring systems</li>
|
| 484 |
+
<li>β
<strong>Proven Results:</strong> Data-driven insights that deliver ROI</li>
|
| 485 |
+
<li>β
<strong>Complete Solution:</strong> From data processing to actionable recommendations</li>
|
| 486 |
+
</ul>
|
| 487 |
+
</div>
|
| 488 |
+
""", unsafe_allow_html=True)
|
| 489 |
+
|
| 490 |
+
else:
|
| 491 |
+
st.markdown('<div class="section-header">π Our AI-Powered Solution</div>', unsafe_allow_html=True)
|
| 492 |
+
st.error("β οΈ AI demonstration requires API configuration")
|
| 493 |
+
st.info("π‘ **Our AI system provides:** Advanced analytics, predictive insights, automated reporting, and intelligent recommendations for production optimization.")
|
| 494 |
+
|
| 495 |
|
| 496 |
def query_ai(model, stats, question, df=None):
|
| 497 |
if not model:
|