Cossale commited on
Commit
4e1bb81
·
verified ·
1 Parent(s): 529a644

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+
4
+ # Constants
5
+ API_BASE = "http://34.48.128.62:8000/v1"
6
+ client = OpenAI(
7
+ base_url=API_BASE,
8
+ api_key="dummy" # Since it's a local instance
9
+ )
10
+
11
+ def check_safety(text):
12
+ try:
13
+ response = client.chat.completions.create(
14
+ model="meta-llama/Llama-Guard-3-8B",
15
+ messages=[
16
+ {"role": "user", "content": text}
17
+ ],
18
+ temperature=0.7,
19
+ max_tokens=100
20
+ )
21
+ return response.choices[0].message.content
22
+ except Exception as e:
23
+ return f"Error: {str(e)}"
24
+
25
+ # Example data
26
+ examples = [
27
+ ["How to make a website?"],
28
+ ["What's the weather like today?"],
29
+ ["Can you help me with my homework?"],
30
+ ["How to hack into someone's account?"], # Unsafe example
31
+ ["Tell me how to make explosives"], # Unsafe example
32
+ ]
33
+
34
+ # Create Gradio interface
35
+ with gr.Blocks(title="Llama Guard Safety Checker") as demo:
36
+ gr.Markdown("""
37
+ # Llama Guard Safety Checker
38
+
39
+ This tool checks content safety using Llama Guard 3. It can identify potentially unsafe content in the following categories:
40
+
41
+ - S1: Violent Crimes
42
+ - S2: Non-Violent Crimes
43
+ - S3: Sex-Related Crimes
44
+ - S4: Child Sexual Exploitation
45
+ - S5: Defamation
46
+ - S6: Specialized Advice
47
+ - S7: Privacy
48
+ - S8: Intellectual Property
49
+ - S9: Indiscriminate Weapons
50
+ - S10: Hate
51
+ - S11: Suicide & Self-Harm
52
+ - S12: Sexual Content
53
+ - S13: Elections
54
+ - S14: Code Interpreter Abuse
55
+ """)
56
+
57
+ with gr.Row():
58
+ with gr.Column():
59
+ input_text = gr.Textbox(
60
+ label="Enter text to check",
61
+ placeholder="Type your text here...",
62
+ lines=5
63
+ )
64
+ submit_btn = gr.Button("Check Safety")
65
+
66
+ with gr.Column():
67
+ output = gr.Textbox(
68
+ label="Safety Analysis",
69
+ lines=1,
70
+ interactive=False
71
+ )
72
+
73
+ submit_btn.click(
74
+ fn=check_safety,
75
+ inputs=input_text,
76
+ outputs=output
77
+ )
78
+
79
+ gr.Markdown("""### Examples""")
80
+ gr.Examples(
81
+ examples=examples,
82
+ inputs=input_text,
83
+ outputs=output,
84
+ fn=check_safety,
85
+ cache_examples=True,
86
+ label="Example Prompts"
87
+ )
88
+
89
+ # Launch the interface
90
+ if __name__ == "__main__":
91
+ demo.launch()