Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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."""
|
| 8 |
+
unique_names = series.dropna().unique()
|
| 9 |
+
name_mapping = {}
|
| 10 |
+
|
| 11 |
+
for name in unique_names:
|
| 12 |
+
matches = process.extractBests(
|
| 13 |
+
name, unique_names,
|
| 14 |
+
scorer=fuzz.token_sort_ratio,
|
| 15 |
+
score_cutoff=threshold
|
| 16 |
+
)
|
| 17 |
+
if matches:
|
| 18 |
+
best_match = max(matches, key=lambda x: (x[1], list(series).count(x[0])))
|
| 19 |
+
name_mapping[name] = best_match[0]
|
| 20 |
+
|
| 21 |
+
return series.replace(name_mapping)
|
| 22 |
+
|
| 23 |
+
def process_file(input_file):
|
| 24 |
+
"""Process uploaded Excel file and return output"""
|
| 25 |
+
try:
|
| 26 |
+
# Read input file
|
| 27 |
+
input_df = pd.read_excel(input_file.name, header=1)
|
| 28 |
+
|
| 29 |
+
# Store original date order
|
| 30 |
+
date_columns = input_df.columns[1:].tolist()
|
| 31 |
+
|
| 32 |
+
# Melt to long format
|
| 33 |
+
df_long = input_df.melt(
|
| 34 |
+
id_vars=[input_df.columns[0]],
|
| 35 |
+
var_name='DATE',
|
| 36 |
+
value_name='CHATTER'
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Force date order
|
| 40 |
+
df_long['DATE'] = pd.Categorical(
|
| 41 |
+
df_long['DATE'],
|
| 42 |
+
categories=date_columns,
|
| 43 |
+
ordered=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Clean names
|
| 47 |
+
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
| 48 |
+
|
| 49 |
+
# Group and pivot
|
| 50 |
+
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 51 |
+
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
| 52 |
+
|
| 53 |
+
pivoted = grouped.pivot(index='CHATTER', columns='DATE', values=input_df.columns[0])
|
| 54 |
+
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 55 |
+
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
| 56 |
+
|
| 57 |
+
# Create temp file for output
|
| 58 |
+
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 59 |
+
final_df.to_excel(tmp.name, sheet_name='Schedule')
|
| 60 |
+
return final_df, tmp.name
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Error processing file: {str(e)}", None
|
| 64 |
+
|
| 65 |
+
# Gradio interface
|
| 66 |
+
with gr.Blocks(title="Schedule Processor") as demo:
|
| 67 |
+
gr.Markdown("# 📅 Schedule Processor")
|
| 68 |
+
gr.Markdown("Upload your schedule Excel file and download the formatted version")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
input_file = gr.File(label="Upload Schedule File", type="file")
|
| 72 |
+
output_file = gr.File(label="Download Processed File", visible=False)
|
| 73 |
+
|
| 74 |
+
output_table = gr.Dataframe(label="Preview", headers=[])
|
| 75 |
+
|
| 76 |
+
btn = gr.Button("Process File")
|
| 77 |
+
|
| 78 |
+
def process_and_show(file):
|
| 79 |
+
df, out_path = process_file(file)
|
| 80 |
+
if out_path:
|
| 81 |
+
return df, gr.File.update(value=out_path, visible=True), gr.Dataframe.update(value=df)
|
| 82 |
+
return df, gr.File.update(visible=False), gr.Dataframe.update(visible=False)
|
| 83 |
+
|
| 84 |
+
btn.click(
|
| 85 |
+
process_and_show,
|
| 86 |
+
inputs=input_file,
|
| 87 |
+
outputs=[output_table, output_file, output_table]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|