afg1 commited on
Commit
052b1a1
·
verified ·
1 Parent(s): ae7a494

Write some tools to do stuff with papers

Browse files
Files changed (1) hide show
  1. app.py +105 -11
app.py CHANGED
@@ -1,22 +1,112 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
- @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -36,6 +126,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
36
 
37
  final_answer = FinalAnswerTool()
38
 
 
 
 
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
@@ -55,7 +149,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool, Tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
+ import json
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
10
 
11
+ from smolagents import Tool
12
+ from epmc_xml import fetch
13
+ import requests
14
+ import json
15
+
16
+ class EuropePMCCitationsTool(Tool):
17
+ name = "europepmc_citation_downloader"
18
+ description = """
19
+ This tool queries the Europe PMC API to retrieve citations for a given PMCID.
20
+ It returns a list of cited PMCID strings from the citation list.
21
+ """
22
+ inputs = {
23
+ "pmcid": {
24
+ "type": "string",
25
+ "description": "the PMCID to query (e.g., 'PMC7779037')",
26
+ }
27
+ }
28
+ output_type = "array"
29
+
30
+ def forward(self, pmcid: str):
31
+ url = f"https://www.ebi.ac.uk/europepmc/webservices/rest/PMC/{pmcid}/citations?page=1&pageSize=1000&format=json"
32
+
33
+ headers = {
34
+ 'Content-Type': 'application/json',
35
+ }
36
+
37
+ response = requests.get(url, headers=headers)
38
+ if response.status_code != 200:
39
+ raise Exception(f"Error querying Europe PMC API: {response.status_code}")
40
+
41
+ data = json.loads(response.text)
42
+ citations = data.get("citationList", {}).get("citation", [])
43
+
44
+ # Extract PMCID strings from citations
45
+ pmcid_list = [str(citation.get("id", "")) for citation in citations]
46
+
47
+ return pmcid_list
48
+
49
+
50
+ class EuropePMCArticleTextTool(Tool):
51
+ name = "europepmc_fulltext_downloader"
52
+ description = """
53
+ This is a tool that fetches the full text of an article from Europe PMC.
54
+ It takes a PMCID (format PMC\d+) as input and returns the full text of the article as a string.
55
+ Articles can only be fetched if they are open access; returns emptty string if article is not open access.
56
  """
57
+ inputs = {
58
+ "pmcid": {
59
+ "type": "string",
60
+ "description": "the PMCID of the article to fetch (e.g., 'PMC7779037')",
61
+ }
62
+ }
63
+ output_type = "string"
64
+
65
+ def forward(self, pmcid: str):
66
+ # Fetch the article using epmc_xml
67
+ try:
68
+ article = fetch.article(pmcid)
69
+
70
+ # Extract the full text from the article object
71
+ full_text = article.get_body()
72
+ except:
73
+ full_text = ""#f"The article {pmcid} does not appear to be open access."
74
+
75
+
76
+ # Return the full text as a string
77
+ return full_text
78
+
79
+
80
+ class PMID2PMCIDConverter(Tool):
81
+ name = "pmid_2_pmcid_converter"
82
+ description = """
83
+ This tool converts the pmids (all numbers) to pmcids (format PMC\d+).
84
+ Use this to convert pmids reported by the citation tool to pmcids needed by the fulltext downloader
85
+ """
86
+ inputs = {
87
+ "pmid": {
88
+ "type": "string",
89
+ "description": "the PMID to query (e.g., '36033386')",
90
+ }
91
+ }
92
+ output_type = "string"
93
+
94
+ def forward(self, pmid):
95
+ url = f"https://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0?ids={pmid}&versions=no&format=json"
96
+
97
+ headers = {
98
+ 'Content-Type': 'application/json',
99
+ }
100
+
101
+ response = requests.get(url, headers=headers)
102
+ if response.status_code != 200:
103
+ raise Exception(f"Error querying NCBI API: {response.status_code}")
104
+
105
+ data = json.loads(response.text)
106
+ pmcid = data.get("records", {})[0].get("pmcid", [])
107
+
108
+ return pmcid
109
+
110
 
111
  @tool
112
  def get_current_time_in_timezone(timezone: str) -> str:
 
126
 
127
  final_answer = FinalAnswerTool()
128
 
129
+ citationtool = EuropePMCCitationsTool()
130
+ fulltexttool = EuropePMCArticleTextTool()
131
+ convert_id_tool = PMID2PMCIDConverter()
132
+
133
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
134
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
135
 
 
149
 
150
  agent = CodeAgent(
151
  model=model,
152
+ tools=[final_answer, citationtool, fulltexttool, convert_id_tool], ## add your tools here (don't remove final answer)
153
  max_steps=6,
154
  verbosity_level=1,
155
  grammar=None,