ocr-llm-test / save_results.py
winamnd's picture
Rename save_data.py to save_results.py
cf84747 verified
import os
import json
import csv
def save_results_to_repo(text, label, repo_path="./wnmnd/ocr-llm-test"):
data = {"text": text, "label": label}
try:
# Ensure the repository exists
if not os.path.exists(repo_path):
os.makedirs(repo_path)
print(f"Folder created at: {repo_path}")
# Define the full file paths for JSON and CSV
results_json = os.path.join(repo_path, "ocr_results.json")
results_csv = os.path.join(repo_path, "ocr_results.csv")
# Save to JSON
if not os.path.exists(results_json):
with open(results_json, "w") as f:
json.dump([], f)
with open(results_json, "r+") as f:
content = json.load(f)
content.append(data)
f.seek(0)
json.dump(content, f, indent=4)
# Save to CSV
file_exists = os.path.exists(results_csv)
with open(results_csv, "a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["text", "label"])
if not file_exists:
writer.writeheader()
writer.writerow(data)
print(f"Results saved: {data}")
except Exception as e:
print(f"Error saving results: {e}")