Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -64,13 +64,10 @@ class CodeLlamaTool(Tool):
|
|
64 |
|
65 |
def forward(self, question: str) -> str:
|
66 |
prompt = f"""You are an AI that uses Python code to answer questions.
|
67 |
-
|
68 |
Question: {question}
|
69 |
-
|
70 |
Instructions:
|
71 |
- If solving requires code, use a block like <tool>code</tool>.
|
72 |
- Always end with <final>FINAL ANSWER</final> containing the final number or string.
|
73 |
-
|
74 |
Example:
|
75 |
Question: What is 5 * sqrt(36)?
|
76 |
Answer:
|
@@ -79,7 +76,6 @@ import math
|
|
79 |
print(5 * math.sqrt(36))
|
80 |
</tool>
|
81 |
<final>30.0</final>
|
82 |
-
|
83 |
Answer:"""
|
84 |
|
85 |
response = self.pipeline(prompt)[0]["generated_text"]
|
@@ -112,152 +108,6 @@ Answer:"""
|
|
112 |
except Exception:
|
113 |
return f"Error executing code:\n{traceback.format_exc()}"
|
114 |
|
115 |
-
import requests
|
116 |
-
from smolagents import Tool
|
117 |
-
|
118 |
-
class ArxivSearchTool(Tool):
|
119 |
-
name = "arxiv_search"
|
120 |
-
description = "Search Arxiv for papers matching a query and return titles and links."
|
121 |
-
inputs = {
|
122 |
-
"query": {"type": "string", "description": "Search query for Arxiv papers"}
|
123 |
-
}
|
124 |
-
output_type = "string"
|
125 |
-
|
126 |
-
def forward(self, query: str) -> str:
|
127 |
-
url = "http://export.arxiv.org/api/query"
|
128 |
-
params = {
|
129 |
-
"search_query": query,
|
130 |
-
"start": 0,
|
131 |
-
"max_results": 3,
|
132 |
-
"sortBy": "relevance",
|
133 |
-
"sortOrder": "descending"
|
134 |
-
}
|
135 |
-
try:
|
136 |
-
response = requests.get(url, params=params, timeout=10)
|
137 |
-
response.raise_for_status()
|
138 |
-
# Simple parse titles and links (basic, for demo)
|
139 |
-
import xml.etree.ElementTree as ET
|
140 |
-
root = ET.fromstring(response.content)
|
141 |
-
ns = {"atom": "http://www.w3.org/2005/Atom"}
|
142 |
-
|
143 |
-
entries = root.findall("atom:entry", ns)
|
144 |
-
results = []
|
145 |
-
for entry in entries:
|
146 |
-
title = entry.find("atom:title", ns).text.strip().replace('\n', ' ')
|
147 |
-
link = entry.find("atom:id", ns).text.strip()
|
148 |
-
results.append(f"{title}\n{link}")
|
149 |
-
return "\n\n".join(results) if results else "No results found."
|
150 |
-
except Exception as e:
|
151 |
-
return f"Error during Arxiv search: {e}"
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
from transformers import pipeline
|
156 |
-
from smolagents import Tool
|
157 |
-
|
158 |
-
class TranslationTool(Tool):
|
159 |
-
name = "translate_text"
|
160 |
-
description = "Translate text from one language to another."
|
161 |
-
|
162 |
-
inputs = {
|
163 |
-
"text": {"type": "string", "description": "Text to translate"},
|
164 |
-
"src_lang": {"type": "string", "description": "Source language code"},
|
165 |
-
"tgt_lang": {"type": "string", "description": "Target language code"},
|
166 |
-
}
|
167 |
-
output_type = "string"
|
168 |
-
|
169 |
-
def __init__(self):
|
170 |
-
self.translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
171 |
-
|
172 |
-
def forward(self, text: str, src_lang: str, tgt_lang: str) -> str:
|
173 |
-
return self.translator(text)[0]["translation_text"]
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
from transformers import pipeline
|
178 |
-
from smolagents import Tool
|
179 |
-
from PIL import Image
|
180 |
-
|
181 |
-
class HuggingFaceDocumentQATool(Tool):
|
182 |
-
name = "document_qa"
|
183 |
-
description = "Answer questions from document images (e.g., scanned invoices)."
|
184 |
-
inputs = {
|
185 |
-
"image_path": {"type": "string", "description": "Path to the image file"},
|
186 |
-
"question": {"type": "string", "description": "Question to ask about the document"}
|
187 |
-
}
|
188 |
-
output_type = "string"
|
189 |
-
|
190 |
-
def __init__(self):
|
191 |
-
self.pipeline = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
|
192 |
-
|
193 |
-
def forward(self, image_path: str, question: str) -> str:
|
194 |
-
image = Image.open(image_path)
|
195 |
-
result = self.pipeline(image, question=question)
|
196 |
-
return result[0]['answer']
|
197 |
-
|
198 |
-
|
199 |
-
from transformers import BlipProcessor, BlipForQuestionAnswering
|
200 |
-
|
201 |
-
class HuggingFaceImageQATool(Tool):
|
202 |
-
name = "image_qa"
|
203 |
-
description = "Answer questions about an image."
|
204 |
-
inputs = {
|
205 |
-
"image_path": {"type": "string", "description": "Path to image"},
|
206 |
-
"question": {"type": "string", "description": "Question about the image"}
|
207 |
-
}
|
208 |
-
output_type = "string"
|
209 |
-
|
210 |
-
def __init__(self):
|
211 |
-
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
212 |
-
self.model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
213 |
-
|
214 |
-
def forward(self, image_path: str, question: str) -> str:
|
215 |
-
image = Image.open(image_path)
|
216 |
-
inputs = self.processor(image, question, return_tensors="pt")
|
217 |
-
out = self.model.generate(**inputs)
|
218 |
-
return self.processor.decode(out[0], skip_special_tokens=True)
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
from transformers import pipeline
|
223 |
-
|
224 |
-
class HuggingFaceTranslationTool(Tool):
|
225 |
-
name = "translate"
|
226 |
-
description = "Translate text from English to another language."
|
227 |
-
inputs = {
|
228 |
-
"text": {"type": "string", "description": "Text to translate"}
|
229 |
-
}
|
230 |
-
output_type = "string"
|
231 |
-
|
232 |
-
def __init__(self):
|
233 |
-
self.translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
234 |
-
|
235 |
-
def forward(self, text: str) -> str:
|
236 |
-
return self.translator(text)[0]["translation_text"]
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
import io
|
241 |
-
import contextlib
|
242 |
-
|
243 |
-
class PythonCodeExecutionTool(Tool):
|
244 |
-
name = "run_python"
|
245 |
-
description = "Execute Python code and return result."
|
246 |
-
inputs = {
|
247 |
-
"code": {"type": "string", "description": "Python code to execute"}
|
248 |
-
}
|
249 |
-
output_type = "string"
|
250 |
-
|
251 |
-
def forward(self, code: str) -> str:
|
252 |
-
output = io.StringIO()
|
253 |
-
try:
|
254 |
-
with contextlib.redirect_stdout(output):
|
255 |
-
exec(code, {})
|
256 |
-
return output.getvalue().strip()
|
257 |
-
except Exception as e:
|
258 |
-
return f"Error: {str(e)}"
|
259 |
-
|
260 |
-
|
261 |
|
262 |
|
263 |
#from smolagents import Tool
|
@@ -417,13 +267,12 @@ class VideoTranscriptionTool(Tool):
|
|
417 |
class BasicAgent:
|
418 |
def __init__(self):
|
419 |
token = os.environ.get("HF_API_TOKEN")
|
420 |
-
|
421 |
model = HfApiModel(
|
422 |
temperature=0.1,
|
423 |
token=token
|
424 |
)
|
425 |
|
426 |
-
#
|
427 |
search_tool = DuckDuckGoSearchTool()
|
428 |
wiki_search_tool = WikiSearchTool()
|
429 |
str_reverse_tool = StringReverseTool()
|
@@ -432,21 +281,20 @@ class BasicAgent:
|
|
432 |
visit_webpage_tool = VisitWebpageTool()
|
433 |
final_answer_tool = FinalAnswerTool()
|
434 |
video_transcription_tool = VideoTranscriptionTool()
|
435 |
-
code_llama_tool = CodeLlamaTool()
|
436 |
-
arxiv_search_tool = ArxivSearchTool()
|
437 |
|
438 |
-
# ✅
|
439 |
-
|
440 |
-
image_qa_tool = HuggingFaceImageQATool()
|
441 |
-
translation_tool = HuggingFaceTranslationTool()
|
442 |
-
python_tool = PythonCodeExecutionTool()
|
443 |
|
444 |
-
system_prompt = """
|
445 |
-
|
446 |
-
|
|
|
|
|
|
|
|
|
|
|
447 |
"""
|
448 |
|
449 |
-
|
450 |
self.agent = CodeAgent(
|
451 |
model=model,
|
452 |
tools=[
|
@@ -454,9 +302,6 @@ Always return your final result in the format:
|
|
454 |
keywords_extract_tool, speech_to_text_tool,
|
455 |
visit_webpage_tool, final_answer_tool,
|
456 |
parse_excel_to_json, video_transcription_tool,
|
457 |
-
arxiv_search_tool,
|
458 |
-
doc_qa_tool, image_qa_tool,
|
459 |
-
translation_tool, python_tool,
|
460 |
code_llama_tool # 🔧 Add here
|
461 |
],
|
462 |
add_base_tools=True
|
|
|
64 |
|
65 |
def forward(self, question: str) -> str:
|
66 |
prompt = f"""You are an AI that uses Python code to answer questions.
|
|
|
67 |
Question: {question}
|
|
|
68 |
Instructions:
|
69 |
- If solving requires code, use a block like <tool>code</tool>.
|
70 |
- Always end with <final>FINAL ANSWER</final> containing the final number or string.
|
|
|
71 |
Example:
|
72 |
Question: What is 5 * sqrt(36)?
|
73 |
Answer:
|
|
|
76 |
print(5 * math.sqrt(36))
|
77 |
</tool>
|
78 |
<final>30.0</final>
|
|
|
79 |
Answer:"""
|
80 |
|
81 |
response = self.pipeline(prompt)[0]["generated_text"]
|
|
|
108 |
except Exception:
|
109 |
return f"Error executing code:\n{traceback.format_exc()}"
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
|
113 |
#from smolagents import Tool
|
|
|
267 |
class BasicAgent:
|
268 |
def __init__(self):
|
269 |
token = os.environ.get("HF_API_TOKEN")
|
|
|
270 |
model = HfApiModel(
|
271 |
temperature=0.1,
|
272 |
token=token
|
273 |
)
|
274 |
|
275 |
+
# Existing tools
|
276 |
search_tool = DuckDuckGoSearchTool()
|
277 |
wiki_search_tool = WikiSearchTool()
|
278 |
str_reverse_tool = StringReverseTool()
|
|
|
281 |
visit_webpage_tool = VisitWebpageTool()
|
282 |
final_answer_tool = FinalAnswerTool()
|
283 |
video_transcription_tool = VideoTranscriptionTool()
|
|
|
|
|
284 |
|
285 |
+
# ✅ New Llama Tool
|
286 |
+
code_llama_tool = CodeLlamaTool()
|
|
|
|
|
|
|
287 |
|
288 |
+
system_prompt = f"""
|
289 |
+
You are my general AI assistant. Your task is to answer the question I asked.
|
290 |
+
First, provide an explanation of your reasoning, step by step, to arrive at the answer.
|
291 |
+
Then, return your final answer in a single line, formatted as follows: "FINAL ANSWER: [YOUR FINAL ANSWER]".
|
292 |
+
[YOUR FINAL ANSWER] should be a number, a string, or a comma-separated list of numbers and/or strings, depending on the question.
|
293 |
+
If the answer is a number, do not use commas or units (e.g., $, %) unless specified.
|
294 |
+
If the answer is a string, do not use articles or abbreviations (e.g., for cities), and write digits in plain text unless specified.
|
295 |
+
If the answer is a comma-separated list, apply the above rules for each element based on whether it is a number or a string.
|
296 |
"""
|
297 |
|
|
|
298 |
self.agent = CodeAgent(
|
299 |
model=model,
|
300 |
tools=[
|
|
|
302 |
keywords_extract_tool, speech_to_text_tool,
|
303 |
visit_webpage_tool, final_answer_tool,
|
304 |
parse_excel_to_json, video_transcription_tool,
|
|
|
|
|
|
|
305 |
code_llama_tool # 🔧 Add here
|
306 |
],
|
307 |
add_base_tools=True
|