Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,21 @@ SYSTEM_PROMPT_SPAM_FILTER = "\n" + read_file("system_prompt_spam_filter.txt")
|
|
| 12 |
SYSTEM_PROMPT_FEEDBACK = "\n" + read_file("system_prompt_feedback.txt")
|
| 13 |
TARGET_OPTIONS_DIR = "target_options"
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def get_target_options():
|
| 16 |
options = []
|
| 17 |
for filename in os.listdir(TARGET_OPTIONS_DIR):
|
|
@@ -26,7 +41,7 @@ def get_target_prompt(option_name):
|
|
| 26 |
return read_file(file_path)
|
| 27 |
return ""
|
| 28 |
|
| 29 |
-
def compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback, user_message):
|
| 30 |
length_instruction = (
|
| 31 |
"\nPlease keep your rewritten message about the same length as the input message."
|
| 32 |
)
|
|
@@ -35,7 +50,13 @@ def compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback,
|
|
| 35 |
|
| 36 |
prompt = f"{SYSTEM_PROMPT_START}\n"
|
| 37 |
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
prompt += (
|
| 40 |
f"\nThe message below is intended for the following combined audience segments: "
|
| 41 |
f"{', '.join(targets)}. Please address the message to all these groups together, synthesizing messaging approaches to be clear, relevant, and persuasive for the group as a whole. "
|
|
@@ -57,7 +78,7 @@ def compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback,
|
|
| 57 |
prompt += SYSTEM_PROMPT_FEEDBACK
|
| 58 |
return prompt.strip()
|
| 59 |
|
| 60 |
-
def run_llm(api_key, targets, consider_spam, feedback, additional_sysinfo, user_message):
|
| 61 |
if not api_key or len(api_key.strip()) < 20:
|
| 62 |
return "Please enter a valid OpenAI API key before running."
|
| 63 |
if not user_message.strip():
|
|
@@ -69,7 +90,7 @@ def run_llm(api_key, targets, consider_spam, feedback, additional_sysinfo, user_
|
|
| 69 |
client = openai.OpenAI(api_key=api_key)
|
| 70 |
except Exception as e:
|
| 71 |
return f"Could not initialize OpenAI client: {e}"
|
| 72 |
-
prompt = compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback, user_message)
|
| 73 |
if prompt.startswith("Error:"):
|
| 74 |
return prompt
|
| 75 |
try:
|
|
@@ -129,6 +150,13 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 129 |
label="OpenAI API Key",
|
| 130 |
info="Enter the key you received, or get one at https://platform.openai.com/api-keys"
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
with gr.Row():
|
| 133 |
target = gr.Checkboxgroup(
|
| 134 |
choices=target_options,
|
|
@@ -171,7 +199,7 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 171 |
|
| 172 |
submit.click(
|
| 173 |
fn=run_llm,
|
| 174 |
-
inputs=[api_key, target, consider_spam, feedback, additional_sysinfo, user_message],
|
| 175 |
outputs=output
|
| 176 |
)
|
| 177 |
|
|
|
|
| 12 |
SYSTEM_PROMPT_FEEDBACK = "\n" + read_file("system_prompt_feedback.txt")
|
| 13 |
TARGET_OPTIONS_DIR = "target_options"
|
| 14 |
|
| 15 |
+
REGION_OPTIONS = [
|
| 16 |
+
"Alberta",
|
| 17 |
+
"Atlantic Canada",
|
| 18 |
+
"British Columbia",
|
| 19 |
+
"Ontario",
|
| 20 |
+
"Quebec",
|
| 21 |
+
"Saskatchewan and Manitoba"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
def get_region_prompt(region_name):
|
| 25 |
+
file_path = os.path.join(TARGET_OPTIONS_DIR, region_name + ".txt")
|
| 26 |
+
if os.path.exists(file_path):
|
| 27 |
+
return read_file(file_path)
|
| 28 |
+
return ""
|
| 29 |
+
|
| 30 |
def get_target_options():
|
| 31 |
options = []
|
| 32 |
for filename in os.listdir(TARGET_OPTIONS_DIR):
|
|
|
|
| 41 |
return read_file(file_path)
|
| 42 |
return ""
|
| 43 |
|
| 44 |
+
def compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback, user_message, region=None):
|
| 45 |
length_instruction = (
|
| 46 |
"\nPlease keep your rewritten message about the same length as the input message."
|
| 47 |
)
|
|
|
|
| 50 |
|
| 51 |
prompt = f"{SYSTEM_PROMPT_START}\n"
|
| 52 |
|
| 53 |
+
# Insert region context if present
|
| 54 |
+
if region:
|
| 55 |
+
region_prompt = get_region_prompt(region)
|
| 56 |
+
if region_prompt.strip():
|
| 57 |
+
prompt += f"\n--- Regional Context: {region} ---\n{region_prompt}\n"
|
| 58 |
+
|
| 59 |
+
# Audience segment context continues unchanged...
|
| 60 |
prompt += (
|
| 61 |
f"\nThe message below is intended for the following combined audience segments: "
|
| 62 |
f"{', '.join(targets)}. Please address the message to all these groups together, synthesizing messaging approaches to be clear, relevant, and persuasive for the group as a whole. "
|
|
|
|
| 78 |
prompt += SYSTEM_PROMPT_FEEDBACK
|
| 79 |
return prompt.strip()
|
| 80 |
|
| 81 |
+
def run_llm(api_key, targets, consider_spam, feedback, additional_sysinfo, user_message, region):
|
| 82 |
if not api_key or len(api_key.strip()) < 20:
|
| 83 |
return "Please enter a valid OpenAI API key before running."
|
| 84 |
if not user_message.strip():
|
|
|
|
| 90 |
client = openai.OpenAI(api_key=api_key)
|
| 91 |
except Exception as e:
|
| 92 |
return f"Could not initialize OpenAI client: {e}"
|
| 93 |
+
prompt = compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback, user_message, region)
|
| 94 |
if prompt.startswith("Error:"):
|
| 95 |
return prompt
|
| 96 |
try:
|
|
|
|
| 150 |
label="OpenAI API Key",
|
| 151 |
info="Enter the key you received, or get one at https://platform.openai.com/api-keys"
|
| 152 |
)
|
| 153 |
+
with gr.Row():
|
| 154 |
+
region = gr.Dropdown(
|
| 155 |
+
choices=REGION_OPTIONS,
|
| 156 |
+
value="British Columbia",
|
| 157 |
+
label="Region",
|
| 158 |
+
info="Select the primary region for this message. The system will use regional context and characteristics as part of the prompt."
|
| 159 |
+
)
|
| 160 |
with gr.Row():
|
| 161 |
target = gr.Checkboxgroup(
|
| 162 |
choices=target_options,
|
|
|
|
| 199 |
|
| 200 |
submit.click(
|
| 201 |
fn=run_llm,
|
| 202 |
+
inputs=[api_key, target, consider_spam, feedback, additional_sysinfo, user_message, region],
|
| 203 |
outputs=output
|
| 204 |
)
|
| 205 |
|