Skip to content

Commit 0580267

Browse files
authored
Merge pull request #200 from shijinpjlab/dev_1009
feat: update ModelRes and add img rule
2 parents f8502c5 + 433bec1 commit 0580267

File tree

8 files changed

+169
-10
lines changed

8 files changed

+169
-10
lines changed

dingo/exec/local.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,30 @@ def evaluate_rule(self, group: List[BaseRule], d: Data) -> ResultInfo:
238238
# analyze result
239239
if tmp.error_status:
240240
result_info.error_status = True
241-
bad_type_list.append(tmp.type)
242-
bad_name_list.append(tmp.type + "-" + tmp.name)
241+
if isinstance(tmp.type, str) and isinstance(tmp.name, str):
242+
bad_type_list.append(tmp.type)
243+
bad_name_list.append(tmp.type + "-" + tmp.name)
244+
elif isinstance(tmp.type, List) and isinstance(tmp.name, List):
245+
if len(tmp.type) != len(tmp.name):
246+
raise Exception(f'ModelRes.type is not the same length to ModelRes.name.\n type: {tmp.type} \n name: {tmp.name}')
247+
for i in range(len(tmp.type)):
248+
bad_type_list.append(tmp.type[i])
249+
bad_name_list.append(tmp.type[i] + "-" + tmp.name[i])
250+
else:
251+
raise Exception('ModelRes.type and ModelRes.name are not str or List at the same time.')
243252
bad_reason_list.extend(tmp.reason)
244253
else:
245-
good_type_list.append(tmp.type)
246-
good_name_list.append(tmp.type + "-" + tmp.name)
254+
if isinstance(tmp.type, str) and isinstance(tmp.name, str):
255+
good_type_list.append(tmp.type)
256+
good_name_list.append(tmp.type + "-" + tmp.name)
257+
elif isinstance(tmp.type, List) and isinstance(tmp.name, List):
258+
if len(tmp.type) != len(tmp.name):
259+
raise Exception(f'ModelRes.type is not the same length to ModelRes.name.\n type: {tmp.type} \n name: {tmp.name}')
260+
for i in range(len(tmp.type)):
261+
good_type_list.append(tmp.type[i])
262+
good_name_list.append(tmp.type[i] + "-" + tmp.name[i])
263+
else:
264+
raise Exception('ModelRes.type and ModelRes.name are not str or List at the same time.')
247265
good_reason_list.extend(tmp.reason)
248266
if result_info.error_status:
249267
result_info.type_list = list(set(bad_type_list))
@@ -271,12 +289,32 @@ def evaluate_prompt(self, group: List[BasePrompt], d: Data) -> ResultInfo:
271289
# analyze result
272290
if tmp.error_status:
273291
result_info.error_status = True
274-
bad_type_list.append(tmp.type)
275-
bad_name_list.append(tmp.type + "-" + tmp.name)
292+
if isinstance(tmp.type, str) and isinstance(tmp.name, str):
293+
bad_type_list.append(tmp.type)
294+
bad_name_list.append(tmp.type + "-" + tmp.name)
295+
elif isinstance(tmp.type, List) and isinstance(tmp.name, List):
296+
if len(tmp.type) != len(tmp.name):
297+
raise Exception(
298+
f'ModelRes.type is not the same length to ModelRes.name.\n type: {tmp.type} \n name: {tmp.name}')
299+
for i in range(len(tmp.type)):
300+
bad_type_list.append(tmp.type[i])
301+
bad_name_list.append(tmp.type[i] + "-" + tmp.name[i])
302+
else:
303+
raise Exception('ModelRes.type and ModelRes.name are not str or List at the same time.')
276304
bad_reason_list.extend(tmp.reason)
277305
else:
278-
good_type_list.append(tmp.type)
279-
good_name_list.append(tmp.type + "-" + tmp.name)
306+
if isinstance(tmp.type, str) and isinstance(tmp.name, str):
307+
good_type_list.append(tmp.type)
308+
good_name_list.append(tmp.type + "-" + tmp.name)
309+
elif isinstance(tmp.type, List) and isinstance(tmp.name, List):
310+
if len(tmp.type) != len(tmp.name):
311+
raise Exception(
312+
f'ModelRes.type is not the same length to ModelRes.name.\n type: {tmp.type} \n name: {tmp.name}')
313+
for i in range(len(tmp.type)):
314+
good_type_list.append(tmp.type[i])
315+
good_name_list.append(tmp.type[i] + "-" + tmp.name[i])
316+
else:
317+
raise Exception('ModelRes.type and ModelRes.name are not str or List at the same time.')
280318
good_reason_list.extend(tmp.reason)
281319
if result_info.error_status:
282320
result_info.type_list = list(set(bad_type_list))

