Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -368,54 +368,6 @@ def reverse_text(text: str) -> str:
|
|
368 |
return text[::-1].replace("\\", "").strip() if text.startswith(('.', ',')) else text
|
369 |
|
370 |
# 3. Unified Document Creation
|
371 |
-
def create_documents(data_source: str, data: list) -> list:
|
372 |
-
"""Handle both Gradio chat and JSON questions"""
|
373 |
-
docs = []
|
374 |
-
|
375 |
-
for item in data:
|
376 |
-
content = ""
|
377 |
-
# Process different data sources
|
378 |
-
if data_source == "json":
|
379 |
-
raw_question = item.get("question", "")
|
380 |
-
content = raw_question # Adjust as per your content processing logic
|
381 |
-
else:
|
382 |
-
print(f"Skipping invalid data source: {data_source}")
|
383 |
-
continue
|
384 |
-
|
385 |
-
# Ensure metadata type safety
|
386 |
-
metadata = {
|
387 |
-
"task_id": str(item.get("task_id", "")),
|
388 |
-
"level": str(item.get("Level", "")),
|
389 |
-
"file_name": str(item.get("file_name", ""))
|
390 |
-
}
|
391 |
-
|
392 |
-
# Check if content is non-empty
|
393 |
-
if content.strip(): # Only append non-empty content
|
394 |
-
docs.append(Document(page_content=content, metadata=metadata))
|
395 |
-
else:
|
396 |
-
print(f"Skipping invalid entry with empty content: {item}")
|
397 |
-
|
398 |
-
return docs
|
399 |
-
|
400 |
-
# Path to your data.json
|
401 |
-
file_path = "/home/wendy/Downloads/data.json"
|
402 |
-
|
403 |
-
def load_data(file_path: str) -> list[dict]:
|
404 |
-
"""Safe JSON data loading with error handling"""
|
405 |
-
if not os.path.exists(file_path):
|
406 |
-
raise FileNotFoundError(f"Data file not found: {file_path}")
|
407 |
-
|
408 |
-
if not file_path.endswith('.json'):
|
409 |
-
raise ValueError("Invalid file format. Only JSON files supported")
|
410 |
-
|
411 |
-
try:
|
412 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
413 |
-
return json.load(f)
|
414 |
-
except json.JSONDecodeError:
|
415 |
-
raise ValueError("Invalid JSON format in data file")
|
416 |
-
except Exception as e:
|
417 |
-
raise RuntimeError(f"Error loading data: {str(e)}")
|
418 |
-
|
419 |
|
420 |
|
421 |
|
@@ -439,19 +391,7 @@ class MyVector_Store:
|
|
439 |
# -----------------------------
|
440 |
# Process JSON data and create documents
|
441 |
# -----------------------------
|
442 |
-
|
443 |
-
|
444 |
-
try:
|
445 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
446 |
-
data = json.load(f)
|
447 |
-
print(data)
|
448 |
-
except FileNotFoundError as e:
|
449 |
-
print(f"Error: {e}")
|
450 |
-
except json.JSONDecodeError as e:
|
451 |
-
print(f"Error decoding JSON: {e}")
|
452 |
-
|
453 |
-
docs = create_documents("json", data)
|
454 |
-
texts = [doc.page_content for doc in docs]
|
455 |
|
456 |
# -----------------------------
|
457 |
# Initialize embedding model
|
|
|
368 |
return text[::-1].replace("\\", "").strip() if text.startswith(('.', ',')) else text
|
369 |
|
370 |
# 3. Unified Document Creation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
|
372 |
|
373 |
|
|
|
391 |
# -----------------------------
|
392 |
# Process JSON data and create documents
|
393 |
# -----------------------------
|
394 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
395 |
|
396 |
# -----------------------------
|
397 |
# Initialize embedding model
|