Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -190,76 +190,4 @@ st.success(f"""
|
|
190 |
|
191 |
|
192 |
|
193 |
-
|
194 |
-
|
195 |
-
# Display raw result for debugging purposes
|
196 |
-
st.markdown("<p style='font-family:sans-serif;font-size: 0.9rem;'> Raw Result: </p>", unsafe_allow_html=True)
|
197 |
-
st.success(raw_answer)
|
198 |
-
|
199 |
-
# Processing the raw_answer
|
200 |
-
processed_answer = raw_answer['answer'].replace(';', ' ') # Clean the answer text
|
201 |
-
row_idx = raw_answer['coordinates'][0][0] # Row index from TAPAS
|
202 |
-
col_idx = raw_answer['coordinates'][0][1] # Column index from TAPAS
|
203 |
-
column_name = df.columns[col_idx] # Column name from the DataFrame
|
204 |
-
row_data = df.iloc[row_idx].to_dict() # Row data corresponding to the row index
|
205 |
-
|
206 |
-
# Handle different types of answers (e.g., 'SUM', 'MAX', 'MIN', 'AVG', etc.)
|
207 |
-
if 'SUM' in processed_answer:
|
208 |
-
summary_type = 'sum'
|
209 |
-
numeric_value = df_numeric[column_name].sum()
|
210 |
-
elif 'MAX' in processed_answer:
|
211 |
-
summary_type = 'maximum'
|
212 |
-
numeric_value = df_numeric[column_name].max()
|
213 |
-
elif 'MIN' in processed_answer:
|
214 |
-
summary_type = 'minimum'
|
215 |
-
numeric_value = df_numeric[column_name].min()
|
216 |
-
elif 'AVG' in processed_answer or 'AVERAGE' in processed_answer:
|
217 |
-
summary_type = 'average'
|
218 |
-
numeric_value = df_numeric[column_name].mean()
|
219 |
-
elif 'COUNT' in processed_answer:
|
220 |
-
summary_type = 'count'
|
221 |
-
numeric_value = df_numeric[column_name].count()
|
222 |
-
elif 'MEDIAN' in processed_answer:
|
223 |
-
summary_type = 'median'
|
224 |
-
numeric_value = df_numeric[column_name].median()
|
225 |
-
elif 'STD' in processed_answer or 'STANDARD DEVIATION' in processed_answer:
|
226 |
-
summary_type = 'std_dev'
|
227 |
-
numeric_value = df_numeric[column_name].std()
|
228 |
-
else:
|
229 |
-
summary_type = 'value'
|
230 |
-
numeric_value = processed_answer # In case of a general answer
|
231 |
-
|
232 |
-
# Build a natural language response based on the aggregation type
|
233 |
-
if summary_type == 'sum':
|
234 |
-
natural_language_answer = f"The total {column_name} is {numeric_value}."
|
235 |
-
elif summary_type == 'maximum':
|
236 |
-
natural_language_answer = f"The highest {column_name} is {numeric_value}, recorded for '{row_data.get('Name', 'Unknown')}'."
|
237 |
-
elif summary_type == 'minimum':
|
238 |
-
natural_language_answer = f"The lowest {column_name} is {numeric_value}, recorded for '{row_data.get('Name', 'Unknown')}'."
|
239 |
-
elif summary_type == 'average':
|
240 |
-
natural_language_answer = f"The average {column_name} is {numeric_value}."
|
241 |
-
elif summary_type == 'count':
|
242 |
-
natural_language_answer = f"The number of entries in {column_name} is {numeric_value}."
|
243 |
-
elif summary_type == 'median':
|
244 |
-
natural_language_answer = f"The median {column_name} is {numeric_value}."
|
245 |
-
elif summary_type == 'std_dev':
|
246 |
-
natural_language_answer = f"The standard deviation of {column_name} is {numeric_value}."
|
247 |
-
else:
|
248 |
-
natural_language_answer = f"The {column_name} value is {numeric_value} for '{row_data.get('Name', 'Unknown')}'."
|
249 |
-
|
250 |
-
# Display the final natural language answer
|
251 |
-
st.markdown("<p style='font-family:sans-serif;font-size: 0.9rem;'> Analysis Results: </p>", unsafe_allow_html=True)
|
252 |
-
st.success(f"""
|
253 |
-
• Answer: {natural_language_answer}
|
254 |
-
|
255 |
-
Data Location:
|
256 |
-
• Row: {row_idx + 1}
|
257 |
-
• Column: {column_name}
|
258 |
-
|
259 |
-
Additional Context:
|
260 |
-
• Full Row Data: {row_data}
|
261 |
-
• Query Asked: "{question}"
|
262 |
-
""")
|
263 |
-
|
264 |
-
except Exception as e:
|
265 |
-
st.warning("Please retype your question and make sure to use the column name and cell value correctly.")
|
|
|
190 |
|
191 |
|
192 |
|
193 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|