kreemyyyy commited on
Commit
b91456d
·
verified ·
1 Parent(s): 2654a8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -40
app.py CHANGED
@@ -1,11 +1,8 @@
1
  import pandas as pd
2
- import openpyxl # ensure XLSX engine is available
3
  import gradio as gr
4
- import uuid
5
- import os
6
- import tempfile
7
-
8
- # 7-Day Schedule Converter with explicit iteration and header fill and download support
9
 
10
  def convert_schedule(file_path, direction):
11
  try:
@@ -49,7 +46,6 @@ def convert_schedule(file_path, direction):
49
  for model in df.index.astype(str):
50
  for day in day_cols:
51
  cell = df.at[model, day]
52
- # Handle NaN values properly
53
  if pd.isna(cell):
54
  continue
55
  for texter in str(cell).split(','):
@@ -59,7 +55,7 @@ def convert_schedule(file_path, direction):
59
  assignments.setdefault(texter, {d: [] for d in day_cols})
60
  assignments[texter][day].append(model)
61
 
62
- if not assignments: # Handle empty assignments
63
  result = pd.DataFrame(columns=day_cols)
64
  first_col_name = 'Texter'
65
  else:
@@ -75,7 +71,6 @@ def convert_schedule(file_path, direction):
75
  for texter in df.index.astype(str):
76
  for day in day_cols:
77
  cell = df.at[texter, day]
78
- # Handle NaN values properly
79
  if pd.isna(cell):
80
  continue
81
  for model in str(cell).split(','):
@@ -85,7 +80,7 @@ def convert_schedule(file_path, direction):
85
  assignments.setdefault(model, {d: [] for d in day_cols})
86
  assignments[model][day].append(texter)
87
 
88
- if not assignments: # Handle empty assignments
89
  result = pd.DataFrame(columns=day_cols)
90
  first_col_name = 'Model'
91
  else:
@@ -104,49 +99,35 @@ def convert_schedule(file_path, direction):
104
  # For display, include index as a column
105
  display_df = result.reset_index().rename(columns={'index': first_col_name})
106
 
107
- # 6. Save to Excel for download - use current directory for Gradio compatibility
108
- output_filename = f"converted_{uuid.uuid4().hex}.xlsx"
109
-
110
- # Clean the DataFrame before saving
111
- result_clean = result.copy()
112
-
113
- # Replace any problematic values
114
- result_clean = result_clean.fillna('OFF') # Fill NaN with 'OFF'
115
 
116
- # Ensure all values are strings to avoid Excel compatibility issues
117
  for col in result_clean.columns:
118
  result_clean[col] = result_clean[col].astype(str)
119
 
120
- # Save with better error handling
121
- try:
122
- # Use simple to_excel with minimal options for better compatibility
123
- result_clean.to_excel(output_filename, index=True, engine='openpyxl')
124
-
125
- # Return the filename directly for Gradio to handle
126
- if os.path.exists(output_filename):
127
- print(f"File created successfully: {output_filename}")
128
- else:
129
- raise FileNotFoundError("File creation failed")
130
-
131
- except Exception as save_error:
132
- print(f"Excel save error: {save_error}")
133
- # Return None if save fails
134
- return display_df, None
135
 
136
- # Return both DataFrame and download path
137
- return display_df, output_filename
138
 
139
  except Exception as e:
140
- # Return error information
141
  error_df = pd.DataFrame({'Error': [f"Error processing file: {str(e)}"]})
142
  return error_df, None
143
 
144
- # Build Gradio interface and launch immediately for Hugging Face Spaces
145
  iface = gr.Interface(
146
  fn=convert_schedule,
147
  inputs=[
148
  gr.File(label='Upload Weekly Schedule (.xlsx)', file_count='single', type='filepath'),
149
- gr.Radio(['A to B', 'B to A'], label='Convert Direction', value='A to B') # Added default value
150
  ],
151
  outputs=[
152
  gr.Dataframe(label='Converted Schedule'),
@@ -160,6 +141,5 @@ iface = gr.Interface(
160
  flagging_mode='never'
161
  )
162
 
163
- # Launch on 0.0.0.0:7860 for Spaces
164
  if __name__ == "__main__":
165
  iface.launch(server_name='0.0.0.0', server_port=7860)
 
1
  import pandas as pd
2
+ import openpyxl
3
  import gradio as gr
4
+ import io
5
+ import base64
 
 
 
6
 
7
  def convert_schedule(file_path, direction):
8
  try:
 
46
  for model in df.index.astype(str):
47
  for day in day_cols:
48
  cell = df.at[model, day]
 
49
  if pd.isna(cell):
50
  continue
51
  for texter in str(cell).split(','):
 
55
  assignments.setdefault(texter, {d: [] for d in day_cols})
56
  assignments[texter][day].append(model)
57
 
58
+ if not assignments:
59
  result = pd.DataFrame(columns=day_cols)
60
  first_col_name = 'Texter'
61
  else:
 
71
  for texter in df.index.astype(str):
72
  for day in day_cols:
73
  cell = df.at[texter, day]
 
74
  if pd.isna(cell):
75
  continue
76
  for model in str(cell).split(','):
 
80
  assignments.setdefault(model, {d: [] for d in day_cols})
81
  assignments[model][day].append(texter)
82
 
83
+ if not assignments:
84
  result = pd.DataFrame(columns=day_cols)
85
  first_col_name = 'Model'
86
  else:
 
99
  # For display, include index as a column
100
  display_df = result.reset_index().rename(columns={'index': first_col_name})
101
 
102
+ # 6. Create Excel file in memory
103
+ output_buffer = io.BytesIO()
104
+ result_clean = result.copy().fillna('OFF')
 
 
 
 
 
105
 
106
+ # Ensure all values are strings
107
  for col in result_clean.columns:
108
  result_clean[col] = result_clean[col].astype(str)
109
 
110
+ # Write to buffer
111
+ result_clean.to_excel(output_buffer, index=True, engine='openpyxl')
112
+ output_buffer.seek(0)
113
+
114
+ # Save buffer content to a temporary file that Gradio can serve
115
+ temp_filename = "converted_schedule.xlsx"
116
+ with open(temp_filename, 'wb') as f:
117
+ f.write(output_buffer.getvalue())
 
 
 
 
 
 
 
118
 
119
+ return display_df, temp_filename
 
120
 
121
  except Exception as e:
 
122
  error_df = pd.DataFrame({'Error': [f"Error processing file: {str(e)}"]})
123
  return error_df, None
124
 
125
+ # Gradio interface
126
  iface = gr.Interface(
127
  fn=convert_schedule,
128
  inputs=[
129
  gr.File(label='Upload Weekly Schedule (.xlsx)', file_count='single', type='filepath'),
130
+ gr.Radio(['A to B', 'B to A'], label='Convert Direction', value='A to B')
131
  ],
132
  outputs=[
133
  gr.Dataframe(label='Converted Schedule'),
 
141
  flagging_mode='never'
142
  )
143
 
 
144
  if __name__ == "__main__":
145
  iface.launch(server_name='0.0.0.0', server_port=7860)