commit
Browse files- app.py +2 -2
- my_agent.py +10 -1
- my_base_wiki_api.py +81 -0
- my_prompt_config.py +17 -37
- my_tool_wiki_filter_tables.py +59 -0
- my_tool_wiki_page_section.py +75 -0
- requirements.txt +2 -3
- test_tools.py +22 -7
app.py
CHANGED
|
@@ -12,11 +12,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# testing --------------------------------------------
|
| 13 |
testing_mode = True
|
| 14 |
questions_to_run = [
|
| 15 |
-
|
| 16 |
#"a1e91b78-d3d8-4675-bb8d-62741b4b68a6",
|
| 17 |
#"2d83110e-a098-4ebb-9987-066c06fa42d0", # OK
|
| 18 |
# "cca530fc-4052-43b2-b130-b30968d8aa44", # OK, need to remove FINAL_ANSWER string
|
| 19 |
-
"4fc2f1ae-8625-45b5-ab34-ad4433bc21f8", # <--------
|
| 20 |
#"6f37996b-2ac7-44b0-8e68-6d28256631b4",
|
| 21 |
#"9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
| 22 |
#"cabe07ed-9eca-40ea-8ead-410ef5e83f91",
|
|
|
|
| 12 |
# testing --------------------------------------------
|
| 13 |
testing_mode = True
|
| 14 |
questions_to_run = [
|
| 15 |
+
"8e867cd7-cff9-4e6c-867a-ff5ddc2550be", # good answer, wrong albums
|
| 16 |
#"a1e91b78-d3d8-4675-bb8d-62741b4b68a6",
|
| 17 |
#"2d83110e-a098-4ebb-9987-066c06fa42d0", # OK
|
| 18 |
# "cca530fc-4052-43b2-b130-b30968d8aa44", # OK, need to remove FINAL_ANSWER string
|
| 19 |
+
#"4fc2f1ae-8625-45b5-ab34-ad4433bc21f8", # <--------
|
| 20 |
#"6f37996b-2ac7-44b0-8e68-6d28256631b4",
|
| 21 |
#"9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
| 22 |
#"cabe07ed-9eca-40ea-8ead-410ef5e83f91",
|
my_agent.py
CHANGED
|
@@ -9,6 +9,8 @@ from my_tool_chess_board import ChessBoard
|
|
| 9 |
from my_tool_fen import FENTool
|
| 10 |
from my_tool_chess_analysis import ChessAnalysisTool
|
| 11 |
from my_prompt_config import MyPromptConfig
|
|
|
|
|
|
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
# https://huggingface.co/docs/transformers/model_doc/mistral?usage=Pipeline
|
|
@@ -55,6 +57,8 @@ class MyAgent:
|
|
| 55 |
ChessBoard(self.chess_board_model_name, self.chess_board_model_dir),
|
| 56 |
FENTool(),
|
| 57 |
ChessAnalysisTool(),
|
|
|
|
|
|
|
| 58 |
WebSearchTool(),
|
| 59 |
FinalAnswerTool(),
|
| 60 |
],
|
|
@@ -69,13 +73,18 @@ class MyAgent:
|
|
| 69 |
"matplotlib.pyplot",
|
| 70 |
"stockfish",
|
| 71 |
"requests",
|
| 72 |
-
"wikipediaapi",
|
| 73 |
"bs4",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
"my_tool_chess_analysis",
|
| 75 |
"my_tool_chess_board",
|
| 76 |
"my_tool_fen",
|
| 77 |
"my_tool_image_load",
|
| 78 |
"my_tool_reverse_string",
|
|
|
|
|
|
|
| 79 |
],
|
| 80 |
)
|
| 81 |
#web_search_agent = CodeAgent(
|
|
|
|
| 9 |
from my_tool_fen import FENTool
|
| 10 |
from my_tool_chess_analysis import ChessAnalysisTool
|
| 11 |
from my_prompt_config import MyPromptConfig
|
| 12 |
+
from my_tool_wiki_page_section import MyWikiPageSectionTool
|
| 13 |
+
from my_tool_wiki_filter_tables import MyWikiTableFilterTool
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
| 16 |
# https://huggingface.co/docs/transformers/model_doc/mistral?usage=Pipeline
|
|
|
|
| 57 |
ChessBoard(self.chess_board_model_name, self.chess_board_model_dir),
|
| 58 |
FENTool(),
|
| 59 |
ChessAnalysisTool(),
|
| 60 |
+
MyWikiPageSectionTool(),
|
| 61 |
+
MyWikiTableFilterTool(),
|
| 62 |
WebSearchTool(),
|
| 63 |
FinalAnswerTool(),
|
| 64 |
],
|
|
|
|
| 73 |
"matplotlib.pyplot",
|
| 74 |
"stockfish",
|
| 75 |
"requests",
|
|
|
|
| 76 |
"bs4",
|
| 77 |
+
"json",
|
| 78 |
+
"lxml",
|
| 79 |
+
"wikitextparser",
|
| 80 |
+
"mwparserfromhell"
|
| 81 |
"my_tool_chess_analysis",
|
| 82 |
"my_tool_chess_board",
|
| 83 |
"my_tool_fen",
|
| 84 |
"my_tool_image_load",
|
| 85 |
"my_tool_reverse_string",
|
| 86 |
+
"my_tool_wiki_page_section",
|
| 87 |
+
"my_tool_wiki_filter_tables"
|
| 88 |
],
|
| 89 |
)
|
| 90 |
#web_search_agent = CodeAgent(
|
my_base_wiki_api.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import wikitextparser as wtp
|
| 4 |
+
|
| 5 |
+
# https://gist.github.com/scionoftech/0f35d5e231be2cf46823d774023268b6
|
| 6 |
+
# https://www.mediawiki.org/wiki/API:Main_page
|
| 7 |
+
|
| 8 |
+
class MyWikiAPI:
|
| 9 |
+
WIKI_BASE_URL = "https://en.wikipedia.org/w/api.php"
|
| 10 |
+
user_agent_headers = {"user-Agent":
|
| 11 |
+
"AgentsCourseAssignment/1.0 (https://huggingface.co/spaces/krzsam/Agents-Course-Assignment)"}
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
print(f"***KS*** Initializing Wiki API")
|
| 15 |
+
|
| 16 |
+
def __find_section_on_page__(self, page_title, section_title):
|
| 17 |
+
response = requests.get(
|
| 18 |
+
self.WIKI_BASE_URL,
|
| 19 |
+
headers=self.user_agent_headers,
|
| 20 |
+
params={
|
| 21 |
+
'action': 'parse',
|
| 22 |
+
'format': 'json',
|
| 23 |
+
'page': page_title,
|
| 24 |
+
'prop': 'sections',
|
| 25 |
+
}).json()
|
| 26 |
+
sections = response['parse']['sections']
|
| 27 |
+
section_ix_found = None
|
| 28 |
+
for sec in sections:
|
| 29 |
+
if sec["line"] == section_title:
|
| 30 |
+
section_ix_found = sec["index"]
|
| 31 |
+
return section_ix_found
|
| 32 |
+
|
| 33 |
+
def __get_page_section_content__(self, page_title, section_id, format="wikitext"):
|
| 34 |
+
response = requests.get(
|
| 35 |
+
self.WIKI_BASE_URL,
|
| 36 |
+
headers=self.user_agent_headers,
|
| 37 |
+
params={
|
| 38 |
+
'action': 'parse',
|
| 39 |
+
'format': 'json',
|
| 40 |
+
'page': page_title,
|
| 41 |
+
'prop': format,
|
| 42 |
+
'section': section_id
|
| 43 |
+
}).json()
|
| 44 |
+
return response["parse"][format]["*"]
|
| 45 |
+
|
| 46 |
+
def get_page_section(self, page_title, section_title):
|
| 47 |
+
section_id = self.__find_section_on_page__(page_title, section_title)
|
| 48 |
+
_ret = ""
|
| 49 |
+
if section_id is not None:
|
| 50 |
+
_ret = self.__get_page_section_content__(page_title, section_id)
|
| 51 |
+
# print(f"***KS*** Got page section for {page_title} / {section_title} \n{_ret}")
|
| 52 |
+
return _ret
|
| 53 |
+
|
| 54 |
+
def filter_section_and_table(self, section_content, sub_section_name, year_start, year_end):
|
| 55 |
+
parsed = wtp.parse(section_content)
|
| 56 |
+
#print(f"***KS*** Parsed section:\n{parsed}")
|
| 57 |
+
|
| 58 |
+
sections = parsed.sections
|
| 59 |
+
print(f"Sections {len(sections)}")
|
| 60 |
+
section_found = None
|
| 61 |
+
for sec in sections:
|
| 62 |
+
print("---------------------------------------------------------------------")
|
| 63 |
+
print(f"{sec.title if sec.title is not None else ''}")
|
| 64 |
+
if sec.title is not None and sec.title.find(sub_section_name) >= 0:
|
| 65 |
+
section_found = sec
|
| 66 |
+
|
| 67 |
+
print(f"Found matching subsection: {section_found}")
|
| 68 |
+
|
| 69 |
+
print(f"Section has tables: {section_found.tables}")
|
| 70 |
+
|
| 71 |
+
table_data = section_found.tables[0].data()
|
| 72 |
+
print(f"Table has data: {table_data}")
|
| 73 |
+
rows_collected = []
|
| 74 |
+
for row in table_data:
|
| 75 |
+
print("---------------------------------------")
|
| 76 |
+
print(f"Table has data: {row}")
|
| 77 |
+
|
| 78 |
+
# TODO do filtering of rows and return the filtered table
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
return ["aaa"]
|
my_prompt_config.py
CHANGED
|
@@ -1,16 +1,8 @@
|
|
| 1 |
from smolagents import PromptTemplates, PlanningPromptTemplate, FinalAnswerPromptTemplate, ManagedAgentPromptTemplate
|
| 2 |
|
| 3 |
-
# Additional tools provided :
|
| 4 |
-
# * _my_tool_chess_analysis : analyze chess position provided in FEN notation and provide best move,
|
| 5 |
-
# available arguments: fen, player_color
|
| 6 |
-
# * _my_tool_chess_board : process an image and extract list of chess pieces, available arguments: img ,
|
| 7 |
-
# * _my_tool_fen : convert list of chess pieces into FEN notation, available arguments: chest_pieces ,
|
| 8 |
-
# * _my_tool_image_load : load an image for given task_id, available arguments: task_id ,
|
| 9 |
-
#
|
| 10 |
# Use <code></code> examples as here
|
| 11 |
# https://github.com/huggingface/smolagents/blob/main/src/smolagents/vision_web_browser.py
|
| 12 |
-
|
| 13 |
-
# * _my_tool_ reverse_string : reverse a string, available arguments: input_str ,
|
| 14 |
|
| 15 |
class MyPromptConfig:
|
| 16 |
PROMPT_TEMPLATES = PromptTemplates(
|
|
@@ -27,14 +19,18 @@ class MyPromptConfig:
|
|
| 27 |
If you are going to use a tool, describe in detail how you are going
|
| 28 |
to invoke the tool and explain parameters used to invoke the tool.
|
| 29 |
|
| 30 |
-
The list of tools available is provided below.
|
| 31 |
-
Decide and
|
|
|
|
|
|
|
| 32 |
|
| 33 |
* _my_tool_chess_analysis : analyze chess position provided as FEN notation
|
| 34 |
* _my_tool_chess_board : extract list of chess pieces from chess board image
|
| 35 |
* _my_tool_fen : convert list of chess pieces into FEN notation
|
| 36 |
* _my_tool_image_load : load image of the chess board for given task_id
|
| 37 |
* _my_tool_reverse_string : reverse provided string
|
|
|
|
|
|
|
| 38 |
|
| 39 |
If the question mentions image or other file, use one of provided tools to load it
|
| 40 |
using task_id associated with the question.
|
|
@@ -47,18 +43,6 @@ class MyPromptConfig:
|
|
| 47 |
plain text unless specified otherwise.
|
| 48 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put
|
| 49 |
in the list is a number or a string.
|
| 50 |
-
|
| 51 |
-
Report your answer using final_answer tool
|
| 52 |
-
|
| 53 |
-
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
|
| 54 |
-
numbers and/or strings.
|
| 55 |
-
If you are asked for a number, don’t use comma to write your number neither use units such as $ or
|
| 56 |
-
percent sign unless specified otherwise.
|
| 57 |
-
If you are asked for a string, don’t use articles, neither abbreviations (e.g. for cities),
|
| 58 |
-
and write the digits in plain text unless specified otherwise.
|
| 59 |
-
If you are asked for a comma separated list, apply the above rules depending of whether the element
|
| 60 |
-
to be put in the list is a number or a string
|
| 61 |
-
|
| 62 |
""",
|
| 63 |
planning=PlanningPromptTemplate(
|
| 64 |
initial_plan="""
|
|
@@ -75,23 +59,19 @@ class MyPromptConfig:
|
|
| 75 |
final_answer=FinalAnswerPromptTemplate(
|
| 76 |
pre_messages="",
|
| 77 |
post_messages="""
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"""
|
| 80 |
),
|
| 81 |
)
|
| 82 |
|
| 83 |
def __init__(self):
|
| 84 |
print("Prompt Templates initialized")
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
#EMPTY_PROMPT_TEMPLATES = PromptTemplates(
|
| 89 |
-
# system_prompt="",
|
| 90 |
-
# planning=PlanningPromptTemplate(
|
| 91 |
-
# initial_plan="",
|
| 92 |
-
# update_plan_pre_messages="",
|
| 93 |
-
# update_plan_post_messages="",
|
| 94 |
-
# ),
|
| 95 |
-
# managed_agent=ManagedAgentPromptTemplate(task="", report=""),
|
| 96 |
-
# final_answer=FinalAnswerPromptTemplate(pre_messages="", post_messages=""),
|
| 97 |
-
#)
|
|
|
|
| 1 |
from smolagents import PromptTemplates, PlanningPromptTemplate, FinalAnswerPromptTemplate, ManagedAgentPromptTemplate
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
# Use <code></code> examples as here
|
| 4 |
# https://github.com/huggingface/smolagents/blob/main/src/smolagents/vision_web_browser.py
|
| 5 |
+
|
|
|
|
| 6 |
|
| 7 |
class MyPromptConfig:
|
| 8 |
PROMPT_TEMPLATES = PromptTemplates(
|
|
|
|
| 19 |
If you are going to use a tool, describe in detail how you are going
|
| 20 |
to invoke the tool and explain parameters used to invoke the tool.
|
| 21 |
|
| 22 |
+
The list of tools available is provided below. They allow to solve a variety of various questions asked.
|
| 23 |
+
Decide and pick and use only tools which are necessary to answer the question asked.
|
| 24 |
+
If the tool looks proper for the task, use it in the first place, instead of generating the code
|
| 25 |
+
yourself to achieve the same task.
|
| 26 |
|
| 27 |
* _my_tool_chess_analysis : analyze chess position provided as FEN notation
|
| 28 |
* _my_tool_chess_board : extract list of chess pieces from chess board image
|
| 29 |
* _my_tool_fen : convert list of chess pieces into FEN notation
|
| 30 |
* _my_tool_image_load : load image of the chess board for given task_id
|
| 31 |
* _my_tool_reverse_string : reverse provided string
|
| 32 |
+
* _my_tool_wiki_page_section : get contents of a section of the Wikipedia page
|
| 33 |
+
* _my_tool_wiki_table_filter : filter the subsection and tables on the Wikipedia page for provided years
|
| 34 |
|
| 35 |
If the question mentions image or other file, use one of provided tools to load it
|
| 36 |
using task_id associated with the question.
|
|
|
|
| 43 |
plain text unless specified otherwise.
|
| 44 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put
|
| 45 |
in the list is a number or a string.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
""",
|
| 47 |
planning=PlanningPromptTemplate(
|
| 48 |
initial_plan="""
|
|
|
|
| 59 |
final_answer=FinalAnswerPromptTemplate(
|
| 60 |
pre_messages="",
|
| 61 |
post_messages="""
|
| 62 |
+
Report your answer using final_answer tool
|
| 63 |
+
|
| 64 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
|
| 65 |
+
numbers and/or strings.
|
| 66 |
+
If you are asked for a number, don’t use comma to write your number neither use units such as $ or
|
| 67 |
+
percent sign unless specified otherwise.
|
| 68 |
+
If you are asked for a string, don’t use articles, neither abbreviations (e.g. for cities),
|
| 69 |
+
and write the digits in plain text unless specified otherwise.
|
| 70 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element
|
| 71 |
+
to be put in the list is a number or a string
|
| 72 |
"""
|
| 73 |
),
|
| 74 |
)
|
| 75 |
|
| 76 |
def __init__(self):
|
| 77 |
print("Prompt Templates initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my_tool_wiki_filter_tables.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from my_base_wiki_api import MyWikiAPI
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
#AUTHORIZED_TYPES = [
|
| 6 |
+
# "string",
|
| 7 |
+
# "boolean",
|
| 8 |
+
# "integer",
|
| 9 |
+
# "number",
|
| 10 |
+
# "image",
|
| 11 |
+
# "audio",
|
| 12 |
+
# "array",
|
| 13 |
+
# "object",
|
| 14 |
+
# "any",
|
| 15 |
+
# "null",
|
| 16 |
+
#]
|
| 17 |
+
|
| 18 |
+
# https://wikipedia-api.readthedocs.io/en/latest/API.html
|
| 19 |
+
# https://github.com/martin-majlis/Wikipedia-API/tree/master
|
| 20 |
+
class MyWikiTableFilterTool(Tool):
|
| 21 |
+
name = "_my_tool_wiki_table_filter"
|
| 22 |
+
description = """
|
| 23 |
+
Extract contents of the page section for the provided a Wikipedia page
|
| 24 |
+
To invoke the tool use code as below
|
| 25 |
+
<code>
|
| 26 |
+
section_contents = _my_tool_wiki_table_filter(section_content='aaa', sub_section_name='bbb',
|
| 27 |
+
year_start='1998', year_end='2005')
|
| 28 |
+
</code>
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
inputs = {
|
| 32 |
+
"section_content": {
|
| 33 |
+
"type": "string",
|
| 34 |
+
"description": "Wikipedia page title",
|
| 35 |
+
},
|
| 36 |
+
"sub_section_name": {
|
| 37 |
+
"type": "string",
|
| 38 |
+
"description": "Wikipedia page title",
|
| 39 |
+
},
|
| 40 |
+
"year_start": {
|
| 41 |
+
"type": "string",
|
| 42 |
+
"description": "Name of the section on the Wikipedia page",
|
| 43 |
+
},
|
| 44 |
+
"year_end": {
|
| 45 |
+
"type": "string",
|
| 46 |
+
"description": "Name of the section on the Wikipedia page",
|
| 47 |
+
},
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
output_type = "string"
|
| 51 |
+
|
| 52 |
+
is_initialized = True
|
| 53 |
+
|
| 54 |
+
def __init__(self):
|
| 55 |
+
print(f"***KS*** Wiki table filter tool initializing ...")
|
| 56 |
+
self.wiki = MyWikiAPI()
|
| 57 |
+
|
| 58 |
+
def forward(self, section_content, sub_section_name, year_start, year_end) -> str:
|
| 59 |
+
return self.wiki.filter_section_and_table(section_content,sub_section_name, year_start, year_end)
|
my_tool_wiki_page_section.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from my_base_wiki_api import MyWikiAPI
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
#AUTHORIZED_TYPES = [
|
| 6 |
+
# "string",
|
| 7 |
+
# "boolean",
|
| 8 |
+
# "integer",
|
| 9 |
+
# "number",
|
| 10 |
+
# "image",
|
| 11 |
+
# "audio",
|
| 12 |
+
# "array",
|
| 13 |
+
# "object",
|
| 14 |
+
# "any",
|
| 15 |
+
# "null",
|
| 16 |
+
#]
|
| 17 |
+
|
| 18 |
+
# https://wikipedia-api.readthedocs.io/en/latest/API.html
|
| 19 |
+
# https://github.com/martin-majlis/Wikipedia-API/tree/master
|
| 20 |
+
class MyWikiPageSectionTool(Tool):
|
| 21 |
+
name = "_my_tool_wiki_page_section"
|
| 22 |
+
description = """
|
| 23 |
+
Extract contents of the page section for the provided a Wikipedia page
|
| 24 |
+
To invoke the tool use code as below
|
| 25 |
+
<code>
|
| 26 |
+
section_contents = _my_tool_wiki_page_section(page_title='aaa', section_title='bbb', version='2022')
|
| 27 |
+
</code>
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
inputs = {
|
| 31 |
+
"page": {
|
| 32 |
+
"type": "string",
|
| 33 |
+
"description": "Wikipedia page title",
|
| 34 |
+
"nullable": True
|
| 35 |
+
},
|
| 36 |
+
"page_title": {
|
| 37 |
+
"type": "string",
|
| 38 |
+
"description": "Wikipedia page title",
|
| 39 |
+
"nullable": True
|
| 40 |
+
},
|
| 41 |
+
"section": {
|
| 42 |
+
"type": "string",
|
| 43 |
+
"description": "Name of the section on the Wikipedia page",
|
| 44 |
+
"nullable": True
|
| 45 |
+
},
|
| 46 |
+
"section_title": {
|
| 47 |
+
"type": "string",
|
| 48 |
+
"description": "Name of the section on the Wikipedia page",
|
| 49 |
+
"nullable": True
|
| 50 |
+
},
|
| 51 |
+
"version": {
|
| 52 |
+
"type": "string",
|
| 53 |
+
"description": "Year version of the Wikipedia page",
|
| 54 |
+
"nullable": True
|
| 55 |
+
},
|
| 56 |
+
"wiki_version": {
|
| 57 |
+
"type": "string",
|
| 58 |
+
"description": "Year version of the Wikipedia page",
|
| 59 |
+
"nullable": True
|
| 60 |
+
},
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
output_type = "string"
|
| 64 |
+
|
| 65 |
+
is_initialized = True
|
| 66 |
+
|
| 67 |
+
def __init__(self):
|
| 68 |
+
print(f"***KS*** Wiki page section tool initializing ...")
|
| 69 |
+
self.wiki = MyWikiAPI()
|
| 70 |
+
|
| 71 |
+
def forward(self, page=None, page_title=None, section=None, section_title=None, version="2025",
|
| 72 |
+
wiki_version="2025") -> str:
|
| 73 |
+
return self.wiki.get_page_section(
|
| 74 |
+
page if page is not None else page_title,
|
| 75 |
+
section if section is not None else section_title)
|
requirements.txt
CHANGED
|
@@ -8,7 +8,6 @@ PyQt6
|
|
| 8 |
opencv-python
|
| 9 |
torch
|
| 10 |
stockfish
|
| 11 |
-
wikipedia-api
|
| 12 |
bs4
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 8 |
opencv-python
|
| 9 |
torch
|
| 10 |
stockfish
|
|
|
|
| 11 |
bs4
|
| 12 |
+
wikitextparser
|
| 13 |
+
mwparserfromhell
|
test_tools.py
CHANGED
|
@@ -3,19 +3,33 @@ from my_tool_image_load import ImageLoadTool
|
|
| 3 |
from my_tool_chess_board import ChessBoard
|
| 4 |
from my_tool_fen import FENTool
|
| 5 |
from my_tool_chess_analysis import ChessAnalysisTool
|
|
|
|
|
|
|
| 6 |
import pytest
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
import matplotlib as mp
|
| 9 |
|
| 10 |
#pytest --capture=no
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@pytest.mark.skip(reason="disabled")
|
| 13 |
-
@pytest.mark.parametrize("_inp,_exp",[("abc", "cba"),("ihg fed cba", "abc def ghi")])
|
| 14 |
def test_tool_reverse_string(_inp,_exp):
|
| 15 |
assert ReverseStringTool().forward(_inp) == _exp
|
| 16 |
|
| 17 |
@pytest.mark.skip(reason="disabled")
|
| 18 |
-
@pytest.mark.parametrize("_task_id,_exp",[("cca530fc-4052-43b2-b130-b30968d8aa44", "")])
|
| 19 |
def test_tool_image_load(_task_id,_exp):
|
| 20 |
print(f"\nLoading image for task id: {_task_id}")
|
| 21 |
t = ImageLoadTool()
|
|
@@ -26,7 +40,8 @@ def test_tool_image_load(_task_id,_exp):
|
|
| 26 |
#plt.show()
|
| 27 |
|
| 28 |
@pytest.mark.skip(reason="disabled")
|
| 29 |
-
@pytest.mark.parametrize("_task_id,_exp",[("cca530fc-4052-43b2-b130-b30968d8aa44",
|
|
|
|
| 30 |
def test_tool_chess_board(_task_id,_exp):
|
| 31 |
print(f"\nLoading image for task id: {_task_id}")
|
| 32 |
t = ImageLoadTool()
|
|
@@ -39,7 +54,7 @@ def test_tool_chess_board(_task_id,_exp):
|
|
| 39 |
assert pieces == _exp
|
| 40 |
|
| 41 |
|
| 42 |
-
|
| 43 |
@pytest.mark.parametrize("_pieces_list,_exp",
|
| 44 |
[
|
| 45 |
("1K1111111PP11111P11RBBqP1111n111Q1111111p11b11111pp111pp1k11r111",
|
|
@@ -52,10 +67,10 @@ def test_tool_fen(_pieces_list,_exp):
|
|
| 52 |
print(f"Got result: {fen}")
|
| 53 |
assert fen == _exp
|
| 54 |
|
| 55 |
-
|
| 56 |
@pytest.mark.parametrize("_fen,_exp",
|
| 57 |
[
|
| 58 |
-
("3r2k1/pp3pp1/4b2p/7Q/3n4/PqBBR2P/5PP1/6K1 b - - 0 1","d8d5")
|
| 59 |
])
|
| 60 |
def test_tool_chess_analysis(_fen,_exp):
|
| 61 |
print(f"\nAnalysing FEN: {_fen}")
|
|
|
|
| 3 |
from my_tool_chess_board import ChessBoard
|
| 4 |
from my_tool_fen import FENTool
|
| 5 |
from my_tool_chess_analysis import ChessAnalysisTool
|
| 6 |
+
from my_tool_wiki_page_section import MyWikiPageSectionTool
|
| 7 |
+
from my_tool_wiki_filter_tables import MyWikiTableFilterTool
|
| 8 |
import pytest
|
|
|
|
| 9 |
import matplotlib as mp
|
| 10 |
|
| 11 |
#pytest --capture=no
|
| 12 |
|
| 13 |
+
|
| 14 |
+
@pytest.mark.parametrize("_page,_section,_sub_section,_year_start,_year_end,_exp",
|
| 15 |
+
[("Mercedes Sosa", "Discography", "Studio albums", 2000, 2009, 3)])
|
| 16 |
+
def test_tool_wiki_page_section(_page, _section, _sub_section, _year_start, _year_end, _exp):
|
| 17 |
+
w = MyWikiPageSectionTool()
|
| 18 |
+
f = MyWikiTableFilterTool()
|
| 19 |
+
result = w(page=_page, section=_section)
|
| 20 |
+
# print(f"Got page section content: \n{result}")
|
| 21 |
+
filtered = f(result, _sub_section, _year_start, _year_end)
|
| 22 |
+
print(f"Table filtered: \n{filtered}")
|
| 23 |
+
assert len(filtered) == _exp
|
| 24 |
+
|
| 25 |
+
|
| 26 |
@pytest.mark.skip(reason="disabled")
|
| 27 |
+
@pytest.mark.parametrize("_inp,_exp", [("abc", "cba"), ("ihg fed cba", "abc def ghi")])
|
| 28 |
def test_tool_reverse_string(_inp,_exp):
|
| 29 |
assert ReverseStringTool().forward(_inp) == _exp
|
| 30 |
|
| 31 |
@pytest.mark.skip(reason="disabled")
|
| 32 |
+
@pytest.mark.parametrize("_task_id,_exp", [("cca530fc-4052-43b2-b130-b30968d8aa44", "")])
|
| 33 |
def test_tool_image_load(_task_id,_exp):
|
| 34 |
print(f"\nLoading image for task id: {_task_id}")
|
| 35 |
t = ImageLoadTool()
|
|
|
|
| 40 |
#plt.show()
|
| 41 |
|
| 42 |
@pytest.mark.skip(reason="disabled")
|
| 43 |
+
@pytest.mark.parametrize("_task_id,_exp", [("cca530fc-4052-43b2-b130-b30968d8aa44",
|
| 44 |
+
"1K1111111PP11111P11RBBqP1111n111Q1111111p11b11111pp111pp1k11r111")])
|
| 45 |
def test_tool_chess_board(_task_id,_exp):
|
| 46 |
print(f"\nLoading image for task id: {_task_id}")
|
| 47 |
t = ImageLoadTool()
|
|
|
|
| 54 |
assert pieces == _exp
|
| 55 |
|
| 56 |
|
| 57 |
+
@pytest.mark.skip(reason="disabled")
|
| 58 |
@pytest.mark.parametrize("_pieces_list,_exp",
|
| 59 |
[
|
| 60 |
("1K1111111PP11111P11RBBqP1111n111Q1111111p11b11111pp111pp1k11r111",
|
|
|
|
| 67 |
print(f"Got result: {fen}")
|
| 68 |
assert fen == _exp
|
| 69 |
|
| 70 |
+
@pytest.mark.skip(reason="disabled")
|
| 71 |
@pytest.mark.parametrize("_fen,_exp",
|
| 72 |
[
|
| 73 |
+
("3r2k1/pp3pp1/4b2p/7Q/3n4/PqBBR2P/5PP1/6K1 b - - 0 1", "d8d5")
|
| 74 |
])
|
| 75 |
def test_tool_chess_analysis(_fen,_exp):
|
| 76 |
print(f"\nAnalysing FEN: {_fen}")
|