urameez commited on
Commit
7038772
·
verified ·
1 Parent(s): 8c5c24b

added tools

Browse files
Files changed (1) hide show
  1. app.py +136 -8
app.py CHANGED
@@ -7,16 +7,144 @@ 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:
@@ -55,7 +183,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,
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
+
11
+ @tool
12
+ def get_top_daily_paper() -> str:
13
+ """
14
+ Retrieves the current top upvoted paper from Hugging Face Daily Papers.
15
+ Returns:
16
+ str: The title and URL of the paper.
17
+ """
18
+ try:
19
+ import requests
20
+ from bs4 import BeautifulSoup
21
+ url = "https://huggingface.co/papers"
22
+ resp = requests.get(url, timeout=10)
23
+ resp.raise_for_status()
24
+ soup = BeautifulSoup(resp.content, "html.parser")
25
+
26
+ # Find the first article (top paper by upvotes)
27
+ paper = soup.select_one("article")
28
+ if not paper:
29
+ return "🔍 Error: No papers found on the page"
30
+
31
+ # Find the title and link within the h3 > a structure
32
+ title_element = paper.select_one("h3 a")
33
+ if not title_element:
34
+ return "🔍 Error: Could not find paper title"
35
+
36
+ title = title_element.get_text(strip=True)
37
+ link = title_element.get("href")
38
+
39
+ if not link:
40
+ return "🔍 Error: Could not find paper link"
41
+
42
+ full_url = f"https://huggingface.co{link}"
43
+ return f"Top Daily Paper: {title} — {full_url}"
44
+ except Exception as e:
45
+ return f"🔍 Error fetching top paper: {str(e)}"
46
+
47
+ @tool
48
+ def get_paper_abstract(paper_url: str) -> str:
49
+ """
50
+ Retrieves the abstract from a Hugging Face paper page.
51
+ Args:
52
+ paper_url: The URL of the paper page
53
+ Returns:
54
+ str: The paper abstract including AI summary if available
55
+ """
56
+ try:
57
+ import requests
58
+ from bs4 import BeautifulSoup
59
+
60
+ resp = requests.get(paper_url, timeout=10)
61
+ resp.raise_for_status()
62
+ soup = BeautifulSoup(resp.content, "html.parser")
63
+
64
+ # Find the abstract section
65
+ abstract_section = soup.find("h2", string="Abstract")
66
+ if not abstract_section:
67
+ return "🔍 Error: Abstract section not found"
68
+
69
+ # Get the parent container of the abstract
70
+ abstract_container = abstract_section.find_next_sibling("div")
71
+ if not abstract_container:
72
+ return "🔍 Error: Abstract content not found"
73
+
74
+ result_parts = []
75
+
76
+ # Look for AI-generated summary (blue box)
77
+ ai_summary = abstract_container.select_one(".bg-blue-500\\/6 p")
78
+ if ai_summary:
79
+ summary_text = ai_summary.get_text(strip=True)
80
+ result_parts.append(f"🤖 AI Summary: {summary_text}")
81
+
82
+ # Get the main abstract text
83
+ main_abstract = abstract_container.select_one("p.text-gray-600")
84
+ if main_abstract:
85
+ # Clean up the text by removing link artifacts and extra spaces
86
+ abstract_text = ""
87
+ for element in main_abstract.descendants:
88
+ if element.name is None: # Text node
89
+ abstract_text += element.strip() + " "
90
+
91
+ abstract_text = " ".join(abstract_text.split()) # Normalize whitespace
92
+ result_parts.append(f"📄 Abstract: {abstract_text}")
93
+
94
+ if not result_parts:
95
+ return "🔍 Error: No abstract content found"
96
+
97
+ return "\n\n".join(result_parts)
98
+
99
+ except Exception as e:
100
+ return f"🔍 Error fetching abstract: {str(e)}"
101
+
102
  @tool
103
+ def summarize_text(text: str, max_sentences: int = 3, model_name: str = "google/pegasus-cnn_dailymail") -> str:
104
+ """
105
+ Summarize a body of text using a Hugging Face Transformers pipeline.
106
+
107
  Args:
108
+ text: The text to be summarized.
109
+ max_sentences: Approximate upper limit for the number of sentences in the output.
110
+ model_name: The Hugging Face model to use for summarization (default is a CNN/DailyMail–fine‑tuned Pegasus).
111
+
112
+ Returns:
113
+ A concise summary string, or an error message.
114
  """
115
+ try:
116
+ from transformers import pipeline
117
+
118
+ # Load summarization pipeline once (could be optimized by caching)
119
+ summarizer = pipeline("summarization", model=model_name)
120
+
121
+ # Heuristically chunk long text into manageable parts for the model
122
+ max_chunk = 1024 # tokens; varies by model
123
+ # Naive chunking, splitting on sentences or whitespace:
124
+ chunks = [text[i:i + max_chunk] for i in range(0, len(text), max_chunk)]
125
+
126
+ # Summarize each chunk
127
+ summaries = []
128
+ for chunk in chunks:
129
+ out = summarizer(chunk,
130
+ max_length=max_sentences * 20,
131
+ min_length=max_sentences * 10,
132
+ do_sample=False)
133
+ summaries.append(out[0]['summary_text'])
134
+
135
+ # Combine chunk-level summaries, optionally resummarize
136
+ combined = " ".join(summaries)
137
+ if len(chunks) > 1:
138
+ final = summarizer(combined,
139
+ max_length=max_sentences * 20,
140
+ min_length=max_sentences * 10,
141
+ do_sample=False)[0]['summary_text']
142
+ return final
143
+ else:
144
+ return summaries[0]
145
+
146
+ except Exception as e:
147
+ return f"Error during summarization: {e}"
148
 
149
  @tool
150
  def get_current_time_in_timezone(timezone: str) -> str:
 
183
 
184
  agent = CodeAgent(
185
  model=model,
186
+ tools=[final_answer,get_current_time_in_timezone,get_top_daily_paper,get_paper_abstract,summarize_text], ## add your tools here (don't remove final answer)
187
  max_steps=6,
188
  verbosity_level=1,
189
  grammar=None,