Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,15 +18,16 @@ Does the human question violate the above policy? Your answer must start with 'Y
|
|
18 |
return prompt
|
19 |
|
20 |
def verificar_politica(message):
|
21 |
-
policies =
|
22 |
-
"No Dangerous Content: The prompt shall not contain or seek generation of content that harms oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide).",
|
23 |
-
"No Harassment: The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).",
|
24 |
-
"No Hate Speech: The prompt shall not contain or seek generation of content that targets identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying based on race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status.",
|
25 |
-
"No Sexually Explicit Information: The prompt shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted."
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
|
|
30 |
response = llm.create_chat_completion(
|
31 |
messages=[{"role": "system", "content": prompt}],
|
32 |
stream=False
|
@@ -34,15 +35,23 @@ def verificar_politica(message):
|
|
34 |
answer = response['choices'][0]['message']['content']
|
35 |
|
36 |
# Calcula las probabilidades de "Yes" y "No" bas谩ndote en la respuesta generada
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
return None
|
47 |
|
48 |
def respond(message, history, system_prompt):
|
@@ -79,7 +88,26 @@ def chat_stream_completion(message, history, system_prompt):
|
|
79 |
message_repl += chunk['choices'][0]["delta"]["content"]
|
80 |
yield message_repl
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return prompt
|
19 |
|
20 |
def verificar_politica(message):
|
21 |
+
policies = {
|
22 |
+
"No Dangerous Content": "The prompt shall not contain or seek generation of content that harms oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide).",
|
23 |
+
"No Harassment": "The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).",
|
24 |
+
"No Hate Speech": "The prompt shall not contain or seek generation of content that targets identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying based on race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status.",
|
25 |
+
"No Sexually Explicit Information": "The prompt shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted."
|
26 |
+
}
|
27 |
|
28 |
+
scores = {}
|
29 |
+
for policy_name, policy_text in policies.items():
|
30 |
+
prompt = generar_prompt(message, policy_text)
|
31 |
response = llm.create_chat_completion(
|
32 |
messages=[{"role": "system", "content": prompt}],
|
33 |
stream=False
|
|
|
35 |
answer = response['choices'][0]['message']['content']
|
36 |
|
37 |
# Calcula las probabilidades de "Yes" y "No" bas谩ndote en la respuesta generada
|
38 |
+
yes_count = answer.lower().count("yes")
|
39 |
+
no_count = answer.lower().count("no")
|
40 |
+
total_count = yes_count + no_count
|
41 |
+
|
42 |
+
if total_count == 0:
|
43 |
+
yes_probability = 0
|
44 |
+
else:
|
45 |
+
yes_probability = yes_count / total_count
|
46 |
+
|
47 |
+
scores[policy_name] = yes_probability
|
48 |
+
print(f"Policy: {policy_name}")
|
49 |
+
print(f"Score: {yes_probability:.2f}")
|
50 |
|
51 |
+
# Determina si alguna pol铆tica tiene una puntuaci贸n alta
|
52 |
+
for policy_name, score in scores.items():
|
53 |
+
if score > 0.5:
|
54 |
+
return f"Your question violates the following policy: {policy_name} with a score of {score:.2f}"
|
55 |
return None
|
56 |
|
57 |
def respond(message, history, system_prompt):
|
|
|
88 |
message_repl += chunk['choices'][0]["delta"]["content"]
|
89 |
yield message_repl
|
90 |
|
91 |
+
# Interfaz de Gradio con la descripci贸n y configuraci贸n adicional
|
92 |
+
with gr.Blocks() as demo:
|
93 |
+
gr.Markdown("# Child-Safe-Chatbot (Experimental)")
|
94 |
+
gr.Markdown("""
|
95 |
+
### Description
|
96 |
+
This chatbot is designed to assist users while ensuring that all interactions comply with defined safety policies. It checks whether user inputs violate any of the following categories:
|
97 |
+
|
98 |
+
- Dangerous Content
|
99 |
+
- Harassment
|
100 |
+
- Hate Speech
|
101 |
+
- Sexually Explicit Information
|
102 |
+
|
103 |
+
The chatbot will inform the user if any violation occurs and, if not, will proceed to respond to the user's message in a friendly manner.
|
104 |
+
|
105 |
+
I'm Norberto Mart铆n Afonso. Follow me on [Twitter](https://twitter.com/norbertomartnaf) and [GitHub](https://github.com/nmarafo) for more updates and projects!
|
106 |
+
""")
|
107 |
+
|
108 |
+
gr.ChatInterface(
|
109 |
+
chat_stream_completion,
|
110 |
+
additional_inputs=[gr.Textbox("You are a helpful AI.", label="System Prompt")]
|
111 |
+
).queue().launch(server_name="0.0.0.0")
|
112 |
+
|
113 |
+
demo.launch(debug=True)
|