saikumar27 commited on
Commit
aa0f37c
·
verified ·
1 Parent(s): a6c3352

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -5,24 +5,31 @@ import pandas as pd
5
  def load_data(uploaded_file):
6
  file_extension = uploaded_file.name.split(".")[-1].lower()
7
 
8
- if file_extension == "csv":
9
- return pd.read_csv(uploaded_file)
10
- elif file_extension in ["xls", "xlsx"]:
11
- return pd.read_excel(uploaded_file)
12
- elif file_extension == "xml":
13
- return pd.read_xml(uploaded_file)
14
- elif file_extension == "txt":
15
- return pd.read_csv(uploaded_file, delimiter="\t", header=None)
16
- else:
17
- st.error("Unsupported file format!")
 
 
 
 
18
  return None
19
 
20
  # Streamlit UI
21
  st.title("ML Model File Uploader")
 
22
  uploaded_file = st.file_uploader("Upload a dataset", type=["csv", "xls", "xlsx", "xml", "txt"])
23
 
24
  if uploaded_file is not None:
25
  df = load_data(uploaded_file)
26
- if df is not None:
27
  st.write("### Data Preview")
28
  st.dataframe(df.head())
 
 
 
5
  def load_data(uploaded_file):
6
  file_extension = uploaded_file.name.split(".")[-1].lower()
7
 
8
+ try:
9
+ if file_extension == "csv":
10
+ return pd.read_csv(uploaded_file)
11
+ elif file_extension in ["xls", "xlsx"]:
12
+ return pd.read_excel(uploaded_file, engine="openpyxl")
13
+ elif file_extension == "xml":
14
+ return pd.read_xml(uploaded_file)
15
+ elif file_extension == "txt":
16
+ return pd.read_csv(uploaded_file, delimiter="\t", header=None, error_bad_lines=False)
17
+ else:
18
+ st.error("Unsupported file format!")
19
+ return None
20
+ except Exception as e:
21
+ st.error(f"Error reading file: {e}")
22
  return None
23
 
24
  # Streamlit UI
25
  st.title("ML Model File Uploader")
26
+ st.write("Upload a dataset in CSV, Excel, XML, or TXT format.")
27
  uploaded_file = st.file_uploader("Upload a dataset", type=["csv", "xls", "xlsx", "xml", "txt"])
28
 
29
  if uploaded_file is not None:
30
  df = load_data(uploaded_file)
31
+ if df is not None and not df.empty:
32
  st.write("### Data Preview")
33
  st.dataframe(df.head())
34
+ else:
35
+ st.warning("The uploaded file is empty or could not be read.")