File size: 5,718 Bytes
7f9dfed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import annotations

import csv
import tempfile
import unittest
from pathlib import Path

from datasets.field_notes import FieldNote, FieldNoteStore, SQLiteFieldNoteStore


class FieldNoteStoreTest(unittest.TestCase):
    def test_save_writes_correction_row(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "field_notes.csv"
            store = FieldNoteStore(path)

            saved_path = store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="What plant is this?",
                    response="Unknown plant",
                    correction="It is basil.",
                    tags="plant-id,demo",
                    image_path="images/basil.jpg",
                    video_path="",
                    use_for_training=True,
                )
            )

            self.assertEqual(saved_path, path)
            with path.open(encoding="utf-8", newline="") as f:
                rows = list(csv.DictReader(f))

            self.assertEqual(len(rows), 1)
            self.assertEqual(rows[0]["model_id"], "minicpm5_1b")
            self.assertEqual(rows[0]["correction"], "It is basil.")
            self.assertEqual(rows[0]["image_path"], "images/basil.jpg")
            self.assertEqual(rows[0]["use_for_training"], "True")

    def test_created_at_uses_utc_offset_without_python_311_utc_import(self) -> None:
        note = FieldNote.create(
            model_id="minicpm5_1b",
            prompt="Prompt",
            response="Response",
            correction="Correction",
            tags="demo",
        )

        self.assertTrue(note.created_at.endswith("+00:00"))

    def test_exports_corrected_notes_to_jsonl(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "field_notes.csv"
            output = Path(tmp) / "field_notes.jsonl"
            store = FieldNoteStore(path)
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt",
                    response="Response",
                    correction="Correction",
                    tags="demo",
                )
            )
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt 2",
                    response="Response 2",
                    correction="",
                    tags="demo",
                )
            )
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt 3",
                    response="Response 3",
                    correction="Correction 3",
                    tags="demo",
                    use_for_training=False,
                )
            )

            exported = store.export_jsonl(output)

            self.assertEqual(exported, output)
            lines = output.read_text(encoding="utf-8").splitlines()
            self.assertEqual(len(lines), 1)
            self.assertIn("Correction", lines[0])

    def test_filters_notes_by_tag_and_training_flag(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            store = FieldNoteStore(Path(tmp) / "field_notes.csv")
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt",
                    response="Response",
                    correction="Correction",
                    tags="ocr,plant-id",
                    use_for_training=True,
                )
            )
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt 2",
                    response="Response 2",
                    correction="Correction 2",
                    tags="audio",
                    use_for_training=False,
                )
            )

            self.assertEqual(len(store.list_notes(tag="plant-id")), 1)
            self.assertEqual(len(store.list_notes(training_only=True)), 1)

    def test_exports_local_hf_dataset_directory(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            store = FieldNoteStore(Path(tmp) / "field_notes.csv")
            target = Path(tmp) / "hf_dataset"
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt",
                    response="Response",
                    correction="Correction",
                    tags="demo",
                )
            )

            exported = store.export_hf_dataset(target)

            self.assertEqual(exported, target)
            self.assertTrue((target / "data.jsonl").exists())
            self.assertTrue((target / "README.md").exists())

    def test_sqlite_store_saves_and_lists_notes(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            store = SQLiteFieldNoteStore(Path(tmp) / "field_notes.sqlite")
            store.save(
                FieldNote.create(
                    model_id="minicpm5_1b",
                    prompt="Prompt",
                    response="Response",
                    correction="Correction",
                    tags="demo",
                    image_path="image.png",
                    use_for_training=True,
                )
            )

            notes = store.list_notes(corrected_only=True, tag="demo", training_only=True)

            self.assertEqual(len(notes), 1)
            self.assertEqual(notes[0].image_path, "image.png")


if __name__ == "__main__":
    unittest.main()