dingo/model/modelres.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
class ModelRes(BaseModel):
77
error_status: bool = False
8-
type: str = "QUALITY_GOOD"
9-
name: str = "Data"
8+
type: str | List[str] = "QUALITY_GOOD"
9+
name: str | List[str] = "Data"
1010
reason: List[str] = []
1111

1212
# Optional fields for enhanced functionality (e.g., hallucination detection)

dingo/model/rule/rule_image.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,46 @@ def eval(cls, input_data: Data) -> ModelRes:
312312
)
313313

314314

315+
@Model.rule_register("QUALITY_BAD_IMG_LABEL_OVERLAP", [])
316+
class RuleImageLabelOverlap(BaseRule):
317+
_metric_info = {
318+
"category": "Rule-Based IMG Quality Metrics",
319+
"quality_dimension": "IMG_LABEL_OVERLAP",
320+
"metric_name": "RuleImageLabelOverlap",
321+
"description": "", # TODO: lindong input
322+
"paper_title": "",
323+
"paper_url": "",
324+
"paper_authors": "",
325+
"evaluation_results": ""
326+
}
327+
328+
dynamic_config = EvaluatorRuleArgs()
329+
330+
@classmethod
331+
def eval(cls, input_data: Data) -> ModelRes:
332+
pass
333+
334+
335+
@Model.rule_register("QUALITY_BAD_IMG_LABEL_VISUALIZATION", [])
336+
class RuleImageLabelVisualization(BaseRule):
337+
_metric_info = {
338+
"category": "Rule-Based IMG Quality Metrics",
339+
"quality_dimension": "IMG_LABEL_VISUALIZATION",
340+
"metric_name": "RuleImageLabelVisualization",
341+
"description": "", # TODO: lindong input
342+
"paper_title": "",
343+
"paper_url": "",
344+
"paper_authors": "",
345+
"evaluation_results": ""
346+
}
347+
348+
dynamic_config = EvaluatorRuleArgs()
349+
350+
@classmethod
351+
def eval(cls, input_data: Data) -> ModelRes:
352+
pass
353+
354+
315355
if __name__ == "__main__":
316356
data = Data(
317357
data_id='1',
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from dingo.config import InputArgs
2+
from dingo.exec import Executor
3+
4+
5+
def image_label_overlap():
6+
input_data = {
7+
"input_path": "../../test/data/img_label/test_img_label_overlap.jsonl",
8+
"dataset": {
9+
"source": "local",
10+
"format": "image",
11+
"field": {
12+
"id": "id",
13+
"content": "content",
14+
"image": "img"
15+
}
16+
},
17+
"executor": {
18+
"rule_list": ["RuleImageLabelOverlap"],
19+
"result_save": {
20+
"bad": True,
21+
"good": True
22+
}
23+
}
24+
}
25+
input_args = InputArgs(**input_data)
26+
executor = Executor.exec_map["local"](input_args)
27+
result = executor.execute()
28+
print(result)
29+
30+
31+
if __name__ == '__main__':
32+
image_label_overlap()

test/data/img_label/overlap_0.jpg

1.57 MB
Loading

test/data/img_label/overlap_1.jpg

1.21 MB
Loading

test/data/img_label/test_img_label_overlap.jsonl

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import re
3+
from typing import List
4+
5+
from dingo.config.input_args import EvaluatorRuleArgs
6+
from dingo.io import Data
7+
from dingo.model.model import Model
8+
from dingo.model.modelres import ModelRes
9+
from dingo.model.rule.base import BaseRule
10+
11+
12+
@Model.rule_register('QUALITY_BAD_RELEVANCE', ['test'])
13+
class RegisterRuleColon(BaseRule):
14+
"""let user input pattern to search"""
15+
dynamic_config = EvaluatorRuleArgs(pattern = "blue")
16+
17+
@classmethod
18+
def eval(cls, input_data: Data) -> ModelRes:
19+
res = ModelRes()
20+
content = input_data.content
21+
if len(content) <= 0:
22+
return res
23+
if content[-1] == ":":
24+
res.error_status = True
25+
res.type = [cls.metric_type, 'TestType']
26+
res.name = [cls.__name__, 'TestName']
27+
res.reason = [content[-100:]]
28+
return res
29+
30+
31+
class TestModelRes:
32+
def test_type_name_list(self):
33+
34+
data = Data(
35+
data_id='0',
36+
prompt="",
37+
content="Hello! The world is a vast and diverse place, full of wonders, cultures, and incredible natural beauty:"
38+
)
39+
40+
res = RegisterRuleColon().eval(data)
41+
# print(res)
42+
assert isinstance(res.type, List)
43+
assert isinstance(res.name, List)
44+
assert len(res.type) == 2
45+
assert len(res.name) == 2
46+
assert 'TestType' in res.type
47+
assert 'TestName' in res.name

0 commit comments

Comments
 (0)