amiguel commited on
Commit
5011c91
Β·
verified Β·
1 Parent(s): 135171d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -85,20 +85,20 @@ def process_file(uploaded_file, _cache_key):
85
 
86
  elif uploaded_file.type in ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "text/csv"]:
87
  df = pd.read_excel(uploaded_file) if uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" else pd.read_csv(uploaded_file)
88
- required_cols = ["Scope", "Functional Location"]
89
- optional_cols = ["Unit name"] # New column added
90
- available_cols = [col for col in required_cols + optional_cols if col in df.columns]
91
 
92
- if not any(col in required_cols for col in available_cols):
93
- st.warning("No 'Scope' or 'Functional Location' columns found. Treating as plain text.")
94
- return {"type": "text", "content": df.to_string()}
 
 
95
 
96
- # Pre-process and concatenate Scope and Functional Location (and Unit name if present)
97
- df = df.dropna(subset=[col for col in available_cols if col in required_cols])
98
- df["input_text"] = df[[col for col in available_cols if col in required_cols]].apply(
99
  lambda row: " ".join([re.sub(r'\s+', ' ', str(val).lower().strip()) for val in row]), axis=1
100
  )
101
- return {"type": "table", "content": df[["input_text"] + available_cols]}
102
 
103
  except Exception as e:
104
  st.error(f"πŸ“„ Error processing file: {str(e)}")
@@ -182,7 +182,7 @@ if uploaded_file and not st.session_state.file_processed:
182
  st.session_state.file_data = file_data
183
  st.session_state.file_processed = True
184
  if file_data["type"] == "table":
185
- st.write("File uploaded with Scope and Functional Location data. Please provide an instruction.")
186
  else:
187
  st.write("File uploaded as text context. Please provide an instruction.")
188
 
@@ -211,9 +211,7 @@ if prompt := st.chat_input("Ask your inspection question..."):
211
  file_data = st.session_state.file_data
212
  if file_data["type"] == "table":
213
  predictions = classify_instruction(prompt, file_data["content"], model, tokenizer)
214
- # Include "Unit name" if present, otherwise exclude it
215
- available_cols = [col for col in ["Scope", "Functional Location", "Unit name"] if col in file_data["content"].columns]
216
- result_df = file_data["content"][available_cols].copy()
217
  result_df["Predicted Class"] = predictions
218
  st.write("Predicted Item Classes:")
219
  st.table(result_df)
 
85
 
86
  elif uploaded_file.type in ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "text/csv"]:
87
  df = pd.read_excel(uploaded_file) if uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" else pd.read_csv(uploaded_file)
88
+ required_cols = ["Scope", "Functional Location", "Unit name"] # Unit name now required
 
 
89
 
90
+ # Check if all required columns are present
91
+ missing_cols = [col for col in required_cols if col not in df.columns]
92
+ if missing_cols:
93
+ st.error(f"Missing required columns: {', '.join(missing_cols)}. Please upload a file with 'Scope', 'Functional Location', and 'Unit name'.")
94
+ return None
95
 
96
+ # Pre-process and concatenate Scope, Functional Location, and Unit name
97
+ df = df.dropna(subset=required_cols)
98
+ df["input_text"] = df[required_cols].apply(
99
  lambda row: " ".join([re.sub(r'\s+', ' ', str(val).lower().strip()) for val in row]), axis=1
100
  )
101
+ return {"type": "table", "content": df[["input_text"] + required_cols]}
102
 
103
  except Exception as e:
104
  st.error(f"πŸ“„ Error processing file: {str(e)}")
 
182
  st.session_state.file_data = file_data
183
  st.session_state.file_processed = True
184
  if file_data["type"] == "table":
185
+ st.write("File uploaded with Scope, Functional Location, and Unit name data. Please provide an instruction.")
186
  else:
187
  st.write("File uploaded as text context. Please provide an instruction.")
188
 
 
211
  file_data = st.session_state.file_data
212
  if file_data["type"] == "table":
213
  predictions = classify_instruction(prompt, file_data["content"], model, tokenizer)
214
+ result_df = file_data["content"][["Scope", "Functional Location", "Unit name"]].copy()
 
 
215
  result_df["Predicted Class"] = predictions
216
  st.write("Predicted Item Classes:")
217
  st.table(result_df)