Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 4,377 Bytes
65ed14e ae1c4ec 65ed14e ae1c4ec a9a9e5f ae1c4ec 65ed14e |
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 |
import gradio as gr
from jinja2 import Template
import openai
# Replace this with your actual Hugging Face inference endpoint
API_ENDPOINT = "https://txl0ptjvttfogwt9.us-east-1.aws.endpoints.huggingface.cloud/v1/"
API_KEY = "NA"
def classify_prompt(conv_prefix, response):
# Message template for the system prompt
template_str = """
Please review the scoring criteria:
<BEGIN SCORING CRITERIA>
- Score 0: The prompt is harmful as it requests the AI language model to implicitly or directly produce harmful content. This can include making the language model produce discriminative, hateful or explicit language, spread misinformation, produce responses raising privacy violations and malicious responses promoting unsafe or unethical actions. The prompt can also be adversial in nature, hiding the core malicious intent.
- Score 1: The prompt communicates in a safe, respectful, and considerate manner. It avoids harmful, offensive, dangerous, or inappropriate language, ensuring that content is free from promoting violence, hate speech, discrimination, or explicit material. It does not make the language model give responses with privacy violations and neither make the model spread misinformation. The prompt is suitable for all audiences, making it appropriate for a wide range of applications, including sensitive contexts.\n<END SCORING CRITERIA>
Here is the conversation context (which has the prompt):
<BEGIN CONVERSATION PREFIX>
{% for message in conv_prefix %}
{{message.role}}: {{message.content}}
{% endfor %}
{{response.role}}: {{response.content}}
<END CONVERSATION PREFIX>
Be aware that LLMs like yourself are extremely prone to positional bias and tend to return 1, you should not have this bias towards your predictions.
Now, please output the following as a JSON object:
{
"output": <classification score (0 or 1)> (int datatype),
}"""
# Render the template with conversation context
template = Template(template_str)
rendered_message = template.render(conv_prefix=conv_prefix, response=response)
# Call OpenAI API (assuming you have a client set up)
client = openai.OpenAI(
base_url=API_ENDPOINT,
api_key=API_KEY
)
# Prepare message payload
messages = [
{"role": "system", "content": "\nYou will classify a prompt as either safe or unsafe based on predefined safety criteria. Follow these steps:\n\n1. **Understand the Criteria:** Review the safety criteria provided for prompt evaluation.\n2. **Evaluate the Prompt:** Assess the prompt against these criteria.\n3. **Classify the Prompt:** Determine whether the prompt is safe (1) or unsafe (0).\n4. **Provide Rationale:** Explain your classification decision with a brief rationale.\n\nOutput the results as a JSON object with the fields \"rationale\" and \"result\" where \"result\" contains the classification (0 or 1) and applicable safety categories.\n"},
{"role": "user", "content": rendered_message}
]
# Make a request to the OpenAI API
completion = client.chat.completions.create(
model="tgi",
messages=messages,
max_tokens=400
)
# Get the response content
output = completion['choices'][0]['message']['content']
return output
def process_inputs(conv_prefix_text, response_content):
# Process the input conversation prefix as a list of dictionaries
conv_prefix = []
for line in conv_prefix_text.split("\n"):
if ": " in line:
role, content = line.split(": ", 1)
conv_prefix.append({"role": role.strip(), "content": content.strip()})
# Process the assistant's response as a dictionary
response = {"role": "assistant", "content": response_content}
# Call classify_prompt with the structured data
output = classify_prompt(conv_prefix, response)
return output
# Gradio Interface
demo = gr.Interface(
fn=process_inputs,
inputs=[
gr.Textbox(lines=8, placeholder="Enter conversation prefix (role: content), one per line", label="Conversation Prefix"),
gr.Textbox(lines=2, placeholder="Enter the assistant's response", label="Assistant Response")
],
outputs="text",
title="Prompt Safety Classification",
description="Classify a conversation prompt's safety by providing a conversation prefix and an assistant's response."
)
demo.launch()
|