File size: 1,813 Bytes
11bd448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from abc import ABC, abstractmethod

import markdown
from bs4 import BeautifulSoup, Comment


class ComplianceCheck(ABC):
    @abstractmethod
    def run_check(self, card: BeautifulSoup) -> bool:
        raise NotImplementedError


class ModelProviderIdentityCheck(ComplianceCheck):
    def run_check(self, card: BeautifulSoup):
        try:
            model_description = card.find("h3", string="Model Description")
            description_list = model_description.find_next_siblings()[0]
            developer = description_list.find(string="Developed by:").parent.next_sibling.strip()

            if developer == "[More Information Needed]":
                return False, None

            return True, developer
        except AttributeError:
            return False, None


class IntendedPurposeCheck(ComplianceCheck):
    def run_check(self, card: BeautifulSoup):
        try:
            direct_use = card.find("h3", string="Direct Use")

            direct_use_content = ""

            sibling_gen = direct_use.nextSiblingGenerator()
            sibling = next(sibling_gen)

            while sibling.name != "h3":
                if not isinstance(sibling, Comment):
                    direct_use_content = direct_use_content + sibling.text
                sibling = next(sibling_gen)

            if direct_use_content.strip() == "[More Information Needed]":
                return False, None

            return True, None
        except AttributeError:
            return False, None


class ComplianceSuite:
    def __init__(self, checks):
        self.checks = checks

    def run(self, model_card):
        model_card_html = markdown.markdown(model_card)
        card_soup = BeautifulSoup(model_card_html, features="html.parser")

        return [c.run_check(card_soup) for c in self.checks]