Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ df["model"] = df["model"].str.strip()
|
|
| 13 |
df["features"] = df["features"].astype(str).str.lower()
|
| 14 |
|
| 15 |
# Control long responses
|
| 16 |
-
MAX_TOTAL_CHARACTERS =
|
| 17 |
|
| 18 |
def extract_numbers(text: str) -> List[float]:
|
| 19 |
"""Extract all numbers from text"""
|
|
@@ -160,8 +160,12 @@ def handle_specific_questions(query: str) -> str:
|
|
| 160 |
|
| 161 |
return ""
|
| 162 |
|
| 163 |
-
def format_car_details(car: Dict, show_features: bool = True) -> str:
|
| 164 |
"""Format car details for display"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
features_text = ""
|
| 166 |
if show_features and 'features' in car:
|
| 167 |
features = car['features'][:200] + "..." if len(car['features']) > 200 else car['features']
|
|
@@ -243,17 +247,45 @@ def answer_question(query: str) -> str:
|
|
| 243 |
return "❌ No matching cars found for your query. Try adjusting your requirements!"
|
| 244 |
|
| 245 |
response = ""
|
| 246 |
-
if len(filtered_df) > 1:
|
| 247 |
-
response += f"Found {len(filtered_df)} matching cars:\n\n"
|
| 248 |
-
|
| 249 |
-
for _, row in filtered_df.head(5).iterrows(): # Show top 5 results
|
| 250 |
-
entry = format_car_details(row.to_dict()) + "\n"
|
| 251 |
-
if len(response + entry) > MAX_TOTAL_CHARACTERS:
|
| 252 |
-
break
|
| 253 |
-
response += entry
|
| 254 |
|
| 255 |
-
if
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
return response.strip()
|
| 259 |
|
|
|
|
| 13 |
df["features"] = df["features"].astype(str).str.lower()
|
| 14 |
|
| 15 |
# Control long responses
|
| 16 |
+
MAX_TOTAL_CHARACTERS = 5000
|
| 17 |
|
| 18 |
def extract_numbers(text: str) -> List[float]:
|
| 19 |
"""Extract all numbers from text"""
|
|
|
|
| 160 |
|
| 161 |
return ""
|
| 162 |
|
| 163 |
+
def format_car_details(car: Dict, show_features: bool = True, compact: bool = False) -> str:
|
| 164 |
"""Format car details for display"""
|
| 165 |
+
if compact:
|
| 166 |
+
# Compact format for showing many cars
|
| 167 |
+
return f"🚗 {car['brand'].title()} {car['model']} | ₹{car['price_lakh']}L | {car['mileage_kmpl']} kmpl | {car['engine']}"
|
| 168 |
+
|
| 169 |
features_text = ""
|
| 170 |
if show_features and 'features' in car:
|
| 171 |
features = car['features'][:200] + "..." if len(car['features']) > 200 else car['features']
|
|
|
|
| 247 |
return "❌ No matching cars found for your query. Try adjusting your requirements!"
|
| 248 |
|
| 249 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
+
# Check if it's a simple brand query (show all cars from that brand)
|
| 252 |
+
is_simple_brand_query = (
|
| 253 |
+
len(brands) == 1 and
|
| 254 |
+
not models and
|
| 255 |
+
min_price is None and max_price is None and
|
| 256 |
+
min_mileage is None and max_mileage is None and
|
| 257 |
+
not features and
|
| 258 |
+
not any(word in query.lower() for word in ['cheap', 'expensive', 'best', 'compare', 'vs'])
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
if is_simple_brand_query and len(filtered_df) > 3:
|
| 262 |
+
# Show all cars for simple brand queries in compact format
|
| 263 |
+
response += f"🏷️ All {brands[0].title()} cars in our database ({len(filtered_df)} models):\n\n"
|
| 264 |
+
for _, row in filtered_df.iterrows():
|
| 265 |
+
response += format_car_details(row.to_dict(), show_features=False, compact=True) + "\n"
|
| 266 |
+
|
| 267 |
+
# Add a summary
|
| 268 |
+
avg_price = filtered_df['price_lakh'].mean()
|
| 269 |
+
avg_mileage = filtered_df['mileage_kmpl'].mean()
|
| 270 |
+
price_range = f"₹{filtered_df['price_lakh'].min()}-{filtered_df['price_lakh'].max()}L"
|
| 271 |
+
response += f"\n📊 Summary: Average price: ₹{avg_price:.1f}L | Average mileage: {avg_mileage:.1f} kmpl | Price range: {price_range}"
|
| 272 |
+
|
| 273 |
+
else:
|
| 274 |
+
# Regular detailed format for filtered results
|
| 275 |
+
if len(filtered_df) > 1:
|
| 276 |
+
response += f"Found {len(filtered_df)} matching cars:\n\n"
|
| 277 |
+
|
| 278 |
+
# Determine how many cars to show in detail
|
| 279 |
+
max_detailed_cars = 8 if len(filtered_df) <= 10 else 5
|
| 280 |
+
|
| 281 |
+
for _, row in filtered_df.head(max_detailed_cars).iterrows():
|
| 282 |
+
entry = format_car_details(row.to_dict()) + "\n"
|
| 283 |
+
if len(response + entry) > MAX_TOTAL_CHARACTERS:
|
| 284 |
+
break
|
| 285 |
+
response += entry
|
| 286 |
+
|
| 287 |
+
if len(filtered_df) > max_detailed_cars:
|
| 288 |
+
response += f"\n... and {len(filtered_df) - max_detailed_cars} more cars match your criteria."
|
| 289 |
|
| 290 |
return response.strip()
|
| 291 |
|