Commit
·
31a5120
1
Parent(s):
b5cc0f9
tools update
Browse files
app.py
CHANGED
@@ -54,6 +54,10 @@ def download_file(file_name: str) -> None:
|
|
54 |
|
55 |
@tool
|
56 |
def open_file_as_text(file_name: str, filetype: Optional[str] = "txt") -> str:
|
|
|
|
|
|
|
|
|
57 |
download_file(file_name)
|
58 |
try:
|
59 |
if filetype == "txt":
|
@@ -86,6 +90,15 @@ def open_file_as_text(file_name: str, filetype: Optional[str] = "txt") -> str:
|
|
86 |
|
87 |
@tool
|
88 |
def web_search(query: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
try:
|
90 |
with DDGS() as ddgs:
|
91 |
results = ddgs.text(query, max_results=3)
|
@@ -95,25 +108,17 @@ def web_search(query: str) -> str:
|
|
95 |
except Exception as e:
|
96 |
return f"Error during search: {str(e)}"
|
97 |
|
98 |
-
def parse_wikipedia_table(table) -> str:
|
99 |
-
rows = []
|
100 |
-
headers = []
|
101 |
-
thead = table.find('thead')
|
102 |
-
if thead:
|
103 |
-
for th in thead.find_all('th'):
|
104 |
-
headers.append(th.get_text(separator=" ", strip=True))
|
105 |
-
if headers:
|
106 |
-
rows.append(" | ".join(headers))
|
107 |
-
tbody = table.find('tbody') or table
|
108 |
-
for tr in tbody.find_all('tr'):
|
109 |
-
cells = tr.find_all(['th', 'td'])
|
110 |
-
cell_texts = [cell.get_text(separator=" ", strip=True) for cell in cells if cell]
|
111 |
-
if cell_texts:
|
112 |
-
rows.append(" | ".join(cell_texts))
|
113 |
-
return "\n".join(rows)
|
114 |
-
|
115 |
@tool
|
116 |
def read_wikipedia_page(url: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
headers = {"User-Agent": "Mozilla/5.0"}
|
118 |
resp = requests.get(url, headers=headers, timeout=10)
|
119 |
resp.raise_for_status()
|
@@ -131,6 +136,16 @@ def read_wikipedia_page(url: str) -> str:
|
|
131 |
|
132 |
@tool
|
133 |
def smart_paginate_around_query(full_text: str, query: str) -> list:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
before_chars = 1000
|
135 |
after_chars = 3000
|
136 |
q = query.lower()
|
@@ -149,10 +164,28 @@ def smart_paginate_around_query(full_text: str, query: str) -> list:
|
|
149 |
|
150 |
@tool
|
151 |
def reverse_sentence(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
return text[::-1]
|
153 |
|
154 |
@tool
|
155 |
def run_python_code(file_name: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
download_file(file_name)
|
157 |
try:
|
158 |
result = subprocess.run(["python", file_name], capture_output=True, text=True, timeout=10)
|
|
|
54 |
|
55 |
@tool
|
56 |
def open_file_as_text(file_name: str, filetype: Optional[str] = "txt") -> str:
|
57 |
+
"""
|
58 |
+
Opens and reads a file of the given type (txt, json, csv, xlsx, mp3).
|
59 |
+
Returns its content as readable text or transcription for audio files.
|
60 |
+
"""
|
61 |
download_file(file_name)
|
62 |
try:
|
63 |
if filetype == "txt":
|
|
|
90 |
|
91 |
@tool
|
92 |
def web_search(query: str) -> str:
|
93 |
+
"""
|
94 |
+
Performs a web search using DuckDuckGo and returns the top results.
|
95 |
+
|
96 |
+
Args:
|
97 |
+
query: Search query string.
|
98 |
+
|
99 |
+
Returns:
|
100 |
+
Top search results formatted as title, snippet, and URL.
|
101 |
+
"""
|
102 |
try:
|
103 |
with DDGS() as ddgs:
|
104 |
results = ddgs.text(query, max_results=3)
|
|
|
108 |
except Exception as e:
|
109 |
return f"Error during search: {str(e)}"
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
@tool
|
112 |
def read_wikipedia_page(url: str) -> str:
|
113 |
+
"""
|
114 |
+
Reads and extracts clean text content from a Wikipedia page.
|
115 |
+
|
116 |
+
Args:
|
117 |
+
url: Full URL to the Wikipedia page.
|
118 |
+
|
119 |
+
Returns:
|
120 |
+
Sectioned and readable content from the page, including paragraphs, lists, and tables.
|
121 |
+
"""
|
122 |
headers = {"User-Agent": "Mozilla/5.0"}
|
123 |
resp = requests.get(url, headers=headers, timeout=10)
|
124 |
resp.raise_for_status()
|
|
|
136 |
|
137 |
@tool
|
138 |
def smart_paginate_around_query(full_text: str, query: str) -> list:
|
139 |
+
"""
|
140 |
+
Splits full text into focused windows surrounding the query keyword.
|
141 |
+
|
142 |
+
Args:
|
143 |
+
full_text: The large text content to paginate.
|
144 |
+
query: Keyword or phrase to center each window on.
|
145 |
+
|
146 |
+
Returns:
|
147 |
+
List of substrings centered around the query within the original text.
|
148 |
+
"""
|
149 |
before_chars = 1000
|
150 |
after_chars = 3000
|
151 |
q = query.lower()
|
|
|
164 |
|
165 |
@tool
|
166 |
def reverse_sentence(text: str) -> str:
|
167 |
+
"""
|
168 |
+
Reverses the input text string.
|
169 |
+
|
170 |
+
Args:
|
171 |
+
text: A string to reverse.
|
172 |
+
|
173 |
+
Returns:
|
174 |
+
Reversed string.
|
175 |
+
"""
|
176 |
return text[::-1]
|
177 |
|
178 |
@tool
|
179 |
def run_python_code(file_name: str) -> str:
|
180 |
+
"""
|
181 |
+
Executes a Python script and returns the output.
|
182 |
+
|
183 |
+
Args:
|
184 |
+
file_name: Name of the Python file to execute.
|
185 |
+
|
186 |
+
Returns:
|
187 |
+
Printed standard output or error message from the script.
|
188 |
+
"""
|
189 |
download_file(file_name)
|
190 |
try:
|
191 |
result = subprocess.run(["python", file_name], capture_output=True, text=True, timeout=10)
|