Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,150 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 4 |
import io
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
from reportlab.lib.pagesizes import letter
|
| 7 |
-
from reportlab.pdfgen import canvas
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# ----------------------------
|
| 14 |
-
def load_logs(file_obj):
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
else:
|
| 24 |
-
df = pd.DataFrame({
|
| 25 |
-
'DeviceID': ['D001', 'D002', 'D003'],
|
| 26 |
-
'Lab': ['Lab A', 'Lab B', 'Lab A'],
|
| 27 |
-
'Type': ['UV', 'Weight', 'Cell'],
|
| 28 |
-
'Timestamp': [pd.Timestamp('2025-06-01 09:00:00'),
|
| 29 |
-
pd.Timestamp('2025-06-01 10:00:00'),
|
| 30 |
-
pd.Timestamp('2025-06-01 11:00:00')],
|
| 31 |
-
'Status': ['OK', 'DOWN', 'OK'],
|
| 32 |
-
'UsageCount': [120, 85, 100]
|
| 33 |
-
})
|
| 34 |
-
return df
|
| 35 |
except Exception as e:
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
if selected_lab != "All":
|
| 43 |
-
|
| 44 |
if selected_type != "All":
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
pdf = canvas.Canvas(buffer, pagesize=letter)
|
| 70 |
-
width, height = letter
|
| 71 |
-
|
| 72 |
-
pdf.setFont("Helvetica-Bold", 14)
|
| 73 |
-
pdf.drawCentredString(width / 2, height - 40, "LabOps Dashboard Summary Report")
|
| 74 |
-
pdf.setFont("Helvetica", 10)
|
| 75 |
-
pdf.drawCentredString(width / 2, height - 60, f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
| 76 |
-
|
| 77 |
-
y = height - 100
|
| 78 |
-
pdf.setFont("Helvetica-Bold", 10)
|
| 79 |
-
pdf.drawString(50, y, "Lab")
|
| 80 |
-
pdf.drawString(150, y, "Device Type")
|
| 81 |
-
pdf.drawString(300, y, "OK")
|
| 82 |
-
pdf.drawString(350, y, "DOWN")
|
| 83 |
-
y -= 20
|
| 84 |
-
pdf.setFont("Helvetica", 10)
|
| 85 |
-
|
| 86 |
-
for (lab, dev_type), row in summary.iterrows():
|
| 87 |
-
pdf.drawString(50, y, str(lab))
|
| 88 |
-
pdf.drawString(150, y, str(dev_type))
|
| 89 |
-
pdf.drawString(300, y, str(int(row.get('OK', 0))))
|
| 90 |
-
pdf.drawString(350, y, str(int(row.get('DOWN', 0))))
|
| 91 |
-
y -= 20
|
| 92 |
-
if y < 50:
|
| 93 |
-
pdf.showPage()
|
| 94 |
-
y = height - 50
|
| 95 |
-
|
| 96 |
-
pdf.save()
|
| 97 |
-
buffer.seek(0)
|
| 98 |
-
return ("LabOps_Summary.pdf", buffer.read())
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
df = load_logs(file_obj)
|
| 106 |
-
labs = ["All"] + sorted(df['Lab'].unique().tolist())
|
| 107 |
-
types = ["All"] + sorted(df['Type'].unique().tolist())
|
| 108 |
-
return df, gr.Dropdown.update(choices=labs, value="All"), gr.Dropdown.update(choices=types, value="All"), gr.Textbox.update(visible=False)
|
| 109 |
-
except Exception as e:
|
| 110 |
-
return None, gr.Dropdown.update(choices=[], value=None), gr.Dropdown.update(choices=[], value=None), gr.Textbox.update(value=f"❌ Error: {e}", visible=True)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
except Exception as e:
|
| 116 |
-
return gr.update(visible=True), gr.Textbox.update(value=f"❌ Error: {e}", visible=True)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
except Exception as e:
|
| 123 |
-
return gr.File.update(visible=False), gr.Textbox.update(value=f"❌ Error: {e}", visible=True)
|
| 124 |
|
| 125 |
-
# ----------------------------
|
| 126 |
-
# Gradio Interface
|
| 127 |
-
# ----------------------------
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
-
gr.Markdown("
|
| 130 |
-
gr.Markdown("Upload lab device logs, filter by Lab and Equipment Type, visualize uptime/downtime & generate PDF reports.")
|
| 131 |
-
|
| 132 |
-
df_state = gr.State()
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
lab_dropdown = gr.Dropdown(label="Select Lab", choices=["All"])
|
| 137 |
type_dropdown = gr.Dropdown(label="Select Equipment Type", choices=["All"])
|
| 138 |
|
| 139 |
-
plot_output = gr.Plot
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
download_file = gr.File(label="Download PDF", visible=False)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
lab_dropdown.change(fn=
|
| 146 |
-
type_dropdown.change(fn=
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
import io
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
df = pd.DataFrame()
|
| 8 |
|
| 9 |
+
def upload_csv(file):
|
| 10 |
+
global df
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
df = pd.read_csv(file.name)
|
| 13 |
+
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
| 14 |
+
if not required_columns.issubset(df.columns):
|
| 15 |
+
return None, "CSV must contain columns: " + ", ".join(required_columns)
|
| 16 |
+
labs = ['All'] + sorted(df['Lab'].dropna().unique().tolist())
|
| 17 |
+
types = ['All'] + sorted(df['Type'].dropna().unique().tolist())
|
| 18 |
+
return (labs, types), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
+
return None, f"Failed to load CSV: {str(e)}"
|
| 21 |
|
| 22 |
+
def filter_and_plot(selected_lab, selected_type):
|
| 23 |
+
global df
|
| 24 |
+
if df.empty:
|
| 25 |
+
return None
|
| 26 |
+
filtered_df = df.copy()
|
| 27 |
if selected_lab != "All":
|
| 28 |
+
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 29 |
if selected_type != "All":
|
| 30 |
+
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 31 |
+
|
| 32 |
+
# Prepare plot
|
| 33 |
+
plt.figure(figsize=(8, 4))
|
| 34 |
+
status_counts = filtered_df["Status"].value_counts()
|
| 35 |
+
status_counts.plot(kind="bar", color=["green", "red"])
|
| 36 |
+
plt.title("Status Counts")
|
| 37 |
+
plt.xlabel("Status")
|
| 38 |
+
plt.ylabel("Count")
|
| 39 |
+
|
| 40 |
+
buf = io.BytesIO()
|
| 41 |
+
plt.savefig(buf, format="png")
|
| 42 |
+
buf.seek(0)
|
| 43 |
+
return buf
|
| 44 |
+
|
| 45 |
+
def download_pdf(selected_lab, selected_type):
|
| 46 |
+
global df
|
| 47 |
+
if df.empty:
|
| 48 |
+
return None
|
| 49 |
+
filtered_df = df.copy()
|
| 50 |
+
if selected_lab != "All":
|
| 51 |
+
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 52 |
+
if selected_type != "All":
|
| 53 |
+
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
pdf = FPDF()
|
| 56 |
+
pdf.add_page()
|
| 57 |
+
pdf.set_font("Arial", size=12)
|
| 58 |
+
pdf.cell(200, 10, txt="LabOps Summary Report", ln=True, align='C')
|
| 59 |
+
pdf.ln(10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
for index, row in filtered_df.iterrows():
|
| 62 |
+
line = f"{row['Timestamp']} | {row['DeviceID']} | {row['Lab']} | {row['Type']} | {row['Status']} | {row['UsageCount']}"
|
| 63 |
+
pdf.multi_cell(0, 10, txt=line)
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
output = io.BytesIO()
|
| 66 |
+
pdf.output(output)
|
| 67 |
+
output.seek(0)
|
| 68 |
+
return output
|
|
|
|
|
|
|
| 69 |
|
|
|
|
|
|
|
|
|
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("🧪 **LabOps Dashboard with Filters**\nUpload lab device logs, filter by Lab and Equipment Type, visualize uptime/downtime & generate PDF reports.")
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
+
csv_input = gr.File(label="Upload Log CSV", file_types=[".csv"])
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
lab_dropdown = gr.Dropdown(label="Select Lab", choices=["All"])
|
| 78 |
type_dropdown = gr.Dropdown(label="Select Equipment Type", choices=["All"])
|
| 79 |
|
| 80 |
+
plot_output = gr.Image(label="Plot")
|
| 81 |
+
download_btn = gr.Button("Download PDF Summary")
|
| 82 |
+
error_box = gr.Textbox(visible=False)
|
|
|
|
| 83 |
|
| 84 |
+
csv_input.change(fn=upload_csv, inputs=csv_input, outputs=[(lab_dropdown, type_dropdown), error_box])
|
| 85 |
+
lab_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 86 |
+
type_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 87 |
+
download_btn.click(fn=download_pdf, inputs=[lab_dropdown, type_dropdown], outputs=gr.File())
|
| 88 |
|
| 89 |
+
demo.launch()
|
|
|