Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
|
96 |
-
# Pre-process and concatenate Scope
|
97 |
-
df = df.dropna(subset=
|
98 |
-
df["input_text"] = df[
|
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"] +
|
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
|
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 |
-
|
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)
|