Mohammad Haghir commited on
Commit ·
5f1c9ec
1
Parent(s): 6829302
file handling
Browse files- agent_utils.py +22 -9
- requirements.txt +2 -1
agent_utils.py
CHANGED
|
@@ -6,7 +6,7 @@ from tavily import TavilyClient
|
|
| 6 |
import requests
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
-
|
| 10 |
|
| 11 |
def wiki_ret(question: str) -> str:
|
| 12 |
""" Retrieve docs from wikipedia """
|
|
@@ -68,7 +68,6 @@ def tavily_ret(query: str, max_results: int = 3) -> str:
|
|
| 68 |
|
| 69 |
|
| 70 |
def handle_file_tool(input: dict) -> str:
|
| 71 |
-
"""Processes file attached to a question"""
|
| 72 |
question = input.get("question", "")
|
| 73 |
file_url = input.get("file", "")
|
| 74 |
|
|
@@ -79,16 +78,30 @@ def handle_file_tool(input: dict) -> str:
|
|
| 79 |
if not response.ok:
|
| 80 |
return f"Failed to download file: {file_url}"
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
image = Image.open(BytesIO(response.content))
|
| 85 |
-
return f"
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
elif
|
| 89 |
with open("temp.pdf", "wb") as f:
|
| 90 |
f.write(response.content)
|
| 91 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
else:
|
| 94 |
-
return f"Unsupported file type: {
|
|
|
|
|
|
| 6 |
import requests
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
+
import pandas as pd
|
| 10 |
|
| 11 |
def wiki_ret(question: str) -> str:
|
| 12 |
""" Retrieve docs from wikipedia """
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
def handle_file_tool(input: dict) -> str:
|
|
|
|
| 71 |
question = input.get("question", "")
|
| 72 |
file_url = input.get("file", "")
|
| 73 |
|
|
|
|
| 78 |
if not response.ok:
|
| 79 |
return f"Failed to download file: {file_url}"
|
| 80 |
|
| 81 |
+
file_ext = file_url.split("?")[0].split(".")[-1].lower()
|
| 82 |
+
|
| 83 |
+
# IMAGE
|
| 84 |
+
if file_ext in ("png", "jpg", "jpeg"):
|
| 85 |
image = Image.open(BytesIO(response.content))
|
| 86 |
+
return f"Image file received for question: '{question}'. (Insert vision model here.)"
|
| 87 |
|
| 88 |
+
# PDF
|
| 89 |
+
elif file_ext == "pdf":
|
| 90 |
with open("temp.pdf", "wb") as f:
|
| 91 |
f.write(response.content)
|
| 92 |
+
return f"PDF file received. (Insert PDF parsing logic here.)"
|
| 93 |
+
|
| 94 |
+
# EXCEL
|
| 95 |
+
elif file_ext in ("xlsx", "xls"):
|
| 96 |
+
df = pd.read_excel(BytesIO(response.content))
|
| 97 |
+
summary = f"Excel data preview:\n{df.head().to_string(index=False)}"
|
| 98 |
+
return f"Question: {question}\n{summary}"
|
| 99 |
+
|
| 100 |
+
# PYTHON CODE
|
| 101 |
+
elif file_ext == "py":
|
| 102 |
+
code = response.content.decode("utf-8")
|
| 103 |
+
return f"Question: {question}\nPython code received:\n{code[:500]}..." # Limit preview
|
| 104 |
|
| 105 |
else:
|
| 106 |
+
return f"Unsupported file type: .{file_ext}"
|
| 107 |
+
|
requirements.txt
CHANGED
|
@@ -7,4 +7,5 @@ wikipedia
|
|
| 7 |
langgraph
|
| 8 |
arxiv
|
| 9 |
tavily-python
|
| 10 |
-
Pillow
|
|
|
|
|
|
| 7 |
langgraph
|
| 8 |
arxiv
|
| 9 |
tavily-python
|
| 10 |
+
Pillow
|
| 11 |
+
pandas
|