Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -199,34 +199,58 @@ import pandas as pd
|
|
199 |
from typing import Dict, List, Tuple, Union
|
200 |
from smolagents import CodeAgent, HfApiModel, tool
|
201 |
|
202 |
-
def process_agent_response(response: Union[Dict, str]) -> Tuple[pd.DataFrame, str]:
|
203 |
"""
|
204 |
Process the agent's response and convert it to a DataFrame
|
205 |
Returns DataFrame and error message (if any)
|
206 |
"""
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
try:
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
|
231 |
def search_products(keyword: str, max_products: int) -> Tuple[pd.DataFrame, str, str]:
|
232 |
"""
|
@@ -238,13 +262,24 @@ def search_products(keyword: str, max_products: int) -> Tuple[pd.DataFrame, str,
|
|
238 |
df, error_msg = process_agent_response(result)
|
239 |
|
240 |
if not df.empty:
|
241 |
-
# Select and reorder relevant columns
|
242 |
display_columns = [
|
243 |
'title', 'price', 'rating', 'reviews', 'description',
|
244 |
'bullet_points', 'average_rating', 'total_reviews'
|
245 |
]
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
status_msg = f"Found {len(df)} products"
|
250 |
return df, status_msg, ""
|
|
|
199 |
from typing import Dict, List, Tuple, Union
|
200 |
from smolagents import CodeAgent, HfApiModel, tool
|
201 |
|
202 |
+
def process_agent_response(response: Union[Dict, List, str]) -> Tuple[pd.DataFrame, str]:
|
203 |
"""
|
204 |
Process the agent's response and convert it to a DataFrame
|
205 |
Returns DataFrame and error message (if any)
|
206 |
"""
|
207 |
+
def extract_products_from_response(resp):
|
208 |
+
"""Helper function to extract product data from various response formats"""
|
209 |
+
if isinstance(resp, list):
|
210 |
+
# Response is already a list of products
|
211 |
+
return resp
|
212 |
+
elif isinstance(resp, dict):
|
213 |
+
# Check if it's wrapped in a 'products' key
|
214 |
+
if 'products' in resp:
|
215 |
+
return resp['products']
|
216 |
+
# Check if it's a single product
|
217 |
+
elif 'title' in resp:
|
218 |
+
return [resp]
|
219 |
+
elif 'error' in resp:
|
220 |
+
return None
|
221 |
+
elif isinstance(resp, str):
|
222 |
try:
|
223 |
+
# Try to parse the string response
|
224 |
+
import ast
|
225 |
+
# Remove common prefixes
|
226 |
+
if "Final answer:" in resp:
|
227 |
+
resp = resp.split("Final answer:", 1)[1].strip()
|
228 |
+
elif "Out - Final answer:" in resp:
|
229 |
+
resp = resp.split("Out - Final answer:", 1)[1].strip()
|
230 |
+
|
231 |
+
parsed = ast.literal_eval(resp)
|
232 |
+
# Recursively process the parsed result
|
233 |
+
return extract_products_from_response(parsed)
|
234 |
+
except:
|
235 |
+
pass
|
236 |
+
return None
|
237 |
+
|
238 |
+
try:
|
239 |
+
products = extract_products_from_response(response)
|
240 |
+
|
241 |
+
if products is None:
|
242 |
+
return pd.DataFrame(), "No valid product data found"
|
243 |
+
|
244 |
+
# Convert to DataFrame
|
245 |
+
df = pd.DataFrame(products)
|
246 |
+
|
247 |
+
# Clean up column names
|
248 |
+
df.columns = [col.lower().strip() for col in df.columns]
|
249 |
+
|
250 |
+
return df, ""
|
251 |
+
|
252 |
+
except Exception as e:
|
253 |
+
return pd.DataFrame(), f"Error processing data: {str(e)}"
|
254 |
|
255 |
def search_products(keyword: str, max_products: int) -> Tuple[pd.DataFrame, str, str]:
|
256 |
"""
|
|
|
262 |
df, error_msg = process_agent_response(result)
|
263 |
|
264 |
if not df.empty:
|
265 |
+
# Select and reorder relevant columns, using lowercase names
|
266 |
display_columns = [
|
267 |
'title', 'price', 'rating', 'reviews', 'description',
|
268 |
'bullet_points', 'average_rating', 'total_reviews'
|
269 |
]
|
270 |
+
# Filter for columns that actually exist in the DataFrame
|
271 |
+
available_columns = [col for col in display_columns if col in df.columns]
|
272 |
+
df = df[available_columns]
|
273 |
+
|
274 |
+
# Clean up the display
|
275 |
+
if 'price' in df.columns:
|
276 |
+
df['price'] = df['price'].apply(lambda x: f"${str(x).strip('.')}")
|
277 |
+
|
278 |
+
# Truncate long text fields for better display
|
279 |
+
if 'description' in df.columns:
|
280 |
+
df['description'] = df['description'].apply(lambda x: x[:200] + '...' if len(x) > 200 else x)
|
281 |
+
if 'bullet_points' in df.columns:
|
282 |
+
df['bullet_points'] = df['bullet_points'].apply(lambda x: x[:200] + '...' if len(x) > 200 else x)
|
283 |
|
284 |
status_msg = f"Found {len(df)} products"
|
285 |
return df, status_msg, ""
|