Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,34 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# βββ
|
| 6 |
-
import gradio_client.utils as
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
_orig_get_type = client_utils.get_type
|
| 9 |
def safe_get_type(schema, *args, **kwargs):
|
| 10 |
-
# if a JSONβschema node is literally True/False, just treat it as a generic dict
|
| 11 |
if isinstance(schema, bool):
|
| 12 |
return "dict"
|
| 13 |
return _orig_get_type(schema, *args, **kwargs)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
|
|
|
|
| 18 |
import pandas as pd
|
| 19 |
from fuzzywuzzy import process, fuzz
|
| 20 |
import tempfile
|
|
@@ -22,6 +36,7 @@ import os
|
|
| 22 |
from openpyxl import load_workbook
|
| 23 |
from openpyxl.styles import Alignment
|
| 24 |
|
|
|
|
| 25 |
def auto_correct_names(series, threshold=90):
|
| 26 |
"""Auto-correct typos in chatter names using fuzzy matching."""
|
| 27 |
unique_names = series.dropna().unique()
|
|
@@ -39,6 +54,7 @@ def auto_correct_names(series, threshold=90):
|
|
| 39 |
|
| 40 |
return series.replace(name_mapping)
|
| 41 |
|
|
|
|
| 42 |
def adjust_excel_formatting(file_path):
|
| 43 |
"""Adjust column widths and enable text wrapping in Excel file."""
|
| 44 |
wb = load_workbook(file_path)
|
|
@@ -55,88 +71,63 @@ def adjust_excel_formatting(file_path):
|
|
| 55 |
|
| 56 |
wb.save(file_path)
|
| 57 |
|
|
|
|
| 58 |
def process_file(input_file):
|
| 59 |
"""Process uploaded Excel file and return output"""
|
| 60 |
try:
|
| 61 |
-
# Read input file
|
| 62 |
input_df = pd.read_excel(input_file, header=1)
|
| 63 |
-
|
| 64 |
-
# Store original date order
|
| 65 |
date_columns = input_df.columns[1:].tolist()
|
| 66 |
-
|
| 67 |
-
# Melt to long format
|
| 68 |
df_long = input_df.melt(
|
| 69 |
-
id_vars=[input_df.columns[0]],
|
| 70 |
-
var_name='DATE',
|
| 71 |
value_name='CHATTER'
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
# Force date order
|
| 75 |
df_long['DATE'] = pd.Categorical(
|
| 76 |
-
df_long['DATE'],
|
| 77 |
categories=date_columns,
|
| 78 |
ordered=True
|
| 79 |
)
|
| 80 |
-
|
| 81 |
-
# Clean names
|
| 82 |
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
| 83 |
-
|
| 84 |
-
# Group and pivot
|
| 85 |
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 86 |
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
| 87 |
-
|
| 88 |
pivoted = grouped.pivot(index='CHATTER', columns='DATE', values=input_df.columns[0])
|
| 89 |
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 90 |
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
| 91 |
-
|
| 92 |
-
# Reset index to show chatter names in preview
|
| 93 |
final_df = final_df.reset_index()
|
| 94 |
-
|
| 95 |
-
# Create temp file with original filename + "_processed"
|
| 96 |
original_filename = os.path.basename(input_file)
|
| 97 |
name_part, ext_part = os.path.splitext(original_filename)
|
| 98 |
processed_filename = f"{name_part}_processed{ext_part}"
|
| 99 |
temp_file_path = os.path.join(tempfile.gettempdir(), processed_filename)
|
| 100 |
-
|
| 101 |
final_df.to_excel(temp_file_path, index=False, sheet_name='Schedule')
|
| 102 |
-
|
| 103 |
-
# Adjust formatting to ensure everything fits within cells
|
| 104 |
adjust_excel_formatting(temp_file_path)
|
| 105 |
-
|
| 106 |
return final_df, temp_file_path
|
| 107 |
-
|
| 108 |
except Exception as e:
|
| 109 |
error_df = pd.DataFrame({"Error": [f"β οΈ {str(e)}"]})
|
| 110 |
return error_df, None
|
| 111 |
|
| 112 |
-
|
| 113 |
def download_file(out_path):
|
| 114 |
"""Return the processed file path for download"""
|
| 115 |
return out_path
|
| 116 |
|
| 117 |
-
# Gradio interface
|
| 118 |
with gr.Blocks(title="Schedule Processor") as demo:
|
| 119 |
gr.Markdown("# π
Schedule Processor")
|
| 120 |
gr.Markdown("Upload your schedule Excel file and download the formatted version")
|
| 121 |
-
|
| 122 |
with gr.Row():
|
| 123 |
input_file = gr.File(label="Upload Schedule File", type="filepath")
|
| 124 |
-
|
| 125 |
with gr.Row():
|
| 126 |
process_btn = gr.Button("Process File", variant="primary")
|
| 127 |
reset_btn = gr.Button("Upload New File")
|
| 128 |
-
|
| 129 |
output_table = gr.Dataframe(label="Preview", wrap=True)
|
| 130 |
download_button = gr.Button("Download Processed File", visible=False)
|
| 131 |
temp_file_path = gr.State(value=None)
|
| 132 |
|
| 133 |
def reset_components():
|
| 134 |
-
"""Reset all components to initial state"""
|
| 135 |
return [
|
| 136 |
-
None,
|
| 137 |
-
pd.DataFrame(),
|
| 138 |
-
None,
|
| 139 |
-
gr.update(visible=False)
|
| 140 |
]
|
| 141 |
|
| 142 |
def process_and_show(file):
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
+
# βββ monkey-patch Gradio to swallow API-info errors βββ
|
| 4 |
+
import gradio as _gr
|
| 5 |
+
|
| 6 |
+
_orig_get_api_info = _gr.Blocks.get_api_info
|
| 7 |
+
|
| 8 |
+
def _safe_get_api_info(self):
|
| 9 |
+
try:
|
| 10 |
+
return _orig_get_api_info(self)
|
| 11 |
+
except Exception:
|
| 12 |
+
# return empty spec if parsing fails
|
| 13 |
+
return {}
|
| 14 |
+
|
| 15 |
+
_gr.Blocks.get_api_info = _safe_get_api_info
|
| 16 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
|
| 18 |
+
# βββ optional: patch client_utils.get_type to handle boolean schemas βββ
|
| 19 |
+
import gradio_client.utils as _client_utils
|
| 20 |
+
|
| 21 |
+
_orig_get_type = _client_utils.get_type
|
| 22 |
|
|
|
|
| 23 |
def safe_get_type(schema, *args, **kwargs):
|
|
|
|
| 24 |
if isinstance(schema, bool):
|
| 25 |
return "dict"
|
| 26 |
return _orig_get_type(schema, *args, **kwargs)
|
| 27 |
|
| 28 |
+
_client_utils.get_type = safe_get_type
|
| 29 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
|
| 31 |
+
import gradio as gr
|
| 32 |
import pandas as pd
|
| 33 |
from fuzzywuzzy import process, fuzz
|
| 34 |
import tempfile
|
|
|
|
| 36 |
from openpyxl import load_workbook
|
| 37 |
from openpyxl.styles import Alignment
|
| 38 |
|
| 39 |
+
|
| 40 |
def auto_correct_names(series, threshold=90):
|
| 41 |
"""Auto-correct typos in chatter names using fuzzy matching."""
|
| 42 |
unique_names = series.dropna().unique()
|
|
|
|
| 54 |
|
| 55 |
return series.replace(name_mapping)
|
| 56 |
|
| 57 |
+
|
| 58 |
def adjust_excel_formatting(file_path):
|
| 59 |
"""Adjust column widths and enable text wrapping in Excel file."""
|
| 60 |
wb = load_workbook(file_path)
|
|
|
|
| 71 |
|
| 72 |
wb.save(file_path)
|
| 73 |
|
| 74 |
+
|
| 75 |
def process_file(input_file):
|
| 76 |
"""Process uploaded Excel file and return output"""
|
| 77 |
try:
|
|
|
|
| 78 |
input_df = pd.read_excel(input_file, header=1)
|
|
|
|
|
|
|
| 79 |
date_columns = input_df.columns[1:].tolist()
|
|
|
|
|
|
|
| 80 |
df_long = input_df.melt(
|
| 81 |
+
id_vars=[input_df.columns[0]],
|
| 82 |
+
var_name='DATE',
|
| 83 |
value_name='CHATTER'
|
| 84 |
)
|
|
|
|
|
|
|
| 85 |
df_long['DATE'] = pd.Categorical(
|
| 86 |
+
df_long['DATE'],
|
| 87 |
categories=date_columns,
|
| 88 |
ordered=True
|
| 89 |
)
|
|
|
|
|
|
|
| 90 |
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
|
|
|
|
|
|
| 91 |
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 92 |
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
|
|
|
| 93 |
pivoted = grouped.pivot(index='CHATTER', columns='DATE', values=input_df.columns[0])
|
| 94 |
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 95 |
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
|
|
|
|
|
|
| 96 |
final_df = final_df.reset_index()
|
|
|
|
|
|
|
| 97 |
original_filename = os.path.basename(input_file)
|
| 98 |
name_part, ext_part = os.path.splitext(original_filename)
|
| 99 |
processed_filename = f"{name_part}_processed{ext_part}"
|
| 100 |
temp_file_path = os.path.join(tempfile.gettempdir(), processed_filename)
|
|
|
|
| 101 |
final_df.to_excel(temp_file_path, index=False, sheet_name='Schedule')
|
|
|
|
|
|
|
| 102 |
adjust_excel_formatting(temp_file_path)
|
|
|
|
| 103 |
return final_df, temp_file_path
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
error_df = pd.DataFrame({"Error": [f"β οΈ {str(e)}"]})
|
| 106 |
return error_df, None
|
| 107 |
|
| 108 |
+
|
| 109 |
def download_file(out_path):
|
| 110 |
"""Return the processed file path for download"""
|
| 111 |
return out_path
|
| 112 |
|
|
|
|
| 113 |
with gr.Blocks(title="Schedule Processor") as demo:
|
| 114 |
gr.Markdown("# π
Schedule Processor")
|
| 115 |
gr.Markdown("Upload your schedule Excel file and download the formatted version")
|
|
|
|
| 116 |
with gr.Row():
|
| 117 |
input_file = gr.File(label="Upload Schedule File", type="filepath")
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
process_btn = gr.Button("Process File", variant="primary")
|
| 120 |
reset_btn = gr.Button("Upload New File")
|
|
|
|
| 121 |
output_table = gr.Dataframe(label="Preview", wrap=True)
|
| 122 |
download_button = gr.Button("Download Processed File", visible=False)
|
| 123 |
temp_file_path = gr.State(value=None)
|
| 124 |
|
| 125 |
def reset_components():
|
|
|
|
| 126 |
return [
|
| 127 |
+
None,
|
| 128 |
+
pd.DataFrame(),
|
| 129 |
+
None,
|
| 130 |
+
gr.update(visible=False)
|
| 131 |
]
|
| 132 |
|
| 133 |
def process_and_show(file):
|