Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
from fuzzywuzzy import process, fuzz
|
| 4 |
import tempfile
|
|
|
|
| 5 |
|
| 6 |
def auto_correct_names(series, threshold=90):
|
| 7 |
"""Auto-correct typos in chatter names using fuzzy matching."""
|
|
@@ -23,23 +24,30 @@ def auto_correct_names(series, threshold=90):
|
|
| 23 |
def process_file(input_file):
|
| 24 |
"""Process uploaded Excel file and return output"""
|
| 25 |
try:
|
|
|
|
| 26 |
input_df = pd.read_excel(input_file, header=1)
|
|
|
|
|
|
|
| 27 |
date_columns = input_df.columns[1:].tolist()
|
| 28 |
|
|
|
|
| 29 |
df_long = input_df.melt(
|
| 30 |
id_vars=[input_df.columns[0]],
|
| 31 |
var_name='DATE',
|
| 32 |
value_name='CHATTER'
|
| 33 |
)
|
| 34 |
|
|
|
|
| 35 |
df_long['DATE'] = pd.Categorical(
|
| 36 |
df_long['DATE'],
|
| 37 |
categories=date_columns,
|
| 38 |
ordered=True
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
| 42 |
|
|
|
|
| 43 |
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 44 |
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
| 45 |
|
|
@@ -47,14 +55,19 @@ def process_file(input_file):
|
|
| 47 |
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 48 |
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 51 |
-
final_df.to_excel(tmp.name, sheet_name='Schedule')
|
| 52 |
return final_df, tmp.name
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
error_df = pd.DataFrame({"Error": [f"⚠️ {str(e)}"]})
|
| 56 |
return error_df, None
|
| 57 |
|
|
|
|
| 58 |
with gr.Blocks(title="Schedule Processor") as demo:
|
| 59 |
gr.Markdown("# 📅 Schedule Processor")
|
| 60 |
gr.Markdown("Upload your schedule Excel file and download the formatted version")
|
|
@@ -63,15 +76,15 @@ with gr.Blocks(title="Schedule Processor") as demo:
|
|
| 63 |
input_file = gr.File(label="Upload Schedule File", type="filepath")
|
| 64 |
output_file = gr.File(label="Download Processed File", visible=False)
|
| 65 |
|
| 66 |
-
output_table = gr.Dataframe(label="Preview")
|
| 67 |
|
| 68 |
btn = gr.Button("Process File")
|
| 69 |
|
| 70 |
def process_and_show(file):
|
| 71 |
df, out_path = process_file(file)
|
| 72 |
if out_path:
|
| 73 |
-
return df, out_path
|
| 74 |
-
return df,
|
| 75 |
|
| 76 |
btn.click(
|
| 77 |
process_and_show,
|
|
@@ -80,4 +93,4 @@ with gr.Blocks(title="Schedule Processor") as demo:
|
|
| 80 |
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
demo.launch(share=True)
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from fuzzywuzzy import process, fuzz
|
| 4 |
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
def auto_correct_names(series, threshold=90):
|
| 8 |
"""Auto-correct typos in chatter names using fuzzy matching."""
|
|
|
|
| 24 |
def process_file(input_file):
|
| 25 |
"""Process uploaded Excel file and return output"""
|
| 26 |
try:
|
| 27 |
+
# Read input file
|
| 28 |
input_df = pd.read_excel(input_file, header=1)
|
| 29 |
+
|
| 30 |
+
# Store original date order
|
| 31 |
date_columns = input_df.columns[1:].tolist()
|
| 32 |
|
| 33 |
+
# Melt to long format
|
| 34 |
df_long = input_df.melt(
|
| 35 |
id_vars=[input_df.columns[0]],
|
| 36 |
var_name='DATE',
|
| 37 |
value_name='CHATTER'
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Force date order
|
| 41 |
df_long['DATE'] = pd.Categorical(
|
| 42 |
df_long['DATE'],
|
| 43 |
categories=date_columns,
|
| 44 |
ordered=True
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Clean names
|
| 48 |
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
| 49 |
|
| 50 |
+
# Group and pivot
|
| 51 |
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 52 |
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
| 53 |
|
|
|
|
| 55 |
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 56 |
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
| 57 |
|
| 58 |
+
# Reset index to show chatter names in preview
|
| 59 |
+
final_df = final_df.reset_index()
|
| 60 |
+
|
| 61 |
+
# Create temp file for output
|
| 62 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 63 |
+
final_df.to_excel(tmp.name, index=False, sheet_name='Schedule')
|
| 64 |
return final_df, tmp.name
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
error_df = pd.DataFrame({"Error": [f"⚠️ {str(e)}"]})
|
| 68 |
return error_df, None
|
| 69 |
|
| 70 |
+
# Gradio interface
|
| 71 |
with gr.Blocks(title="Schedule Processor") as demo:
|
| 72 |
gr.Markdown("# 📅 Schedule Processor")
|
| 73 |
gr.Markdown("Upload your schedule Excel file and download the formatted version")
|
|
|
|
| 76 |
input_file = gr.File(label="Upload Schedule File", type="filepath")
|
| 77 |
output_file = gr.File(label="Download Processed File", visible=False)
|
| 78 |
|
| 79 |
+
output_table = gr.Dataframe(label="Preview", wrap=True)
|
| 80 |
|
| 81 |
btn = gr.Button("Process File")
|
| 82 |
|
| 83 |
def process_and_show(file):
|
| 84 |
df, out_path = process_file(file)
|
| 85 |
if out_path:
|
| 86 |
+
return df, gr.File.update(value=out_path, visible=True)
|
| 87 |
+
return df, gr.File.update(visible=False)
|
| 88 |
|
| 89 |
btn.click(
|
| 90 |
process_and_show,
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
+
demo.launch(share=True)
|