Spaces:
Sleeping
Sleeping
Update src/agent.py
Browse files- src/agent.py +14 -0
src/agent.py
CHANGED
|
@@ -99,6 +99,20 @@ class BasicAgent():
|
|
| 99 |
if file_path.endswith(".txt"):
|
| 100 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 101 |
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
elif file_path.endswith(".docx"):
|
| 103 |
doc = docx.Document(file_path)
|
| 104 |
return "\n".join([p.text for p in doc.paragraphs])
|
|
|
|
| 99 |
if file_path.endswith(".txt"):
|
| 100 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 101 |
return f.read()
|
| 102 |
+
elif file_path.endswith(".csv"):
|
| 103 |
+
data = []
|
| 104 |
+
try:
|
| 105 |
+
# Use csv.DictReader to read the file into a list of dictionaries
|
| 106 |
+
with open(file_path, mode='r', newline='', encoding='utf-8') as file:
|
| 107 |
+
reader = csv.DictReader(file)
|
| 108 |
+
for row in reader:
|
| 109 |
+
data.append(row)
|
| 110 |
+
|
| 111 |
+
# Return the structured data as a JSON string
|
| 112 |
+
return json.dumps(data, indent=2)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Error reading CSV file: {e}")
|
| 115 |
+
return ""
|
| 116 |
elif file_path.endswith(".docx"):
|
| 117 |
doc = docx.Document(file_path)
|
| 118 |
return "\n".join([p.text for p in doc.paragraphs])
|