Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,12 +12,12 @@ def upload_csv(file):
|
|
| 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
|
| 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
|
| 21 |
|
| 22 |
def filter_and_plot(selected_lab, selected_type):
|
| 23 |
global df
|
|
@@ -29,11 +29,11 @@ def filter_and_plot(selected_lab, selected_type):
|
|
| 29 |
if selected_type != "All":
|
| 30 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 31 |
|
| 32 |
-
#
|
| 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
|
| 37 |
plt.xlabel("Status")
|
| 38 |
plt.ylabel("Count")
|
| 39 |
|
|
@@ -74,14 +74,16 @@ with gr.Blocks() as demo:
|
|
| 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 |
-
|
|
|
|
|
|
|
| 81 |
download_btn = gr.Button("Download PDF Summary")
|
| 82 |
-
error_box = gr.Textbox(visible=False)
|
| 83 |
|
| 84 |
-
|
|
|
|
| 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())
|
|
|
|
| 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 gr.update(choices=["All"]), gr.update(choices=["All"]), "❌ 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 gr.update(choices=labs, value="All"), gr.update(choices=types, value="All"), ""
|
| 19 |
except Exception as e:
|
| 20 |
+
return gr.update(choices=["All"]), gr.update(choices=["All"]), f"❌ Failed to load CSV: {str(e)}"
|
| 21 |
|
| 22 |
def filter_and_plot(selected_lab, selected_type):
|
| 23 |
global df
|
|
|
|
| 29 |
if selected_type != "All":
|
| 30 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
| 31 |
|
| 32 |
+
# Plotting
|
| 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("Device Status Overview")
|
| 37 |
plt.xlabel("Status")
|
| 38 |
plt.ylabel("Count")
|
| 39 |
|
|
|
|
| 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"], value="All")
|
| 78 |
+
type_dropdown = gr.Dropdown(label="Select Equipment Type", choices=["All"], value="All")
|
| 79 |
|
| 80 |
+
error_box = gr.Textbox(label="Message", visible=True, interactive=False)
|
| 81 |
+
|
| 82 |
+
plot_output = gr.Image(label="Device Status Plot")
|
| 83 |
download_btn = gr.Button("Download PDF Summary")
|
|
|
|
| 84 |
|
| 85 |
+
# Hook up event logic
|
| 86 |
+
csv_input.change(fn=upload_csv, inputs=csv_input, outputs=[lab_dropdown, type_dropdown, error_box])
|
| 87 |
lab_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 88 |
type_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 89 |
download_btn.click(fn=download_pdf, inputs=[lab_dropdown, type_dropdown], outputs=gr.File())
|