File size: 7,489 Bytes
01e36e0 f0f8c82 01e36e0 f0f8c82 01e36e0 f0f8c82 01e36e0 8d6f3d6 fd75962 8d6f3d6 f0f8c82 8d6f3d6 f0f8c82 01e36e0 7f22d46 f0f8c82 01e36e0 9f1dddc 01e36e0 f0f8c82 01e36e0 f0f8c82 01e36e0 | 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | import gradio as gr
import openai
import os
import tiktoken
def read_file(path):
with open(path, "r", encoding="utf-8") as f:
return f.read().strip()
SYSTEM_PROMPT_START = read_file("system_prompt_start.txt")
SYSTEM_PROMPT_SPAM_FILTER = "\n" + read_file("system_prompt_spam_filter.txt")
SYSTEM_PROMPT_FEEDBACK = "\n" + read_file("system_prompt_feedback.txt")
TARGET_OPTIONS_DIR = "target_options"
def get_target_options():
options = []
for filename in os.listdir(TARGET_OPTIONS_DIR):
if filename.endswith(".txt"):
options.append(filename[:-4])
options.sort()
return options
def get_target_prompt(option_name):
file_path = os.path.join(TARGET_OPTIONS_DIR, option_name + ".txt")
if os.path.exists(file_path):
return read_file(file_path)
return ""
def compose_system_prompt(target, additional_sysinfo, consider_spam, feedback, user_message):
length_instruction = (
"\nPlease keep your rewritten message about the same length as the input message."
)
prompt = f"{SYSTEM_PROMPT_START}\n"
prompt += get_target_prompt(target) + "\n"
if additional_sysinfo.strip():
prompt += additional_sysinfo.strip() + "\n"
prompt += length_instruction
if consider_spam:
prompt += SYSTEM_PROMPT_SPAM_FILTER
if feedback:
prompt += SYSTEM_PROMPT_FEEDBACK
return prompt.strip()
def run_llm(api_key, target, consider_spam, feedback, additional_sysinfo, user_message):
if not api_key or len(api_key.strip()) < 20:
return "Please enter a valid OpenAI API key before running."
if not user_message.strip():
return "Please enter a message."
# Use the user-supplied API key to create OpenAI client
try:
client = openai.OpenAI(api_key=api_key)
except Exception as e:
return f"Could not initialize OpenAI client: {e}"
prompt = compose_system_prompt(target, additional_sysinfo, consider_spam, feedback, user_message)
try:
completion = client.chat.completions.create(
model="gpt-4.1",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": user_message}
]
)
return completion.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred: {e}"
target_options = get_target_options()
with gr.Blocks() as demo:
# Logo-and-instructions row
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("""
## Experimental LLM Segment Messaging App
This tool helps you improve, adapt, and review your written messaging—emails, newsletters, petitions, announcements, and more—using a state-of-the-art AI language model. It is based on the excellent research on segmentation by [EcoAnalytics](https://ecoanalyticscanada.org/).
You can select your intended audience, add optional instructions for the AI, and receive an output that is tailored for clarity, effectiveness, and your communication goal.
The system will reformulate your message to match the length of your input (not longer), and if you ask for feedback, the response will contain a feedback section of appropriate length after your main message. This feedback (should) always come at the end and follow three dashes ('---').
Selecting the Consider Spam Filters button will include instructions to the LLM to consider ways to avoid spam filters.
When you are ready, just hit the "Generate Targetted Message" button and wait a moment. If you are unhappy with the result, or want to see another version, you can resubmit the same information for a different result.
This tool is “expert adjacent”—it is designed to support people with some experience in communications work, not to generate communications materials from scratch.
This tool was created by [Organizing for Change](https://www.organizingforchange.org). For support, contact Sebastian at sebastian@organizingforchange.org.
**API Key Instructions**
- If you received a link to this app, you may also have received an OpenAI API key you can use.
- (Optional) If you did not get a key, you can create your own at [OpenAI API Keys](https://platform.openai.com/api-keys).
- (Optional) If creating a key, you will need to create an account and connect a payment method. Don't worry, this program does not consume many credits per run.
- Copy and paste your key in the box below. Your API key will only be used for this session and is never stored.
""")
with gr.Column(scale=1):
gr.Image(
"logo.png",
elem_id="logo-img",
show_label=False,
show_download_button=False,
show_share_button=False, # hide share
)
with gr.Row():
api_key = gr.Textbox(
type="password",
placeholder="Paste your OpenAI API key here...",
label="OpenAI API Key",
info="Enter the key you received, or get one at https://platform.openai.com/api-keys"
)
with gr.Row():
target = gr.Dropdown(
choices=target_options,
value=target_options[0] if target_options else None,
label="Target Audience",
info="Select your message's intended audience."
)
with gr.Row():
consider_spam = gr.Checkbox(
label="Consider Spam Filters?",
info="Check to ensure the system considers spam filters. Select this if you want the LLM to consider ways to avoid spam filters (for emails)."
)
with gr.Row():
feedback = gr.Checkbox(
label="Request Feedback from LLM?",
info="If checked, the LLM will provide additional feedback and suggestions about your message after rewriting it.",
value=True # This makes it checked by default!
)
additional_sysinfo = gr.Textbox(
lines=3,
placeholder="(Optional) Add extra instructions to system prompt...",
label="Additional System Prompt Info",
info="Extra context or rules for the system prompt. You should include at least what the type of comms product you are inputting if it isn't otherwise obvious (e.g. entering 'This is for a petition' if you are entering petition text). You can also provide additional context for the LLM to pull from that might not be in your current comms product. You can also provide other guidance like 'Keep it short!'",
)
user_message = gr.Textbox(
lines=10,
placeholder="Paste your message here...",
label="Input Message",
info="Paste or write your actual message here."
)
submit = gr.Button("Generate Targetted Message", variant="primary")
output = gr.Textbox(
lines=10,
label="Message as Modified by LLM for Target Audience",
info="The LLM’s refined response will appear here. You can regenerate the response and get something new by clicking 'Generate Targetted Message' again."
)
submit.click(
fn=run_llm,
inputs=[api_key, target, consider_spam, feedback, additional_sysinfo, user_message],
outputs=output
)
if __name__ == "__main__":
demo.launch(share=True) |