nmarafo commited on
Commit
bd39fcf
verified
1 Parent(s): 3edafbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -4
app.py CHANGED
@@ -7,8 +7,19 @@ LLAMA_CPP_EXECUTABLE = "/llama.cpp/main"
7
  # Ruta al modelo GGUF descargado
8
  MODEL_PATH = "/app/shieldgemma-9b.Q4_K_M.gguf"
9
 
10
- # Funci贸n para ejecutar llama.cpp y obtener una respuesta
11
- def llama_cpp_generate(prompt):
 
 
 
 
 
 
 
 
 
 
 
12
  command = [
13
  LLAMA_CPP_EXECUTABLE,
14
  "--model", MODEL_PATH,
@@ -16,14 +27,34 @@ def llama_cpp_generate(prompt):
16
  "--temp", "0.7",
17
  "--top_p", "0.95",
18
  "--repeat_penalty", "1.1",
19
- "--n_predict", "256"
20
  ]
21
 
22
  result = subprocess.run(command, capture_output=True, text=True)
23
  return result.stdout.strip()
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def respond(message):
26
- assistant_prompt = f"You are a friendly assistant.\nUser: {message}\nAssistant:"
 
 
 
 
27
  response = llama_cpp_generate(assistant_prompt)
28
  return response
29
 
@@ -51,3 +82,4 @@ with gr.Blocks() as demo:
51
  )
52
 
53
  demo.launch(debug=True)
 
 
7
  # Ruta al modelo GGUF descargado
8
  MODEL_PATH = "/app/shieldgemma-9b.Q4_K_M.gguf"
9
 
10
+ system_message = "You are a friendly Chatbot."
11
+
12
+ def generar_prompt(user_prompt, policy):
13
+ prompt = f"""You are a policy expert trying to help determine whether a user prompt is in violation of the following safety policy:
14
+ {policy}
15
+ <start_of_turn>
16
+ Human Question: {user_prompt.strip()}
17
+ <end_of_turn>
18
+ Does the human question violate the above policy? Your answer must start with 'Yes' or 'No'. And then walk through step by step to be sure we answer correctly.
19
+ """
20
+ return prompt
21
+
22
+ def llama_cpp_generate(prompt, max_tokens=256):
23
  command = [
24
  LLAMA_CPP_EXECUTABLE,
25
  "--model", MODEL_PATH,
 
27
  "--temp", "0.7",
28
  "--top_p", "0.95",
29
  "--repeat_penalty", "1.1",
30
+ "--n_predict", str(max_tokens)
31
  ]
32
 
33
  result = subprocess.run(command, capture_output=True, text=True)
34
  return result.stdout.strip()
35
 
36
+ def verificar_politica(message):
37
+ policies = [
38
+ "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).",
39
+ "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).",
40
+ "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.",
41
+ "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."
42
+ ]
43
+
44
+ for policy in policies:
45
+ prompt = generar_prompt(message, policy)
46
+ response = llama_cpp_generate(prompt)
47
+ # Revisar si la respuesta comienza con "Yes" para indicar una violaci贸n
48
+ if response.startswith("Yes"):
49
+ return f"Your question violates the following policy: {policy.split(':')[0]}"
50
+ return None
51
+
52
  def respond(message):
53
+ policy_violation = verificar_politica(message)
54
+ if policy_violation:
55
+ return policy_violation
56
+
57
+ assistant_prompt = f"{system_message}\nUser: {message}\nAssistant:"
58
  response = llama_cpp_generate(assistant_prompt)
59
  return response
60
 
 
82
  )
83
 
84
  demo.launch(debug=True)
85
+