Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -113,12 +113,112 @@ Answer:"""
|
|
113 |
return f"Error executing code:\n{traceback.format_exc()}"
|
114 |
|
115 |
|
116 |
-
from transformers
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
|
124 |
#from smolagents import Tool
|
@@ -297,10 +397,10 @@ class BasicAgent:
|
|
297 |
arxiv_search_tool = ArxivSearchTool()
|
298 |
|
299 |
# ✅ Add Hugging Face default tools
|
300 |
-
doc_qa_tool =
|
301 |
-
image_qa_tool =
|
302 |
-
translation_tool =
|
303 |
-
python_tool =
|
304 |
|
305 |
system_prompt = """You are my general AI assistant...
|
306 |
Always return your final result in the format:
|
|
|
113 |
return f"Error executing code:\n{traceback.format_exc()}"
|
114 |
|
115 |
|
116 |
+
from transformers import pipeline
|
117 |
+
from smolagents import Tool
|
118 |
+
|
119 |
+
class TranslationTool(Tool):
|
120 |
+
name = "translate_text"
|
121 |
+
description = "Translate text from one language to another."
|
122 |
+
|
123 |
+
inputs = {
|
124 |
+
"text": {"type": "string", "description": "Text to translate"},
|
125 |
+
"src_lang": {"type": "string", "description": "Source language code"},
|
126 |
+
"tgt_lang": {"type": "string", "description": "Target language code"},
|
127 |
+
}
|
128 |
+
output_type = "string"
|
129 |
+
|
130 |
+
def __init__(self):
|
131 |
+
self.translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
132 |
+
|
133 |
+
def forward(self, text: str, src_lang: str, tgt_lang: str) -> str:
|
134 |
+
return self.translator(text)[0]["translation_text"]
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
from transformers import pipeline
|
139 |
+
from smolagents import Tool
|
140 |
+
from PIL import Image
|
141 |
+
|
142 |
+
class HuggingFaceDocumentQATool(Tool):
|
143 |
+
name = "document_qa"
|
144 |
+
description = "Answer questions from document images (e.g., scanned invoices)."
|
145 |
+
inputs = {
|
146 |
+
"image_path": {"type": "string", "description": "Path to the image file"},
|
147 |
+
"question": {"type": "string", "description": "Question to ask about the document"}
|
148 |
+
}
|
149 |
+
output_type = "string"
|
150 |
+
|
151 |
+
def __init__(self):
|
152 |
+
self.pipeline = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
|
153 |
+
|
154 |
+
def forward(self, image_path: str, question: str) -> str:
|
155 |
+
image = Image.open(image_path)
|
156 |
+
result = self.pipeline(image, question=question)
|
157 |
+
return result[0]['answer']
|
158 |
+
|
159 |
+
|
160 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering
|
161 |
+
|
162 |
+
class HuggingFaceImageQATool(Tool):
|
163 |
+
name = "image_qa"
|
164 |
+
description = "Answer questions about an image."
|
165 |
+
inputs = {
|
166 |
+
"image_path": {"type": "string", "description": "Path to image"},
|
167 |
+
"question": {"type": "string", "description": "Question about the image"}
|
168 |
+
}
|
169 |
+
output_type = "string"
|
170 |
+
|
171 |
+
def __init__(self):
|
172 |
+
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
173 |
+
self.model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
174 |
+
|
175 |
+
def forward(self, image_path: str, question: str) -> str:
|
176 |
+
image = Image.open(image_path)
|
177 |
+
inputs = self.processor(image, question, return_tensors="pt")
|
178 |
+
out = self.model.generate(**inputs)
|
179 |
+
return self.processor.decode(out[0], skip_special_tokens=True)
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
from transformers import pipeline
|
184 |
+
|
185 |
+
class HuggingFaceTranslationTool(Tool):
|
186 |
+
name = "translate"
|
187 |
+
description = "Translate text from English to another language."
|
188 |
+
inputs = {
|
189 |
+
"text": {"type": "string", "description": "Text to translate"}
|
190 |
+
}
|
191 |
+
output_type = "string"
|
192 |
+
|
193 |
+
def __init__(self):
|
194 |
+
self.translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
195 |
+
|
196 |
+
def forward(self, text: str) -> str:
|
197 |
+
return self.translator(text)[0]["translation_text"]
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
import io
|
202 |
+
import contextlib
|
203 |
+
|
204 |
+
class PythonCodeExecutionTool(Tool):
|
205 |
+
name = "run_python"
|
206 |
+
description = "Execute Python code and return result."
|
207 |
+
inputs = {
|
208 |
+
"code": {"type": "string", "description": "Python code to execute"}
|
209 |
+
}
|
210 |
+
output_type = "string"
|
211 |
+
|
212 |
+
def forward(self, code: str) -> str:
|
213 |
+
output = io.StringIO()
|
214 |
+
try:
|
215 |
+
with contextlib.redirect_stdout(output):
|
216 |
+
exec(code, {})
|
217 |
+
return output.getvalue().strip()
|
218 |
+
except Exception as e:
|
219 |
+
return f"Error: {str(e)}"
|
220 |
+
|
221 |
+
|
222 |
|
223 |
|
224 |
#from smolagents import Tool
|
|
|
397 |
arxiv_search_tool = ArxivSearchTool()
|
398 |
|
399 |
# ✅ Add Hugging Face default tools
|
400 |
+
doc_qa_tool = HuggingFaceDocumentQATool()
|
401 |
+
image_qa_tool = HuggingFaceImageQATool()
|
402 |
+
translation_tool = HuggingFaceTranslationTool()
|
403 |
+
python_tool = PythonCodeExecutionTool()
|
404 |
|
405 |
system_prompt = """You are my general AI assistant...
|
406 |
Always return your final result in the format:
|