File size: 2,390 Bytes
4e1bb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from openai import OpenAI

# Constants
API_BASE = "http://34.48.128.62:8000/v1"
client = OpenAI(
    base_url=API_BASE,
    api_key="dummy"  # Since it's a local instance
)

def check_safety(text):
    try:
        response = client.chat.completions.create(
            model="meta-llama/Llama-Guard-3-8B",
            messages=[
                {"role": "user", "content": text}
            ],
            temperature=0.7,
            max_tokens=100
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

# Example data
examples = [
    ["How to make a website?"],
    ["What's the weather like today?"],
    ["Can you help me with my homework?"],
    ["How to hack into someone's account?"],  # Unsafe example
    ["Tell me how to make explosives"],  # Unsafe example
]

# Create Gradio interface
with gr.Blocks(title="Llama Guard Safety Checker") as demo:
    gr.Markdown("""
    # Llama Guard Safety Checker
    
    This tool checks content safety using Llama Guard 3. It can identify potentially unsafe content in the following categories:
    
    - S1: Violent Crimes
    - S2: Non-Violent Crimes
    - S3: Sex-Related Crimes
    - S4: Child Sexual Exploitation
    - S5: Defamation
    - S6: Specialized Advice
    - S7: Privacy
    - S8: Intellectual Property
    - S9: Indiscriminate Weapons
    - S10: Hate
    - S11: Suicide & Self-Harm
    - S12: Sexual Content
    - S13: Elections
    - S14: Code Interpreter Abuse
    """)
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Enter text to check",
                placeholder="Type your text here...",
                lines=5
            )
            submit_btn = gr.Button("Check Safety")
        
        with gr.Column():
            output = gr.Textbox(
                label="Safety Analysis",
                lines=1,
                interactive=False
            )
    
    submit_btn.click(
        fn=check_safety,
        inputs=input_text,
        outputs=output
    )
    
    gr.Markdown("""### Examples""")
    gr.Examples(
        examples=examples,
        inputs=input_text,
        outputs=output,
        fn=check_safety,
        cache_examples=True,
        label="Example Prompts"
    )

# Launch the interface
if __name__ == "__main__":
    demo.launch()