File size: 1,258 Bytes
cf84747
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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}")