Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

arxiv.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import random
  17. from abc import ABC
  18. from functools import partial
  19. import arxiv
  20. import pandas as pd
  21. from graph.settings import DEBUG
  22. from graph.component.base import ComponentBase, ComponentParamBase
  23. class ArXivParam(ComponentParamBase):
  24. """
  25. Define the ArXiv component parameters.
  26. """
  27. def __init__(self):
  28. super().__init__()
  29. self.top_n = 6
  30. self.sort_by = 'submittedDate'
  31. def check(self):
  32. self.check_positive_integer(self.top_n, "Top N")
  33. self.check_valid_value(self.sort_by, "ArXiv Search Sort_by",
  34. ['submittedDate', 'lastUpdatedDate', 'relevance'])
  35. class ArXiv(ComponentBase, ABC):
  36. component_name = "ArXiv"
  37. def _run(self, history, **kwargs):
  38. ans = self.get_input()
  39. ans = " - ".join(ans["content"]) if "content" in ans else ""
  40. if not ans:
  41. return ArXiv.be_output("")
  42. try:
  43. sort_choices = {"relevance": arxiv.SortCriterion.Relevance,
  44. "lastUpdatedDate": arxiv.SortCriterion.LastUpdatedDate,
  45. 'submittedDate': arxiv.SortCriterion.SubmittedDate}
  46. arxiv_client = arxiv.Client()
  47. search = arxiv.Search(
  48. query=ans,
  49. max_results=self._param.top_n,
  50. sort_by=sort_choices[self._param.sort_by]
  51. )
  52. arxiv_res = [
  53. {"content": 'Title: ' + i.title + '\nPdf_Url: <a href="' + i.pdf_url + '"></a> \nSummary: ' + i.summary} for
  54. i in list(arxiv_client.results(search))]
  55. except Exception as e:
  56. return ArXiv.be_output("**ERROR**: " + str(e))
  57. if not arxiv_res:
  58. return ArXiv.be_output("")
  59. df = pd.DataFrame(arxiv_res)
  60. if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
  61. return df