shrudhit commited on
Commit
4e1d14f
·
verified ·
1 Parent(s): 1a7421c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -2,19 +2,19 @@ import gradio as gr
2
  import pandas as pd
3
  import tempfile
4
 
5
- def process_file(file):
6
 
7
  # read correct sheet
8
- df = pd.read_excel(file.name, sheet_name="Price Recommendations-Fill This", header=None)
9
 
10
  # delete first 4 rows
11
  df = df.iloc[4:].reset_index(drop=True)
12
 
13
- # ensure at least 10 columns
14
  while df.shape[1] < 10:
15
  df[df.shape[1]] = ""
16
 
17
- # convert column I and J to numbers
18
  df.iloc[:,8] = pd.to_numeric(df.iloc[:,8], errors="coerce").fillna(0)
19
  df.iloc[:,9] = pd.to_numeric(df.iloc[:,9], errors="coerce").fillna(0)
20
 
@@ -22,15 +22,15 @@ def process_file(file):
22
  df.iloc[:,7] = df.iloc[:,8] - df.iloc[:,9]
23
 
24
  # save CSV
25
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
26
- df.to_csv(tmp.name, index=False, header=False)
27
 
28
- return tmp.name
29
 
30
 
31
  demo = gr.Interface(
32
  fn=process_file,
33
- inputs=gr.File(label="Upload Excel File"),
34
  outputs=gr.File(label="Download CSV"),
35
  title="Excel → CSV Tool"
36
  )
 
2
  import pandas as pd
3
  import tempfile
4
 
5
+ def process_file(file_path):
6
 
7
  # read correct sheet
8
+ df = pd.read_excel(file_path, sheet_name="Price Recommendations-Fill This", header=None)
9
 
10
  # delete first 4 rows
11
  df = df.iloc[4:].reset_index(drop=True)
12
 
13
+ # ensure columns up to J exist
14
  while df.shape[1] < 10:
15
  df[df.shape[1]] = ""
16
 
17
+ # convert columns I and J to numeric
18
  df.iloc[:,8] = pd.to_numeric(df.iloc[:,8], errors="coerce").fillna(0)
19
  df.iloc[:,9] = pd.to_numeric(df.iloc[:,9], errors="coerce").fillna(0)
20
 
 
22
  df.iloc[:,7] = df.iloc[:,8] - df.iloc[:,9]
23
 
24
  # save CSV
25
+ output_path = "/tmp/output.csv"
26
+ df.to_csv(output_path, index=False, header=False)
27
 
28
+ return output_path
29
 
30
 
31
  demo = gr.Interface(
32
  fn=process_file,
33
+ inputs=gr.File(type="filepath", label="Upload Excel File"),
34
  outputs=gr.File(label="Download CSV"),
35
  title="Excel → CSV Tool"
36
  )