File size: 5,769 Bytes
23d6dd1
8c910e7
 
f4bc135
56af1aa
23d6dd1
c48ebf9
71aa590
56af1aa
23a71da
c48ebf9
f4bc135
 
 
 
 
d03b0e0
 
56af1aa
 
d03b0e0
40fb589
 
 
66f1683
40fb589
 
d03b0e0
40fb589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d03b0e0
40fb589
 
 
 
 
 
 
 
 
d03b0e0
5feef48
40fb589
 
d03b0e0
 
 
 
 
 
 
 
 
 
 
 
 
 
56af1aa
d03b0e0
 
 
 
 
23d6dd1
d03b0e0
fcc9f7a
 
d03b0e0
 
 
 
a7963ea
d03b0e0
a7963ea
d03b0e0
 
 
 
 
 
 
 
 
8256398
56af1aa
 
 
 
8c910e7
d03b0e0
 
8c910e7
c48ebf9
 
8c910e7
 
d03b0e0
8c910e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
922abfa
8c910e7
 
 
 
 
dade5ab
8c910e7
 
56af1aa
 
 
8c910e7
 
 
d03b0e0
 
 
 
8c910e7
e9a074f
8c910e7
23d6dd1
c48ebf9
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
163
164
import gradio as gr
import requests
import os
import json
import re

# Configure the endpoint and authentication
ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://aqxz70rjxilvoxwd.us-east-1.aws.endpoints.huggingface.cloud")
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip()


def is_token_configured():
    if not HF_API_TOKEN:
        return "⚠️ Warning: HF_API_TOKEN is not configured. The app won't work until you add this secret in your Space settings."
    return "βœ… API token is configured"

class SafetyChecker:
    def __init__(self):
        self.ENDPOINT_URL = ENDPOINT_URL
        self.HF_API_TOKEN = HF_API_TOKEN

    # def extract_and_parse_json(self, response: str):
    #     match = re.search(r'```(?:json)?\s*(.*?)\s*```', response, re.DOTALL)
    #     content = match.group(1).strip() if match else response.strip()
        
    #     if not content.startswith("{") and ":" in content:
    #         content = "{" + content + "}"
        
    #     try:
    #         parsed = json.loads(content)
    #     except json.JSONDecodeError:
    #         cleaned = content.replace(""", "\"").replace(""", "\"").replace("'", "\"")
    #         cleaned = re.sub(r',\s*}', '}', cleaned)
    #         cleaned = re.sub(r',\s*]', ']', cleaned)
    #         try:
    #             parsed = json.loads(cleaned)
    #         except Exception:
    #             pairs = re.findall(r'"([^"]+)":\s*"?([^",\{\}\[\]]+)"?', content)
    #             if pairs:
    #                 parsed = {k.strip(): v.strip() for k, v in pairs}
    #             else:
    #                 parsed = {
    #                     "Safety": "",
    #                     "Score": "",
    #                     "Unsafe Categories": "",
    #                 }
    #     return parsed

    def extract_and_parse_json(self, response: str):
        if response.startswith("```"):
            response = response.strip("`").strip()
            if response.startswith("json"):
                response = response[4:].strip()
    
        # Now response should be clean JSON text
        try:
            parsed = json.loads(response)
        except Exception:
            # If somehow still not JSON, return default empty
            parsed = {
                "Safety": "",
                "Score": "",
                "Unsafe Categories": [],
            }
        
        return parsed

    

    def check_safety(self, input_text):
        if not input_text.strip():
            return "⚠️ Please enter some text to check."
        
        payload = {"inputs": input_text}
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.HF_API_TOKEN}"
        }
        
        try:
            response = requests.post(self.ENDPOINT_URL, json=payload, headers=headers, timeout=30)

            if response.status_code == 200:
                result_raw = response.json()
                
                if isinstance(result_raw, str):
                    parsed_result = self.extract_and_parse_json(result_raw)
                else:
                    parsed_result = result_raw

                safety = parsed_result.get("Safety", "Unknown")
                # score = parsed_result.get("Score", "")
                # categories = parsed_result.get("Unsafe Categories", "")

                is_safe = (safety.lower() == "safe")

                if is_safe:
                    return f"βœ… {result_raw}"
                else:
                    return f"❌ {result_raw}"
            else:
                return f"❗ Error: Request failed with status code {response.status_code}.\nDetails: {response.text}"

        except requests.exceptions.Timeout:
            return "❗ Error: Request timed out."
        except requests.exceptions.ConnectionError:
            return "❗ Error: Failed to connect to the endpoint."
        except Exception as e:
            return f"❗ Error: {str(e)}"

# Create an instance of SafetyChecker
checker = SafetyChecker()

# Build Gradio App
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
    gr.Markdown("# Safety Content Classifier")
    gr.Markdown("## Connected to external safety model endpoint")
    
    token_status = gr.Markdown(is_token_configured())
    
    with gr.Accordion("About this demo", open=False):
        gr.Markdown("""
        This demo uses an external API endpoint to classify text based on safety policies.
        It checks content against the following categories:
        - Harassment
        - Dangerous Content
        - Hate Speech
        - Sexually Explicit Information
        
        The model will respond with 'Safe' or 'Unsafe' followed by any violated categories.
        """)
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Enter text to check",
                placeholder="Type here...",
                lines=5
            )

            check_button = gr.Button("Check Safety", variant="primary")
        
        with gr.Column():
            output = gr.Textbox(
                label="Safety Result",
                lines=8
            )
    
    # Notice: Now we use checker.check_safety instead of check_safety directly
    check_button.click(fn=checker.check_safety, inputs=[input_text], outputs=output)
    input_text.submit(fn=checker.check_safety, inputs=[input_text], outputs=output)
    
    gr.Examples(
        [
            ["Hello, how are you today?"],
            ["I want to learn how to make a bomb."],
            ["Let's meet for coffee tomorrow."],
            ["COVID vaccines are a secret plot by the government."],
        ],
        inputs=[input_text]
    )

demo.launch()