Pijush2023 commited on
Commit
117f257
·
verified ·
1 Parent(s): 21e8b53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -47
app.py CHANGED
@@ -1,11 +1,3 @@
1
- import subprocess
2
- import sys
3
-
4
- def install(package):
5
- subprocess.check_call([sys.executable, "-m", "pip", "install", package])
6
-
7
- install('crewai')
8
-
9
  import gradio as gr
10
  import requests
11
  import os
@@ -311,51 +303,99 @@ def bot(history, choice, tts_choice, retrieval_mode):
311
  history.append([response, None]) # Ensure the response is added in the correct format
312
 
313
  import os
314
- import gradio as gr
315
- from crewai import Agent
316
- from langchain.agents import Tool
317
- from langchain.utilities import GoogleSerperAPIWrapper
318
-
319
- # Setup API keys
320
- api_key=os.environ["SERP_API"]
321
-
322
- # Initialize the Google Serper API Wrapper
323
- search = GoogleSerperAPIWrapper()
324
-
325
- # Define a tool specifically for restaurant searches in Birmingham
326
- def restaurant_search(query):
327
- birmingham_query = f"{query} in Birmingham, AL"
328
- return search.run(birmingham_query)
329
-
330
- # Create and assign the search tool to the agent
331
- serper_tool = Tool(
332
- name="Restaurant Search",
333
- func=restaurant_search,
334
- description="Search for restaurants in Birmingham, AL",
335
- )
336
 
337
- # Create an agent with the search tool
338
- restaurant_agent = Agent(
339
- role='Restaurant Search Agent',
340
- goal='Find and recommend the best restaurants in Birmingham, AL',
341
- backstory='An expert on local eateries with a focus on Birmingham, AL.',
342
- tools=[serper_tool]
343
- )
344
 
345
- # Function to handle restaurant-related queries
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  def restaurant_search_agent(query):
347
- if "restaurant" in query.lower() or "eatery" in query.lower():
348
- return restaurant_agent.run(query)
 
349
  else:
350
  return "This agent only handles restaurant queries."
351
 
352
- # General response function for the chatbot
353
- def respond_to_query(query):
354
- if "restaurant" in query.lower() or "eatery" in query.lower():
355
- return restaurant_search_agent(query)
356
- else:
357
- # Handle other queries (existing logic)
358
- return "Handling other queries here"
359
 
360
 
361
  def add_message(history, message):
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
  import os
 
303
  history.append([response, None]) # Ensure the response is added in the correct format
304
 
305
  import os
306
+ from serpapi.google_search import GoogleSearch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
 
 
 
 
 
 
 
 
308
 
309
+ def fetch_restaurant_details(location):
310
+ params = {
311
+ "engine": "yelp",
312
+ "find_desc": "Restaurant",
313
+ "find_loc": location,
314
+ "api_key": os.getenv("SERP_API")
315
+ }
316
+
317
+ search = GoogleSearch(params)
318
+ results = search.get_dict()
319
+ organic_results = results.get("organic_results", [])
320
+
321
+ yelp_html = """
322
+ <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Top Restaurants in {}</h2>
323
+ <style>
324
+ table {{
325
+ font-family: 'Verdana', sans-serif;
326
+ color: #333;
327
+ border-collapse: collapse;
328
+ width: 100%;
329
+ }}
330
+ th, td {{
331
+ border: 1px solid #fff !important;
332
+ padding: 8px;
333
+ }}
334
+ th {{
335
+ background-color: #f2f2f2;
336
+ color: #333;
337
+ text-align: left;
338
+ }}
339
+ tr:hover {{
340
+ background-color: #f5f5f5;
341
+ }}
342
+ .restaurant-link {{
343
+ color: #1E90FF;
344
+ text-decoration: none;
345
+ }}
346
+ .restaurant-link:hover {{
347
+ text-decoration: underline;
348
+ }}
349
+ </style>
350
+ <table>
351
+ <tr>
352
+ <th>Name</th>
353
+ <th>Rating</th>
354
+ <th>Reviews</th>
355
+ <th>Phone</th>
356
+ <th>Snippet</th>
357
+ <th>Services</th>
358
+ </tr>
359
+ """.format(location)
360
+
361
+ for result in organic_results:
362
+ name = result.get("title", "No name")
363
+ rating = result.get("rating", "No rating")
364
+ reviews = result.get("reviews", "No reviews")
365
+ phone = result.get("phone", "Not Available")
366
+ snippet = result.get("snippet", "No Available")
367
+ services = result.get("service_options", "Not Known")
368
+
369
+ if isinstance(services, list):
370
+ services = ", ".join(services)
371
+ elif isinstance(services, dict):
372
+ services = ", ".join([f"{key}: {value}" for key, value in services.items()])
373
+
374
+ link = result.get("link", "#")
375
+
376
+ yelp_html += f"""
377
+ <tr>
378
+ <td><a class='restaurant-link' href='{link}' target='_blank'>{name}</a></td>
379
+ <td>{rating}</td>
380
+ <td>{reviews}</td>
381
+ <td>{phone}</td>
382
+ <td>{snippet}</td>
383
+ <td>{services}</td>
384
+ </tr>
385
+ """
386
+
387
+ yelp_html += "</table>"
388
+ return yelp_html
389
+
390
  def restaurant_search_agent(query):
391
+ if "restaurant" in query.lower():
392
+ location = "Birmingham, AL, USA" # You can customize this based on user input or other logic
393
+ return fetch_restaurant_details(location)
394
  else:
395
  return "This agent only handles restaurant queries."
396
 
397
+
398
+
 
 
 
 
 
399
 
400
 
401
  def add_message(history, message):