STARX33 commited on
Commit
c702b75
·
verified ·
1 Parent(s): 596ab7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -4
app.py CHANGED
@@ -4,7 +4,7 @@ import requests
4
  import pandas as pd
5
  import yaml
6
  import gradio as gr
7
-
8
  from datetime import datetime
9
  from typing import Optional
10
 
@@ -35,19 +35,52 @@ def get_current_time_in_timezone(timezone: str) -> str:
35
  return f"Error fetching time for timezone '{timezone}': {e}"
36
 
37
  @tool
38
- def wiki(query: str) -> str:
39
  """
40
- Searches Wikipedia for a query and returns the page content (English).
 
 
41
 
42
  Args:
43
  query: The Wikipedia page title or search term.
 
44
  """
45
  try:
 
46
  wikipedia.set_lang("en")
47
  page = wikipedia.page(query, auto_suggest=False)
 
 
 
 
 
48
  return page.content
 
 
 
 
 
 
49
  except Exception as e:
50
- return f"Error fetching Wikipedia page for '{query}': {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # -----------------------------------------------------------------------------
53
  # 2) LOAD PROMPTS & RAG MAP
 
4
  import pandas as pd
5
  import yaml
6
  import gradio as gr
7
+ from bs4 import BeautifulSoup
8
  from datetime import datetime
9
  from typing import Optional
10
 
 
35
  return f"Error fetching time for timezone '{timezone}': {e}"
36
 
37
  @tool
38
+ def wiki(query: str, section: Optional[str] = None) -> str:
39
  """
40
+ Searches Wikipedia for `query` and returns either:
41
+ - the full page text (if section is None), or
42
+ - the text of a specific section (if section is given).
43
 
44
  Args:
45
  query: The Wikipedia page title or search term.
46
+ section: (optional) The section heading to extract (e.g. "Discography").
47
  """
48
  try:
49
+ # 1) resolve the page
50
  wikipedia.set_lang("en")
51
  page = wikipedia.page(query, auto_suggest=False)
52
+ except Exception as e:
53
+ return f"Error finding Wikipedia page for '{query}': {e}"
54
+
55
+ # 2) if no section requested, return raw content
56
+ if not section:
57
  return page.content
58
+
59
+ # 3) otherwise fetch the HTML and parse
60
+ try:
61
+ resp = requests.get(page.url, timeout=10)
62
+ resp.raise_for_status()
63
+ soup = BeautifulSoup(resp.text, "lxml")
64
  except Exception as e:
65
+ return f"Error fetching HTML for '{page.url}': {e}"
66
+
67
+ # 4) locate the heading matching `section`
68
+ header = None
69
+ for h in soup.find_all(["h2", "h3"]):
70
+ if section.lower() in h.get_text().lower():
71
+ header = h
72
+ break
73
+ if not header:
74
+ return f"Section '{section}' not found on page '{query}'."
75
+
76
+ # 5) collect text until the next same-level heading
77
+ texts = []
78
+ for sib in header.next_siblings:
79
+ if getattr(sib, "name", None) in ["h2", "h3"]:
80
+ break
81
+ texts.append(sib.get_text(separator="\n").strip())
82
+
83
+ return "\n\n".join([t for t in texts if t])
84
 
85
  # -----------------------------------------------------------------------------
86
  # 2) LOAD PROMPTS & RAG MAP