Spaces:
Sleeping
Sleeping
Ritvik
commited on
Commit
Β·
e6dbc24
1
Parent(s):
b381835
Updated app 9
Browse files
app.py
CHANGED
|
@@ -65,19 +65,22 @@ def web_search_duckduckgo(query: str, max_results: int = 5, max_retries: int = 2
|
|
| 65 |
results = []
|
| 66 |
for attempt in range(max_retries):
|
| 67 |
try:
|
|
|
|
| 68 |
with DDGS() as ddgs:
|
| 69 |
for r in ddgs.text(query, region="in-en", safesearch="Moderate", max_results=max_results):
|
| 70 |
results.append({"title": r['title'], "url": r['href']})
|
| 71 |
if not results:
|
| 72 |
raise ValueError("No results found")
|
| 73 |
-
formatted_results = "\n
|
| 74 |
search_cache[cache_key] = (time.time(), formatted_results)
|
| 75 |
print(f"Search results for {query}: {formatted_results}")
|
| 76 |
return formatted_results
|
| 77 |
except Exception as e:
|
| 78 |
print(f"Search attempt {attempt + 1} failed: {str(e)}")
|
| 79 |
if attempt + 1 == max_retries:
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
time.sleep(1)
|
| 82 |
|
| 83 |
# Trigger keywords for garage search
|
|
@@ -94,7 +97,10 @@ trigger_keywords = [
|
|
| 94 |
"car noise issue", "check engine light", "dashboard warning light", "local garage",
|
| 95 |
"trusted mechanic", "authorized service center", "car towing service near me",
|
| 96 |
"car not starting", "flat battery", "jump start service", "roadside assistance",
|
| 97 |
-
"ac not cooling", "car breakdown", "pickup and drop car service", "service centers for car repair"
|
|
|
|
|
|
|
|
|
|
| 98 |
]
|
| 99 |
|
| 100 |
# ReAct agent response with thought process (updated for search enforcement)
|
|
@@ -173,6 +179,11 @@ def respond(message, history, vehicle_profile):
|
|
| 173 |
search_query = f"car {search_query}"
|
| 174 |
if vehicle_profile.get("city"):
|
| 175 |
search_query = f"{search_query} {vehicle_profile['city']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
else:
|
| 177 |
# If no city is provided, ask for clarification
|
| 178 |
response = "I'd be happy to help you find repair workshops near you. However, I need to know your location. Could you please tell me your city or update your vehicle profile?"
|
|
@@ -182,7 +193,7 @@ def respond(message, history, vehicle_profile):
|
|
| 182 |
print(f"Performing web search for: {search_query}")
|
| 183 |
search_results = web_search_duckduckgo(search_query)
|
| 184 |
print(f"Search Results:\n{search_results}")
|
| 185 |
-
final_response = f"π Here are some car repair service centers I found:\n\n{search_results}\n\n**Tip**: {maintenance_tips[hash(message) % len(maintenance_tips)]}"
|
| 186 |
for i in range(0, len(final_response), 10):
|
| 187 |
yield final_response[:i + 10]
|
| 188 |
return
|
|
@@ -237,6 +248,11 @@ def respond(message, history, vehicle_profile):
|
|
| 237 |
search_query = f"car {search_query}"
|
| 238 |
if vehicle_profile.get("city"):
|
| 239 |
search_query = f"{search_query} {vehicle_profile['city']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
else:
|
| 241 |
response = "I'd be happy to help you find repair workshops near you. However, I need to know your location. Could you please tell me your city or update your vehicle profile?"
|
| 242 |
for i in range(0, len(response), 10):
|
|
@@ -245,7 +261,7 @@ def respond(message, history, vehicle_profile):
|
|
| 245 |
print(f"Performing web search for: {search_query}")
|
| 246 |
search_results = web_search_duckduckgo(search_query)
|
| 247 |
print(f"Search Results:\n{search_results}")
|
| 248 |
-
final_response = f"π Here are some car repair service centers I found:\n\n{search_results}\n\n**Tip**: {maintenance_tips[hash(message) % len(maintenance_tips)]}"
|
| 249 |
for i in range(0, len(final_response), 10):
|
| 250 |
yield final_response[:i + 10]
|
| 251 |
break
|
|
|
|
| 65 |
results = []
|
| 66 |
for attempt in range(max_retries):
|
| 67 |
try:
|
| 68 |
+
print(f"Attempting DuckDuckGo search for: {query} (Attempt {attempt + 1})")
|
| 69 |
with DDGS() as ddgs:
|
| 70 |
for r in ddgs.text(query, region="in-en", safesearch="Moderate", max_results=max_results):
|
| 71 |
results.append({"title": r['title'], "url": r['href']})
|
| 72 |
if not results:
|
| 73 |
raise ValueError("No results found")
|
| 74 |
+
formatted_results = "\n".join(f"- [{r['title']}]({r['url']})" for r in results)
|
| 75 |
search_cache[cache_key] = (time.time(), formatted_results)
|
| 76 |
print(f"Search results for {query}: {formatted_results}")
|
| 77 |
return formatted_results
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Search attempt {attempt + 1} failed: {str(e)}")
|
| 80 |
if attempt + 1 == max_retries:
|
| 81 |
+
error_message = f"β οΈ Web search failed after {max_retries} attempts: {str(e)}"
|
| 82 |
+
search_cache[cache_key] = (time.time(), error_message)
|
| 83 |
+
return error_message
|
| 84 |
time.sleep(1)
|
| 85 |
|
| 86 |
# Trigger keywords for garage search
|
|
|
|
| 97 |
"car noise issue", "check engine light", "dashboard warning light", "local garage",
|
| 98 |
"trusted mechanic", "authorized service center", "car towing service near me",
|
| 99 |
"car not starting", "flat battery", "jump start service", "roadside assistance",
|
| 100 |
+
"ac not cooling", "car breakdown", "pickup and drop car service", "service centers for car repair",
|
| 101 |
+
"interior car cleaning", "paint protection", "ceramic coating", "glass coating", "surface coating",
|
| 102 |
+
"full car detailing", "exterior car cleaning", "car dry cleaning", "car body polish", "full car wash",
|
| 103 |
+
"deep cleaning", "car decor"
|
| 104 |
]
|
| 105 |
|
| 106 |
# ReAct agent response with thought process (updated for search enforcement)
|
|
|
|
| 179 |
search_query = f"car {search_query}"
|
| 180 |
if vehicle_profile.get("city"):
|
| 181 |
search_query = f"{search_query} {vehicle_profile['city']}"
|
| 182 |
+
# Add CarMaa recommendation for Uttarakhand cities
|
| 183 |
+
uttarakhand_cities = ["dehradun", "haridwar", "roorkee", "haldwani", "nainital", "rudrapur"]
|
| 184 |
+
carmaa_recommendation = ""
|
| 185 |
+
if any(city in vehicle_profile['city'].lower() for city in uttarakhand_cities):
|
| 186 |
+
carmaa_recommendation = "- [CarMaa Car Care - Recommended](https://carmaacarcare.com/contactus/)\n"
|
| 187 |
else:
|
| 188 |
# If no city is provided, ask for clarification
|
| 189 |
response = "I'd be happy to help you find repair workshops near you. However, I need to know your location. Could you please tell me your city or update your vehicle profile?"
|
|
|
|
| 193 |
print(f"Performing web search for: {search_query}")
|
| 194 |
search_results = web_search_duckduckgo(search_query)
|
| 195 |
print(f"Search Results:\n{search_results}")
|
| 196 |
+
final_response = f"π Here are some car repair service centers I found:\n\n{carmaa_recommendation}{search_results}\n\n**Tip**: {maintenance_tips[hash(message) % len(maintenance_tips)]}"
|
| 197 |
for i in range(0, len(final_response), 10):
|
| 198 |
yield final_response[:i + 10]
|
| 199 |
return
|
|
|
|
| 248 |
search_query = f"car {search_query}"
|
| 249 |
if vehicle_profile.get("city"):
|
| 250 |
search_query = f"{search_query} {vehicle_profile['city']}"
|
| 251 |
+
# Add CarMaa recommendation for Uttarakhand cities
|
| 252 |
+
uttarakhand_cities = ["dehradun", "haridwar", "roorkee", "haldwani", "nainital", "rudrapur"]
|
| 253 |
+
carmaa_recommendation = ""
|
| 254 |
+
if any(city in vehicle_profile['city'].lower() for city in uttarakhand_cities):
|
| 255 |
+
carmaa_recommendation = "- [CarMaa Car Care - Recommended](https://carmaacarcare.com/contactus/)\n"
|
| 256 |
else:
|
| 257 |
response = "I'd be happy to help you find repair workshops near you. However, I need to know your location. Could you please tell me your city or update your vehicle profile?"
|
| 258 |
for i in range(0, len(response), 10):
|
|
|
|
| 261 |
print(f"Performing web search for: {search_query}")
|
| 262 |
search_results = web_search_duckduckgo(search_query)
|
| 263 |
print(f"Search Results:\n{search_results}")
|
| 264 |
+
final_response = f"π Here are some car repair service centers I found:\n\n{carmaa_recommendation}{search_results}\n\n**Tip**: {maintenance_tips[hash(message) % len(maintenance_tips)]}"
|
| 265 |
for i in range(0, len(final_response), 10):
|
| 266 |
yield final_response[:i + 10]
|
| 267 |
break
|