LamiaYT commited on
Commit
34dd2d1
·
1 Parent(s): 0be2cd2
Files changed (1) hide show
  1. app.py +10 -21
app.py CHANGED
@@ -18,35 +18,24 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
 
19
  @tool
20
  def smart_web_search(query: str) -> str:
21
- """Smart web search with Serper API and Wikipedia fallback."""
 
 
 
 
 
 
 
22
  try:
23
  time.sleep(random.uniform(1, 3))
24
  serper_key = os.getenv("SERPER_API_KEY")
25
  if serper_key:
26
- url = "https://google.serper.dev/search"
27
- payload = json.dumps({"q": query, "num": 5})
28
- headers = {
29
- 'X-API-KEY': serper_key,
30
- 'Content-Type': 'application/json'
31
- }
32
- response = requests.post(url, headers=headers, data=payload, timeout=15)
33
- if response.status_code == 200:
34
- data = response.json()
35
- results = []
36
- if 'answerBox' in data:
37
- results.append(f"ANSWER: {data['answerBox'].get('answer', '')}")
38
- if 'knowledgeGraph' in data:
39
- kg = data['knowledgeGraph']
40
- results.append(f"INFO: {kg.get('title', '')} - {kg.get('description', '')}")
41
- if 'organic' in data:
42
- for item in data['organic'][:3]:
43
- results.append(f"RESULT: {item.get('title', '')} - {item.get('snippet', '')}")
44
- return "\n".join(results) if results else "No Serper results"
45
- # Wikipedia fallback
46
  return get_detailed_wikipedia(query)
47
  except Exception as e:
48
  return f"Search error: {str(e)}"
49
 
 
50
  @tool
51
  def extract_youtube_details(url: str) -> str:
52
  """Extract details from YouTube videos."""
 
18
 
19
  @tool
20
  def smart_web_search(query: str) -> str:
21
+ """Smart web search with Serper API and Wikipedia fallback.
22
+
23
+ Args:
24
+ query (str): The search query to execute
25
+
26
+ Returns:
27
+ str: Search results from Serper API or Wikipedia
28
+ """
29
  try:
30
  time.sleep(random.uniform(1, 3))
31
  serper_key = os.getenv("SERPER_API_KEY")
32
  if serper_key:
33
+ # ... rest of your existing implementation ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  return get_detailed_wikipedia(query)
35
  except Exception as e:
36
  return f"Search error: {str(e)}"
37
 
38
+
39
  @tool
40
  def extract_youtube_details(url: str) -> str:
41
  """Extract details from YouTube videos."""