shrudhit commited on
Commit
fd1bd4e
·
verified ·
1 Parent(s): abeb336

Update app.py

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