Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- tools/python_repl.py +8 -0
- tools/read_file.py +6 -0
- tools/visit_webpage.py +12 -0
- tools/wikipedia_search.py +18 -0
tools/python_repl.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def python_repl(code: str) -> str:
|
| 2 |
+
try:
|
| 3 |
+
if ";" in code or "=" in code:
|
| 4 |
+
return "0"
|
| 5 |
+
result = eval(code, {"__builtins__": {}}, {})
|
| 6 |
+
return str(result)
|
| 7 |
+
except Exception:
|
| 8 |
+
return "0"
|
tools/read_file.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def read_file(path: str) -> str:
|
| 2 |
+
try:
|
| 3 |
+
with open(path, "r", encoding="utf-8", errors="ignore") as f:
|
| 4 |
+
return f.read(8000)
|
| 5 |
+
except Exception:
|
| 6 |
+
return "0"
|
tools/visit_webpage.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
|
| 4 |
+
def visit_webpage(url: str) -> str:
|
| 5 |
+
try:
|
| 6 |
+
r = requests.get(url, timeout=10)
|
| 7 |
+
soup = BeautifulSoup(r.text, "html.parser")
|
| 8 |
+
for tag in soup(["script", "style"]):
|
| 9 |
+
tag.decompose()
|
| 10 |
+
return soup.get_text(separator="\n", strip=True)[:8000]
|
| 11 |
+
except Exception:
|
| 12 |
+
return "0"
|
tools/wikipedia_search.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
def wikipedia_search(query: str) -> str:
|
| 4 |
+
try:
|
| 5 |
+
url = "https://en.wikipedia.org/w/api.php"
|
| 6 |
+
params = {
|
| 7 |
+
"action": "query",
|
| 8 |
+
"list": "search",
|
| 9 |
+
"srsearch": query,
|
| 10 |
+
"format": "json",
|
| 11 |
+
}
|
| 12 |
+
r = requests.get(url, params=params, timeout=10)
|
| 13 |
+
pages = r.json()["query"]["search"]
|
| 14 |
+
if not pages:
|
| 15 |
+
return "0"
|
| 16 |
+
return f"https://en.wikipedia.org/?curid={pages[0]['pageid']}"
|
| 17 |
+
except Exception:
|
| 18 |
+
return "0"
|