OrganizingForChange's picture
Update app.py
ea4ca98 verified
Raw History Blame Contribute Delete
16 kB
import gradio as gr
import openai
import os
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_FEEDBACK = "\n" + read_file("system_prompt_feedback.txt")
TARGET_OPTIONS_DIR = "target_options"
REGION_OPTIONS_DIR = "region_options"
def get_region_options():
options = []
if not os.path.isdir(REGION_OPTIONS_DIR):
return options
for filename in os.listdir(REGION_OPTIONS_DIR):
if filename.endswith(".txt"):
options.append(filename[:-4])
options.sort()
return options
region_options = get_region_options()
def get_region_prompt(region_name):
file_path = os.path.join(REGION_OPTIONS_DIR, region_name + ".txt")
if os.path.exists(file_path):
return read_file(file_path)
return ""
def get_target_options():
options = []
if not os.path.isdir(TARGET_OPTIONS_DIR):
return 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(targets, additional_sysinfo, feedback, user_message, region=None):
length_instruction = (
"\nPlease keep your rewritten message about the same length as the input message."
)
if not targets or len(targets) == 0:
return "Error: Please select at least one audience segment."
prompt = f"{SYSTEM_PROMPT_START}\n"
if region:
region_prompt = get_region_prompt(region)
if region_prompt.strip():
prompt += (
f"\n--- Regional Context: {region} ---\n"
f"\nThe message you generate should be crafted to appeal to audiences in {region}. "
f"{region_prompt}\n"
f"The following provides information about cultural, linguistic, or attitudinal characteristics specific to {region}. Adapt the input message accordingly, integrating regional aspects wherever appropriate.\n"
)
prompt += (
f"\nThe message below is intended for the following combined audience segments: "
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. "
"When there may be conflicting messaging advice for different segments, please use your best judgment to combine or balance them."
)
for t in targets:
p = get_target_prompt(t)
if p.strip():
prompt += f"\n\n--- Segment Context: {t} ---\n{p}\n"
if additional_sysinfo and additional_sysinfo.strip():
prompt += "\n" + additional_sysinfo.strip() + "\n"
prompt += length_instruction
if feedback:
prompt += SYSTEM_PROMPT_FEEDBACK
return prompt.strip()
def split_main_and_feedback(llm_output):
import re
pattern = re.compile(r'^(?:\s*)(?:-{3,}|[—–―]+)(?:\s*)$', flags=re.MULTILINE)
m = pattern.search(llm_output or "")
if m:
main_message = llm_output[:m.start()].strip()
feedback_message = llm_output[m.end():].strip()
else:
main_message = (llm_output or "").strip()
feedback_message = ""
return main_message, feedback_message
def is_gpt5_model(model_name: str) -> bool:
return bool(model_name and model_name.startswith("gpt-5"))
def run_llm(
api_key, targets, additional_sysinfo, user_message, region,
model, reasoning_effort, verbosity, temperature, top_p
):
feedback = True # ALWAYS ENABLED
try:
client = openai.OpenAI(api_key=api_key)
except Exception as e:
return f"Could not initialize OpenAI client: {e}", "", ""
prompt = compose_system_prompt(targets, additional_sysinfo, feedback, user_message, region)
if prompt.startswith("Error:"):
return prompt, "", ""
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": user_message}
]
try:
req = {
"model": model,
"input": messages
}
# Route parameters based on model family
if is_gpt5_model(model):
# gpt-5 family: use "reasoning" and "text" options
req["reasoning"] = {"effort": reasoning_effort} if reasoning_effort else None
req["text"] = {"verbosity": verbosity} if verbosity else None
else:
# gpt-4 family: use temperature/top_p
if temperature is not None:
req["temperature"] = float(temperature)
if top_p is not None:
req["top_p"] = float(top_p)
# Remove any None entries so we don't send them
req = {k: v for k, v in req.items() if v is not None}
resp = client.responses.create(**req)
full_content = (getattr(resp, "output_text", None) or "").strip()
if not full_content:
parts = []
for item in getattr(resp, "output", []) or []:
for part in getattr(item, "content", []) or []:
if getattr(part, "type", None) in ("output_text", "text"):
text = getattr(part, "text", None)
if text:
parts.append(text)
full_content = "".join(parts).strip()
usage = getattr(resp, "usage", None)
if usage:
token_message = f"Input: {usage.input_tokens}, Output: {usage.output_tokens}, Total: {usage.total_tokens}"
else:
token_message = "Token info unavailable"
main_msg, feedback_msg = split_main_and_feedback(full_content)
if not feedback_msg:
feedback_msg = "(No feedback section found. The model may not have followed instructions.)"
return main_msg, feedback_msg, token_message
except Exception as e:
return f"An error occurred: {e}", "", ""
target_options = get_target_options()
# Add gpt-4 models alongside gpt-5
model_choices = [
"gpt-5", "gpt-5-mini",
"gpt-4o", "gpt-4-turbo"
]
custom_css = """
/* Apply to both textarea and contenteditable fallback /
.scrollable-tb textarea,
.scrollable-tb [contenteditable="true"] {
overflow-y: auto !important;
resize: vertical !important;
min-height: 140px !important;
max-height: 400px !important;
-webkit-overflow-scrolling: touch;
scrollbar-width: auto !important; / Firefox /
-ms-overflow-style: auto !important; / IE/Edge legacy */
}
/* Re-enable scrollbars that Gradio hides */
.scrollable-tb textarea::-webkit-scrollbar,
.scrollable-tb [contenteditable="true"]::-webkit-scrollbar {
width: 10px !important;
height: 10px !important;
}
/* Visible thumb/track */
.scrollable-tb textarea::-webkit-scrollbar-thumb,
.scrollable-tb [contenteditable="true"]::-webkit-scrollbar-thumb {
background: rgba(0,0,0,0.35) !important;
border-radius: 8px !important;
}
.scrollable-tb textarea::-webkit-scrollbar-track,
.scrollable-tb [contenteditable="true"]::-webkit-scrollbar-track {
background: transparent !important;
}
/* If Gradio adds classes that hide scrollbars, neutralize them */
.scrollable-tb .scrollbar-hidden,
.scrollable-tb .scrollbar-hide {
scrollbar-width: auto !important;
-ms-overflow-style: auto !important;
}
.scrollable-tb .scrollbar-hidden::-webkit-scrollbar,
.scrollable-tb .scrollbar-hide::-webkit-scrollbar {
display: initial !important;
}
"""
with gr.Blocks(css=custom_css) as demo:
# --- Logo-and-instructions row (TOP) ---
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.
You can select your intended audiences (choose multiple if needed!), 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 you will receive both a rewritten message and additional feedback and suggestions about your message after rewriting it.
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. For support, contact Sebastian at sebastian@organizingforchange.org.
""")
with gr.Column(scale=1):
gr.Image(
"logo.png",
elem_id="logo-img",
show_label=False,
show_download_button=False,
show_share_button=False,
)
# --- Model/Settings/API Key panel ---
with gr.Column():
gr.Markdown("## 1. LLM Setup & API Key")
with gr.Group():
with gr.Accordion("Advanced: LLM Setup", open=False):
with gr.Row():
with gr.Column():
model = gr.Dropdown(
choices=model_choices,
value="gpt-5",
label="OpenAI Model",
info="Select the model to use."
)
with gr.Column():
reasoning_effort = gr.Dropdown(
choices=["minimal", "low", "medium", "high"],
value="medium",
label="Reasoning Effort (gpt-5 only)",
info="How hard the model thinks and how willingly it calls tools",
visible=True # default visible for gpt-5
)
with gr.Column():
verbosity = gr.Dropdown(
choices=["low", "medium", "high"],
value="medium",
label="Verbosity (gpt-5 only)",
info="Whether answers are short and to the point or long and comprehensive (impacts feedback section most)",
visible=True # default visible for gpt-5
)
# Sliders shown for gpt-4 family only
with gr.Row():
with gr.Column():
temperature = gr.Slider(
minimum=0.0, maximum=2.0, value=0.7, step=0.05,
label="Temperature (gpt-4 only)",
info="Higher is more random/creative; lower is more focused",
visible=False # hidden for gpt-5 default
)
with gr.Column():
top_p = gr.Slider(
minimum=0.0, maximum=1.0, value=1.0, step=0.01,
label="Top-p (gpt-4 only)",
info="Nucleus sampling; consider reducing if increasing temperature",
visible=False # hidden for gpt-5 default
)
with gr.Group():
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"
)
# --- Main tool UI ---
# --- Segment and Region selection ---
with gr.Column():
gr.Markdown("## 2. Targetting and Message Input")
with gr.Group():
with gr.Row():
region = gr.Dropdown(
choices=region_options,
value=region_options[0] if region_options else None,
label="Region",
info="Select the primary region for this message. The system will use regional context and characteristics as part of the prompt."
)
with gr.Row():
target = gr.Checkboxgroup(
choices=get_target_options(),
label="Target Audience Segments",
info="Select one or more intended audience segments. Your message will be adapted for all selected groups together.",
value=[]
)
with gr.Accordion("Advanced: System Prompt Injection", open=False):
additional_sysinfo = gr.Textbox(
lines=3,
placeholder="(Optional) System Prompt Injection...",
label="",
info="Use this to inject additional instructions to the system prompt.",
show_label=False
)
with gr.Group():
user_message = gr.Textbox(
lines=10,
max_lines=16,
placeholder="Paste your communications copy here...",
label="Input Message",
info="Paste or write your actual communications copy here."
)
submit = gr.Button("Generate Targetted Message", variant="primary")
# --- All outputs in a single simple column for best scroll behavior ---
with gr.Column():
gr.Markdown("## 3. Output Message & LLM Feedback")
output = gr.Textbox(
lines=8,
max_lines=16,
label="Output Message",
info="The LLM’s refined response will appear here",
interactive=True,
show_copy_button=True,
elem_classes=["scrollable-tb"]
)
with gr.Column():
feedback_box = gr.Textbox(
lines=8,
max_lines=16,
label="LLM Feedback",
info="The LLM’s feedback and suggestions specific to your message and audience will appear here",
interactive=True,
show_copy_button=True,
elem_classes=["scrollable-tb"]
)
with gr.Column():
token_count_box = gr.Textbox(
label="Token Usage",
interactive=False,
info="See <a href='https://platform.openai.com/docs/pricing' target='_blank'>OpenAI pricing details</a> and <a href='https://epoch.ai/gradient-updates/how-much-energy-does-chatgpt-use' target='_blank'>How much energy does ChatGPT use?</a> for information about token cost and energy usage"
)
# Dynamic UI: toggle fields on model change
def on_model_change(selected_model):
if is_gpt5_model(selected_model):
return (
gr.update(visible=True), # reasoning_effort
gr.update(visible=True), # verbosity
gr.update(visible=False), # temperature
gr.update(visible=False), # top_p
)
else:
return (
gr.update(visible=False), # reasoning_effort
gr.update(visible=False), # verbosity
gr.update(visible=True), # temperature
gr.update(visible=True), # top_p
)
model.change(
fn=on_model_change,
inputs=model,
outputs=[reasoning_effort, verbosity, temperature, top_p]
)
# --- Connect submit button after outputs are created ---
submit.click(
fn=run_llm,
inputs=[
api_key, target, additional_sysinfo, user_message, region,
model, reasoning_effort, verbosity, temperature, top_p
],
outputs=[output, feedback_box, token_count_box]
)
if __name__ == "__main__":
demo.launch(share=True)