wt002 commited on
Commit
e060a36
·
verified ·
1 Parent(s): c93c36d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py CHANGED
@@ -109,6 +109,130 @@ Answer:"""
109
 
110
 
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  #from smolagents import Tool
113
  #from langchain_community.document_loaders import WikipediaLoader
114
 
@@ -283,6 +407,12 @@ class BasicAgent:
283
 
284
  # ✅ New Llama Tool
285
  code_llama_tool = CodeLlamaTool()
 
 
 
 
 
 
286
 
287
  system_prompt = f"""
288
  You are my general AI assistant. Your task is to answer the question I asked.
@@ -301,6 +431,9 @@ If the answer is a comma-separated list, apply the above rules for each element
301
  keywords_extract_tool, speech_to_text_tool,
302
  visit_webpage_tool, final_answer_tool,
303
  parse_excel_to_json, video_transcription_tool,
 
 
 
304
  code_llama_tool # 🔧 Add here
305
  ],
306
  add_base_tools=True
 
109
 
110
 
111
 
112
+
113
+ import requests
114
+ from smolagents import Tool
115
+
116
+ class ArxivSearchTool(Tool):
117
+ name = "arxiv_search"
118
+ description = "Search Arxiv for papers matching a query and return titles and links."
119
+ inputs = {
120
+ "query": {"type": "string", "description": "Search query for Arxiv papers"}
121
+ }
122
+ output_type = "string"
123
+
124
+ def forward(self, query: str) -> str:
125
+ url = "http://export.arxiv.org/api/query"
126
+ params = {
127
+ "search_query": query,
128
+ "start": 0,
129
+ "max_results": 3,
130
+ "sortBy": "relevance",
131
+ "sortOrder": "descending"
132
+ }
133
+ try:
134
+ response = requests.get(url, params=params, timeout=10)
135
+ response.raise_for_status()
136
+ # Simple parse titles and links (basic, for demo)
137
+ import xml.etree.ElementTree as ET
138
+ root = ET.fromstring(response.content)
139
+ ns = {"atom": "http://www.w3.org/2005/Atom"}
140
+
141
+ entries = root.findall("atom:entry", ns)
142
+ results = []
143
+ for entry in entries:
144
+ title = entry.find("atom:title", ns).text.strip().replace('\n', ' ')
145
+ link = entry.find("atom:id", ns).text.strip()
146
+ results.append(f"{title}\n{link}")
147
+ return "\n\n".join(results) if results else "No results found."
148
+ except Exception as e:
149
+ return f"Error during Arxiv search: {e}"
150
+
151
+
152
+ from transformers import pipeline
153
+ from smolagents import Tool
154
+ from PIL import Image
155
+
156
+ class HuggingFaceDocumentQATool(Tool):
157
+ name = "document_qa"
158
+ description = "Answer questions from document images (e.g., scanned invoices)."
159
+ inputs = {
160
+ "image_path": {"type": "string", "description": "Path to the image file"},
161
+ "question": {"type": "string", "description": "Question to ask about the document"}
162
+ }
163
+ output_type = "string"
164
+
165
+ def __init__(self):
166
+ self.pipeline = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
167
+
168
+ def forward(self, image_path: str, question: str) -> str:
169
+ image = Image.open(image_path)
170
+ result = self.pipeline(image, question=question)
171
+ return result[0]['answer']
172
+
173
+
174
+ from transformers import BlipProcessor, BlipForQuestionAnswering
175
+
176
+ class HuggingFaceImageQATool(Tool):
177
+ name = "image_qa"
178
+ description = "Answer questions about an image."
179
+ inputs = {
180
+ "image_path": {"type": "string", "description": "Path to image"},
181
+ "question": {"type": "string", "description": "Question about the image"}
182
+ }
183
+ output_type = "string"
184
+
185
+ def __init__(self):
186
+ self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
187
+ self.model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
188
+
189
+ def forward(self, image_path: str, question: str) -> str:
190
+ image = Image.open(image_path)
191
+ inputs = self.processor(image, question, return_tensors="pt")
192
+ out = self.model.generate(**inputs)
193
+ return self.processor.decode(out[0], skip_special_tokens=True)
194
+
195
+
196
+ from transformers import pipeline
197
+
198
+ class HuggingFaceTranslationTool(Tool):
199
+ name = "translate"
200
+ description = "Translate text from English to another language."
201
+ inputs = {
202
+ "text": {"type": "string", "description": "Text to translate"}
203
+ }
204
+ output_type = "string"
205
+
206
+ def __init__(self):
207
+ self.translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
208
+
209
+ def forward(self, text: str) -> str:
210
+ return self.translator(text)[0]["translation_text"]
211
+
212
+
213
+ import io
214
+ import contextlib
215
+
216
+ class PythonCodeExecutionTool(Tool):
217
+ name = "run_python"
218
+ description = "Execute Python code and return result."
219
+ inputs = {
220
+ "code": {"type": "string", "description": "Python code to execute"}
221
+ }
222
+ output_type = "string"
223
+
224
+ def forward(self, code: str) -> str:
225
+ output = io.StringIO()
226
+ try:
227
+ with contextlib.redirect_stdout(output):
228
+ exec(code, {})
229
+ return output.getvalue().strip()
230
+ except Exception as e:
231
+ return f"Error: {str(e)}"
232
+
233
+
234
+
235
+
236
  #from smolagents import Tool
237
  #from langchain_community.document_loaders import WikipediaLoader
238
 
 
407
 
408
  # ✅ New Llama Tool
409
  code_llama_tool = CodeLlamaTool()
410
+ # ✅ Add Hugging Face default tools
411
+ arxiv_search_tool = ArxivSearchTool()
412
+ doc_qa_tool = HuggingFaceDocumentQATool()
413
+ image_qa_tool = HuggingFaceImageQATool()
414
+ translation_tool = HuggingFaceTranslationTool()
415
+ python_tool = PythonCodeExecutionTool()
416
 
417
  system_prompt = f"""
418
  You are my general AI assistant. Your task is to answer the question I asked.
 
431
  keywords_extract_tool, speech_to_text_tool,
432
  visit_webpage_tool, final_answer_tool,
433
  parse_excel_to_json, video_transcription_tool,
434
+ arxiv_search_tool,
435
+ doc_qa_tool, image_qa_tool,
436
+ translation_tool, python_tool,
437
  code_llama_tool # 🔧 Add here
438
  ],
439
  add_base_tools=True