File size: 15,958 Bytes
95b2b3a
 
 
 
 
 
 
 
 
 
 
458f62d
95b2b3a
da666e9
 
7346450
 
da666e9
 
 
 
 
 
 
272bb46
 
458f62d
272bb46
 
 
 
95b2b3a
 
7346450
 
95b2b3a
 
 
 
 
 
 
 
 
 
 
 
0a0e1b0
95b2b3a
 
 
 
 
 
 
272bb46
 
 
2116921
 
002632a
2116921
002632a
2116921
272bb46
95b2b3a
 
 
 
 
 
 
 
 
 
 
 
 
7346450
95b2b3a
7346450
95b2b3a
 
 
 
7346450
 
cd3e95e
 
7346450
cd3e95e
7346450
 
cd3e95e
7346450
 
9e73d96
 
 
 
309c263
9e73d96
 
309c263
7346450
 
95b2b3a
 
 
7346450
a3eab05
236e188
95b2b3a
7346450
a3eab05
 
 
 
 
 
95b2b3a
9e73d96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3eab05
 
 
 
 
 
 
 
 
 
 
 
 
309c263
a3eab05
309c263
a3eab05
 
7346450
 
 
a3eab05
7346450
a3eab05
95b2b3a
7346450
95b2b3a
 
9e73d96
 
 
 
 
 
0a0e1b0
67ff81f
8c961a2
 
 
 
 
 
 
 
 
 
c4c1bb6
 
8c961a2
 
 
 
 
71ff0ba
 
8c961a2
 
 
 
 
 
 
 
 
71ff0ba
 
8c961a2
 
 
 
 
 
 
 
 
e499795
67ff81f
 
 
95b2b3a
2145342
95b2b3a
 
 
 
9e73d96
95b2b3a
 
 
7346450
95b2b3a
 
 
 
 
9e73d96
46cacb3
95b2b3a
 
 
 
 
 
0a0e1b0
95b2b3a
0a0e1b0
8137d03
094a002
5f72538
8137d03
f52dd0e
e8084ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f52dd0e
8137d03
 
 
 
 
 
 
2145342
ae28c00
7348c8e
5f72538
cb1918e
 
 
 
 
 
 
 
 
 
9e73d96
cb1918e
 
9e73d96
cb1918e
95b2b3a
ea4ca98
ae28c00
 
e37e028
db61975
094a002
db61975
ae28c00
95b2b3a
428f41d
ae28c00
 
c4c1bb6
ae28c00
 
 
 
95b2b3a
 
67ff81f
 
 
5f72538
9a01525
76e1c08
c4c1bb6
f0ce6f9
12e50ce
f0ce6f9
8c961a2
 
9a01525
a9111e5
9a01525
 
c4c1bb6
9a01525
12e50ce
f0ce6f9
79eb28b
8c961a2
9a01525
a9111e5
d0cf50b
 
 
12e50ce
3a8054a
12e50ce
9e73d96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0cf50b
95b2b3a
 
9e73d96
 
 
 
7346450
95b2b3a
 
 
a331378
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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)