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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
from plotly.subplots import make_subplots
|
| 7 |
+
import warnings
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
warnings.filterwarnings('ignore')
|
| 12 |
+
|
| 13 |
+
def process_data(file):
|
| 14 |
+
"""Process uploaded CSV file and generate comprehensive analysis"""
|
| 15 |
+
if file is None:
|
| 16 |
+
return "Please upload a CSV file", None, None, None, None, None
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# Read the uploaded file
|
| 20 |
+
df = pd.read_csv(file.name, sep='\t')
|
| 21 |
+
|
| 22 |
+
# Data preprocessing
|
| 23 |
+
df['date'] = pd.to_datetime(df['date'], format='%m/%d/%Y')
|
| 24 |
+
if 'original_date' in df.columns:
|
| 25 |
+
df['original_date'] = pd.to_datetime(df['original_date'], format='%d/%m/%Y')
|
| 26 |
+
|
| 27 |
+
df['day_of_week'] = df['date'].dt.day_name()
|
| 28 |
+
df['week'] = df['date'].dt.isocalendar().week
|
| 29 |
+
df['month'] = df['date'].dt.month
|
| 30 |
+
df['is_weekend'] = df['day_of_week'].isin(['Saturday', 'Sunday'])
|
| 31 |
+
|
| 32 |
+
# Generate all analyses
|
| 33 |
+
summary_text = generate_summary(df)
|
| 34 |
+
overview_plot = create_overview_plot(df)
|
| 35 |
+
material_plot = create_material_analysis(df)
|
| 36 |
+
correlation_plot = create_correlation_analysis(df)
|
| 37 |
+
time_analysis_plot = create_time_analysis(df)
|
| 38 |
+
anomaly_report = detect_anomalies_report(df)
|
| 39 |
+
|
| 40 |
+
return summary_text, overview_plot, material_plot, correlation_plot, time_analysis_plot, anomaly_report
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error processing file: {str(e)}", None, None, None, None, None
|
| 44 |
+
|
| 45 |
+
def generate_summary(df):
|
| 46 |
+
"""Generate comprehensive summary statistics"""
|
| 47 |
+
total_production = df['weight_kg'].sum()
|
| 48 |
+
total_items = len(df)
|
| 49 |
+
daily_avg = df.groupby('date')['weight_kg'].sum().mean()
|
| 50 |
+
|
| 51 |
+
summary = f"""
|
| 52 |
+
# Production Data Analysis Report
|
| 53 |
+
Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 54 |
+
|
| 55 |
+
## Dataset Overview
|
| 56 |
+
- **Total Records**: {total_items:,}
|
| 57 |
+
- **Date Range**: {df['date'].min().strftime('%Y-%m-%d')} to {df['date'].max().strftime('%Y-%m-%d')}
|
| 58 |
+
- **Production Days**: {df['date'].nunique()}
|
| 59 |
+
- **Total Production**: {total_production:,.0f} kg
|
| 60 |
+
- **Daily Average**: {daily_avg:,.0f} kg
|
| 61 |
+
|
| 62 |
+
## Material Type Breakdown
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
for material in df['material_type'].unique():
|
| 66 |
+
mat_data = df[df['material_type'] == material]
|
| 67 |
+
mat_total = mat_data['weight_kg'].sum()
|
| 68 |
+
mat_pct = mat_total / total_production * 100
|
| 69 |
+
mat_count = len(mat_data)
|
| 70 |
+
summary += f"- **{material.title()}**: {mat_total:,.0f} kg ({mat_pct:.1f}%) - {mat_count:,} records\n"
|
| 71 |
+
|
| 72 |
+
# Shift analysis
|
| 73 |
+
if 'shift' in df.columns:
|
| 74 |
+
shift_data = df.groupby('shift')['weight_kg'].agg(['sum', 'mean', 'count'])
|
| 75 |
+
summary += f"\n## Shift Performance\n"
|
| 76 |
+
for shift in shift_data.index:
|
| 77 |
+
summary += f"- **Shift {shift}**: {shift_data.loc[shift, 'sum']:,.0f} kg total, {shift_data.loc[shift, 'mean']:.1f} kg avg, {shift_data.loc[shift, 'count']} records\n"
|
| 78 |
+
|
| 79 |
+
return summary
|
| 80 |
+
|
| 81 |
+
def create_overview_plot(df):
|
| 82 |
+
"""Create overall production trend plot"""
|
| 83 |
+
daily_total = df.groupby('date')['weight_kg'].sum().reset_index()
|
| 84 |
+
|
| 85 |
+
fig = px.line(daily_total, x='date', y='weight_kg',
|
| 86 |
+
title='Daily Production Trend',
|
| 87 |
+
labels={'weight_kg': 'Total Weight (kg)', 'date': 'Date'},
|
| 88 |
+
template='plotly_white')
|
| 89 |
+
|
| 90 |
+
fig.update_layout(height=400, showlegend=False)
|
| 91 |
+
return fig
|
| 92 |
+
|
| 93 |
+
def create_material_analysis(df):
|
| 94 |
+
"""Create material type comparison plots"""
|
| 95 |
+
# Daily production by material type
|
| 96 |
+
daily_by_material = df.groupby(['date', 'material_type'])['weight_kg'].sum().reset_index()
|
| 97 |
+
|
| 98 |
+
fig = px.line(daily_by_material, x='date', y='weight_kg', color='material_type',
|
| 99 |
+
title='Daily Production by Material Type',
|
| 100 |
+
labels={'weight_kg': 'Weight (kg)', 'date': 'Date'},
|
| 101 |
+
template='plotly_white')
|
| 102 |
+
|
| 103 |
+
fig.update_layout(height=400)
|
| 104 |
+
return fig
|
| 105 |
+
|
| 106 |
+
def create_correlation_analysis(df):
|
| 107 |
+
"""Create correlation matrix plot"""
|
| 108 |
+
daily_by_material = df.groupby(['date', 'material_type'])['weight_kg'].sum().unstack(fill_value=0)
|
| 109 |
+
|
| 110 |
+
if len(daily_by_material.columns) > 1:
|
| 111 |
+
correlation_matrix = daily_by_material.corr()
|
| 112 |
+
|
| 113 |
+
fig = px.imshow(correlation_matrix,
|
| 114 |
+
title='Material Type Correlation Matrix',
|
| 115 |
+
template='plotly_white',
|
| 116 |
+
color_continuous_scale='RdBu',
|
| 117 |
+
aspect='auto')
|
| 118 |
+
fig.update_layout(height=400)
|
| 119 |
+
return fig
|
| 120 |
+
else:
|
| 121 |
+
# Create empty plot if only one material type
|
| 122 |
+
fig = go.Figure()
|
| 123 |
+
fig.add_annotation(text="Only one material type - correlation analysis not applicable",
|
| 124 |
+
xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False)
|
| 125 |
+
fig.update_layout(title="Material Type Correlation Matrix", height=400)
|
| 126 |
+
return fig
|
| 127 |
+
|
| 128 |
+
def create_time_analysis(df):
|
| 129 |
+
"""Create time pattern analysis"""
|
| 130 |
+
# Weekly pattern
|
| 131 |
+
weekly_pattern = df.groupby(['day_of_week', 'material_type'])['weight_kg'].mean().reset_index()
|
| 132 |
+
|
| 133 |
+
# Define day order
|
| 134 |
+
day_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 135 |
+
weekly_pattern['day_of_week'] = pd.Categorical(weekly_pattern['day_of_week'], categories=day_order, ordered=True)
|
| 136 |
+
weekly_pattern = weekly_pattern.sort_values('day_of_week')
|
| 137 |
+
|
| 138 |
+
fig = px.bar(weekly_pattern, x='day_of_week', y='weight_kg', color='material_type',
|
| 139 |
+
title='Weekly Production Pattern (Average by Day)',
|
| 140 |
+
labels={'weight_kg': 'Average Weight (kg)', 'day_of_week': 'Day of Week'},
|
| 141 |
+
template='plotly_white')
|
| 142 |
+
|
| 143 |
+
fig.update_layout(height=400)
|
| 144 |
+
return fig
|
| 145 |
+
|
| 146 |
+
def detect_anomalies_report(df):
|
| 147 |
+
"""Generate anomaly detection report"""
|
| 148 |
+
def detect_outliers(data, column='weight_kg'):
|
| 149 |
+
Q1 = data[column].quantile(0.25)
|
| 150 |
+
Q3 = data[column].quantile(0.75)
|
| 151 |
+
IQR = Q3 - Q1
|
| 152 |
+
lower_bound = Q1 - 1.5 * IQR
|
| 153 |
+
upper_bound = Q3 + 1.5 * IQR
|
| 154 |
+
anomalies = data[(data[column] < lower_bound) | (data[column] > upper_bound)]
|
| 155 |
+
return anomalies, lower_bound, upper_bound
|
| 156 |
+
|
| 157 |
+
report = "# Anomaly Detection Report\n\n"
|
| 158 |
+
|
| 159 |
+
for material in df['material_type'].unique():
|
| 160 |
+
material_data = df[df['material_type'] == material]
|
| 161 |
+
anomalies, lower, upper = detect_outliers(material_data)
|
| 162 |
+
|
| 163 |
+
report += f"## {material.title()} Material\n"
|
| 164 |
+
report += f"- **Normal Range**: {lower:.1f} - {upper:.1f} kg\n"
|
| 165 |
+
report += f"- **Anomalies Detected**: {len(anomalies)}\n"
|
| 166 |
+
|
| 167 |
+
if len(anomalies) > 0:
|
| 168 |
+
report += f"- **Anomaly Dates**: {', '.join(anomalies['date'].dt.strftime('%Y-%m-%d').head(10).tolist())}\n"
|
| 169 |
+
if len(anomalies) > 10:
|
| 170 |
+
report += f" ... and {len(anomalies) - 10} more\n"
|
| 171 |
+
report += "\n"
|
| 172 |
+
|
| 173 |
+
return report
|
| 174 |
+
|
| 175 |
+
# Create Gradio interface
|
| 176 |
+
def create_interface():
|
| 177 |
+
with gr.Blocks(title="Production Data Analysis", theme=gr.themes.Soft()) as demo:
|
| 178 |
+
gr.Markdown("# 🏭 Production Data Analysis Dashboard")
|
| 179 |
+
gr.Markdown("Upload your production data CSV file to generate comprehensive analysis reports and visualizations.")
|
| 180 |
+
|
| 181 |
+
with gr.Row():
|
| 182 |
+
file_input = gr.File(
|
| 183 |
+
label="Upload CSV File",
|
| 184 |
+
file_types=[".csv"],
|
| 185 |
+
type="filepath"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
analyze_btn = gr.Button("Analyze Data", variant="primary", size="lg")
|
| 189 |
+
|
| 190 |
+
with gr.Row():
|
| 191 |
+
with gr.Column(scale=1):
|
| 192 |
+
summary_output = gr.Markdown(label="Summary Report")
|
| 193 |
+
anomaly_output = gr.Markdown(label="Anomaly Report")
|
| 194 |
+
|
| 195 |
+
with gr.Row():
|
| 196 |
+
with gr.Column():
|
| 197 |
+
overview_plot = gr.Plot(label="Production Overview")
|
| 198 |
+
correlation_plot = gr.Plot(label="Correlation Analysis")
|
| 199 |
+
with gr.Column():
|
| 200 |
+
material_plot = gr.Plot(label="Material Analysis")
|
| 201 |
+
time_plot = gr.Plot(label="Time Pattern Analysis")
|
| 202 |
+
|
| 203 |
+
analyze_btn.click(
|
| 204 |
+
fn=process_data,
|
| 205 |
+
inputs=[file_input],
|
| 206 |
+
outputs=[summary_output, overview_plot, material_plot, correlation_plot, time_plot, anomaly_output]
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
gr.Markdown("""
|
| 210 |
+
## Data Format Requirements
|
| 211 |
+
Your CSV file should contain the following columns:
|
| 212 |
+
- `date`: Date in MM/DD/YYYY format
|
| 213 |
+
- `weight_kg`: Production weight in kilograms
|
| 214 |
+
- `material_type`: Type of material (e.g., liquid, solid, waste_water)
|
| 215 |
+
- `shift`: Shift number (optional)
|
| 216 |
+
- `number`: Item number (optional)
|
| 217 |
+
|
| 218 |
+
The file should be tab-separated (TSV format with .csv extension).
|
| 219 |
+
""")
|
| 220 |
+
|
| 221 |
+
return demo
|
| 222 |
+
|
| 223 |
+
if __name__ == "__main__":
|
| 224 |
+
demo = create_interface()
|
| 225 |
+
demo.launch()
|