codys12 commited on
Commit
46a6686
·
verified ·
1 Parent(s): 87f81fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -236,25 +236,30 @@ def process_woocommerce_data_in_memory(netcom_file):
236
 
237
  return output_buffer
238
 
239
- def process_file_and_return_csv(uploaded_file):
240
  """
241
- - Takes the uploaded file,
242
- - Processes it,
243
- - Writes the CSV to a temp file,
244
- - Returns that path for Gradio to provide as a downloadable file.
245
  """
246
- processed_csv_io = process_woocommerce_data_in_memory(uploaded_file)
247
-
248
- with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
249
- tmp.write(processed_csv_io.getvalue())
250
- tmp.flush() # ensure data is written to disk
251
- temp_path = tmp.name
 
 
252
 
253
- return temp_path
 
 
 
 
 
254
 
255
- app = gr.Interface(
256
- fn=process_file_and_return_csv,
257
- inputs=gr.File(label="Upload NetCom CSV", file_types=["text", "csv"]),
258
  outputs=gr.File(label="Download WooCommerce CSV"),
259
  title="NetCom to WooCommerce CSV Processor",
260
  description="Upload your NetCom Reseller Schedule CSV to generate the WooCommerce import-ready CSV."
@@ -262,4 +267,6 @@ app = gr.Interface(
262
 
263
  if __name__ == "__main__":
264
  openai_api_key = os.getenv("OPENAI_API_KEY")
265
- app.launch()
 
 
 
236
 
237
  return output_buffer
238
 
239
+ def process_woocommerce_data_in_memory(netcom_file):
240
  """
241
+ Reads the uploaded NetCom CSV file in-memory, processes it to the WooCommerce format,
242
+ and returns the resulting CSV as bytes, suitable for download.
 
 
243
  """
244
+ # [Keep all your existing processing code exactly the same until the end]
245
+
246
+ # 9. Convert to CSV (in memory)
247
+ output_buffer = BytesIO()
248
+ woo_final_df.to_csv(output_buffer, index=False, encoding='utf-8-sig')
249
+ output_buffer.seek(0)
250
+
251
+ return output_buffer
252
 
253
+ def process_file(uploaded_file):
254
+ """
255
+ Takes the uploaded file, processes it, and returns the CSV as a file-like object
256
+ """
257
+ processed_csv_io = process_woocommerce_data_in_memory(uploaded_file)
258
+ return processed_csv_io
259
 
260
+ interface = gr.Interface(
261
+ fn=process_file,
262
+ inputs=gr.File(label="Upload NetCom CSV", file_types=[".csv"]),
263
  outputs=gr.File(label="Download WooCommerce CSV"),
264
  title="NetCom to WooCommerce CSV Processor",
265
  description="Upload your NetCom Reseller Schedule CSV to generate the WooCommerce import-ready CSV."
 
267
 
268
  if __name__ == "__main__":
269
  openai_api_key = os.getenv("OPENAI_API_KEY")
270
+ if not openai_api_key:
271
+ print("Warning: OPENAI_API_KEY environment variable not set")
272
+ interface.launch()