Spaces:
Sleeping
Sleeping
add guardrail llm
Browse files- app.py +80 -21
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,6 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
llm = Llama.from_pretrained(
|
6 |
repo_id="amir22010/fine_tuned_product_marketing_email_gemma_2_9b_q4_k_m",
|
@@ -11,6 +17,9 @@ llm = Llama.from_pretrained(
|
|
11 |
verbose=False
|
12 |
)
|
13 |
|
|
|
|
|
|
|
14 |
#marketing prompt
|
15 |
marketing_email_prompt = """Below is a product and description, please write a marketing email for this product.
|
16 |
|
@@ -23,32 +32,82 @@ marketing_email_prompt = """Below is a product and description, please write a m
|
|
23 |
### Marketing Email:
|
24 |
{}"""
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
{
|
30 |
"role": "system",
|
31 |
-
"content": "Your
|
32 |
},
|
33 |
-
{"role": "user", "content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
product, # product
|
35 |
description, # description
|
36 |
"", # output - leave this blank for generation!
|
37 |
-
)},
|
38 |
-
],
|
39 |
-
# response_format={
|
40 |
-
# "type": "json_object",
|
41 |
-
# },
|
42 |
-
max_tokens=4096,
|
43 |
-
temperature=0.7,
|
44 |
-
stream=True
|
45 |
)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
import os
|
4 |
+
import asyncio
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
client = Groq(
|
8 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
9 |
+
)
|
10 |
|
11 |
llm = Llama.from_pretrained(
|
12 |
repo_id="amir22010/fine_tuned_product_marketing_email_gemma_2_9b_q4_k_m",
|
|
|
17 |
verbose=False
|
18 |
)
|
19 |
|
20 |
+
#guardrail model
|
21 |
+
guard_llm = "llama-guard-3-8b"
|
22 |
+
|
23 |
#marketing prompt
|
24 |
marketing_email_prompt = """Below is a product and description, please write a marketing email for this product.
|
25 |
|
|
|
32 |
### Marketing Email:
|
33 |
{}"""
|
34 |
|
35 |
+
#gaurdrails prompt
|
36 |
+
guardrail_prompt = """You're given a list of moderation categories as below:
|
37 |
+
|
38 |
+
- illegal: Illegal activity.
|
39 |
+
- child abuse: child sexual abuse material or any content that exploits or harms children.
|
40 |
+
- hate violence harassment: Generation of hateful, harassing, or violent content: content that expresses, incites, or promotes hate based on identity, content that intends to harass, threaten, or bully an individual, content that promotes or glorifies violence or celebrates the suffering or humiliation of others.
|
41 |
+
- malware: Generation of malware: content that attempts to generate code that is designed to disrupt, damage, or gain unauthorized access to a computer system.
|
42 |
+
- physical harm: activity that has high risk of physical harm, including: weapons development, military and warfare, management or operation of critical infrastructure in energy, transportation, and water, content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.
|
43 |
+
- economic harm: activity that has high risk of economic harm, including: multi-level marketing, gambling, payday lending, automated determinations of eligibility for credit, employment, educational institutions, or public assistance services.
|
44 |
+
- fraud: Fraudulent or deceptive activity, including: scams, coordinated inauthentic behavior, plagiarism, academic dishonesty, astroturfing, such as fake grassroots support or fake review generation, disinformation, spam, pseudo-pharmaceuticals.
|
45 |
+
- adult: Adult content, adult industries, and dating apps, including: content meant to arouse sexual excitement, such as the description of sexual activity, or that promotes sexual services (excluding sex education and wellness), erotic chat, pornography.
|
46 |
+
- political: Political campaigning or lobbying, by: generating high volumes of campaign materials, generating campaign materials personalized to or targeted at specific demographics, building conversational or interactive systems such as chatbots that provide information about campaigns or engage in political advocacy or lobbying, building products for political campaigning or lobbying purposes.
|
47 |
+
- privacy: Activity that violates people's privacy, including: tracking or monitoring an individual without their consent, facial recognition of private individuals, classifying individuals based on protected characteristics, using biometrics for identification or assessment, unlawful collection or disclosure of personal identifiable information or educational, financial, or other protected records.
|
48 |
+
- unqualified law: Engaging in the unauthorized practice of law, or offering tailored legal advice without a qualified person reviewing the information.
|
49 |
+
- unqualified financial: Offering tailored financial advice without a qualified person reviewing the information.
|
50 |
+
- unqualified health: Telling someone that they have or do not have a certain health condition, or providing instructions on how to cure or treat a health condition.
|
51 |
+
|
52 |
+
Please classify the following user prompt into one of these categories, and answer with that single word only.
|
53 |
+
|
54 |
+
If the user prompt does not fall within these categories, is safe and does not need to be moderated, please answer "not moderated".
|
55 |
+
|
56 |
+
user prompt: {}
|
57 |
+
"""
|
58 |
+
|
59 |
+
async def topical_guardrail(user_request):
|
60 |
+
"""
|
61 |
+
checking provided user content authenticity
|
62 |
+
"""
|
63 |
+
messages = [
|
64 |
{
|
65 |
"role": "system",
|
66 |
+
"content": "Your role is to assess whether the user prompt is moderate or not.",
|
67 |
},
|
68 |
+
{"role": "user", "content": guardrail_prompt.format(user_request)},
|
69 |
+
]
|
70 |
+
response = client.chat.completions.create(
|
71 |
+
model=guard_llm, messages=messages, temperature=0
|
72 |
+
)
|
73 |
+
return response.choices[0].message.content
|
74 |
+
|
75 |
+
async def greet(product,description):
|
76 |
+
user_request = marketing_email_prompt.format(
|
77 |
product, # product
|
78 |
description, # description
|
79 |
"", # output - leave this blank for generation!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
)
|
81 |
+
topical_guardrail_task = asyncio.create_task(topical_guardrail(user_request))
|
82 |
+
while True:
|
83 |
+
done, _ = await asyncio.wait(
|
84 |
+
[topical_guardrail_task], return_when=asyncio.FIRST_COMPLETED
|
85 |
+
)
|
86 |
+
if topical_guardrail_task in done:
|
87 |
+
guardrail_response = topical_guardrail_task.result()
|
88 |
+
if guardrail_response != "not moderated":
|
89 |
+
yield "Sorry can't proceed for generate marketing email!. Your content needs to be moderated first."
|
90 |
+
else:
|
91 |
+
output = llm.create_chat_completion(
|
92 |
+
messages=[
|
93 |
+
{
|
94 |
+
"role": "system",
|
95 |
+
"content": "Your go-to Email Marketing Guru - I'm here to help you craft compelling campaigns, boost conversions, and take your business to the next level.",
|
96 |
+
},
|
97 |
+
{"role": "user", "content": user_request},
|
98 |
+
],
|
99 |
+
max_tokens=4096,
|
100 |
+
temperature=0.7,
|
101 |
+
stream=True
|
102 |
+
)
|
103 |
+
partial_message = ""
|
104 |
+
for chunk in output:
|
105 |
+
delta = chunk['choices'][0]['delta']
|
106 |
+
if 'content' in delta:
|
107 |
+
partial_message = partial_message + delta.get('content', '')
|
108 |
+
yield partial_message
|
109 |
+
else:
|
110 |
+
await asyncio.sleep(0.1) # sleep for a bit before checking the tasks again
|
111 |
+
|
112 |
+
demo = gr.Interface(fn=await greet, inputs=["text","text"], outputs="text")
|
113 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
torch
|
2 |
huggingface-hub
|
3 |
-
llama-cpp-python
|
|
|
|
1 |
torch
|
2 |
huggingface-hub
|
3 |
+
llama-cpp-python
|
4 |
+
groq
|