Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,7 @@ def get_target_prompt(option_name):
|
|
| 41 |
return read_file(file_path)
|
| 42 |
return ""
|
| 43 |
|
| 44 |
-
def compose_system_prompt(targets, additional_sysinfo,
|
| 45 |
length_instruction = (
|
| 46 |
"\nPlease keep your rewritten message about the same length as the input message."
|
| 47 |
)
|
|
@@ -86,8 +86,6 @@ def compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback,
|
|
| 86 |
def run_llm(
|
| 87 |
api_key, targets, feedback, additional_sysinfo, user_message, region, model, temperature, top_p
|
| 88 |
):
|
| 89 |
-
# ...existing checks...
|
| 90 |
-
|
| 91 |
try:
|
| 92 |
client = openai.OpenAI(api_key=api_key)
|
| 93 |
except Exception as e:
|
|
@@ -112,18 +110,69 @@ def run_llm(
|
|
| 112 |
prompt_tokens = usage.prompt_tokens
|
| 113 |
completion_tokens = usage.completion_tokens
|
| 114 |
total_tokens = usage.total_tokens
|
| 115 |
-
token_message = f"
|
| 116 |
else:
|
| 117 |
-
token_message = "Token
|
| 118 |
return completion.choices[0].message.content.strip(), token_message
|
| 119 |
except Exception as e:
|
| 120 |
return f"An error occurred: {e}", ""
|
| 121 |
|
| 122 |
target_options = get_target_options()
|
| 123 |
|
|
|
|
|
|
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
|
| 126 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Column(scale=2):
|
| 129 |
gr.Markdown("""
|
|
@@ -134,8 +183,6 @@ You can select your intended audiences (choose multiple if needed!), add optiona
|
|
| 134 |
|
| 135 |
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 ('---').
|
| 136 |
|
| 137 |
-
Selecting the Consider Spam Filters button will include instructions to the LLM to consider ways to avoid spam filters.
|
| 138 |
-
|
| 139 |
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.
|
| 140 |
|
| 141 |
This tool is “expert adjacent”—it is designed to support people with some experience in communications work, not to generate communications materials from scratch.
|
|
@@ -147,7 +194,7 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 147 |
- If you received a link to this app, you may also have received an OpenAI API key you can use.
|
| 148 |
- (Optional) If you did not get a key, you can create your own at [OpenAI API Keys](https://platform.openai.com/api-keys).
|
| 149 |
- (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.
|
| 150 |
-
- Copy and paste your key in the box
|
| 151 |
""")
|
| 152 |
with gr.Column(scale=1):
|
| 153 |
gr.Image(
|
|
@@ -155,15 +202,9 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 155 |
elem_id="logo-img",
|
| 156 |
show_label=False,
|
| 157 |
show_download_button=False,
|
| 158 |
-
show_share_button=False,
|
| 159 |
)
|
| 160 |
-
|
| 161 |
-
api_key = gr.Textbox(
|
| 162 |
-
type="password",
|
| 163 |
-
placeholder="Paste your OpenAI API key here...",
|
| 164 |
-
label="OpenAI API Key",
|
| 165 |
-
info="Enter the key you received, or get one at https://platform.openai.com/api-keys"
|
| 166 |
-
)
|
| 167 |
with gr.Row():
|
| 168 |
region = gr.Dropdown(
|
| 169 |
choices=REGION_OPTIONS,
|
|
@@ -182,7 +223,7 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 182 |
feedback = gr.Checkbox(
|
| 183 |
label="Request Feedback from LLM?",
|
| 184 |
info="If checked, the LLM will provide additional feedback and suggestions about your message after rewriting it.",
|
| 185 |
-
value=True
|
| 186 |
)
|
| 187 |
|
| 188 |
additional_sysinfo = gr.Textbox(
|
|
@@ -199,33 +240,6 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 199 |
info="Paste or write your actual message here."
|
| 200 |
)
|
| 201 |
|
| 202 |
-
model_choices = ["gpt-4.1", "gpt-4", "gpt-3.5-turbo"]
|
| 203 |
-
model = gr.Dropdown(
|
| 204 |
-
choices=model_choices,
|
| 205 |
-
value="gpt-4.1",
|
| 206 |
-
label="OpenAI Model",
|
| 207 |
-
info="Select the model to use."
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
temperature = gr.Slider(
|
| 211 |
-
minimum=0,
|
| 212 |
-
maximum=2,
|
| 213 |
-
value=0.7,
|
| 214 |
-
step=0.01,
|
| 215 |
-
label="Temperature",
|
| 216 |
-
info="Higher values (e.g., 1.0) make output more random; lower values (e.g., 0.2) more focused."
|
| 217 |
-
)
|
| 218 |
-
|
| 219 |
-
top_p = gr.Slider(
|
| 220 |
-
minimum=0,
|
| 221 |
-
maximum=1,
|
| 222 |
-
value=1,
|
| 223 |
-
step=0.01,
|
| 224 |
-
label="Top-p",
|
| 225 |
-
info="Controls nucleus sampling; typical values are 0.8 (diverse) to 1 (default)."
|
| 226 |
-
)
|
| 227 |
-
|
| 228 |
-
|
| 229 |
submit = gr.Button("Generate Targetted Message", variant="primary")
|
| 230 |
output = gr.Textbox(
|
| 231 |
lines=10,
|
|
@@ -235,12 +249,7 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 235 |
token_pricing_md = gr.Markdown(
|
| 236 |
"<a href='https://platform.openai.com/docs/pricing' target='_blank'>View OpenAI pricing details</a>"
|
| 237 |
)
|
| 238 |
-
|
| 239 |
-
token_count_box = gr.Textbox(
|
| 240 |
-
label="Token Usage",
|
| 241 |
-
interactive=False,
|
| 242 |
-
value="Tokens exchanged will appear here."
|
| 243 |
-
)
|
| 244 |
submit.click(
|
| 245 |
fn=run_llm,
|
| 246 |
inputs=[api_key, target, feedback, additional_sysinfo, user_message, region, model, temperature, top_p],
|
|
|
|
| 41 |
return read_file(file_path)
|
| 42 |
return ""
|
| 43 |
|
| 44 |
+
def compose_system_prompt(targets, additional_sysinfo, feedback, user_message, region=None):
|
| 45 |
length_instruction = (
|
| 46 |
"\nPlease keep your rewritten message about the same length as the input message."
|
| 47 |
)
|
|
|
|
| 86 |
def run_llm(
|
| 87 |
api_key, targets, feedback, additional_sysinfo, user_message, region, model, temperature, top_p
|
| 88 |
):
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
client = openai.OpenAI(api_key=api_key)
|
| 91 |
except Exception as e:
|
|
|
|
| 110 |
prompt_tokens = usage.prompt_tokens
|
| 111 |
completion_tokens = usage.completion_tokens
|
| 112 |
total_tokens = usage.total_tokens
|
| 113 |
+
token_message = f"P:{prompt_tokens}|C:{completion_tokens}|T:{total_tokens} | Temp:{temperature} | Top-p:{top_p} | {model}"
|
| 114 |
else:
|
| 115 |
+
token_message = f"Token info N/A | Temp:{temperature} | Top-p:{top_p} | {model}"
|
| 116 |
return completion.choices[0].message.content.strip(), token_message
|
| 117 |
except Exception as e:
|
| 118 |
return f"An error occurred: {e}", ""
|
| 119 |
|
| 120 |
target_options = get_target_options()
|
| 121 |
|
| 122 |
+
model_choices = ["gpt-4.1", "gpt-4", "gpt-3.5-turbo"]
|
| 123 |
+
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
|
| 126 |
+
# --- Compact controls row at TOP ---
|
| 127 |
+
with gr.Row():
|
| 128 |
+
with gr.Column(scale=1):
|
| 129 |
+
api_key = gr.Textbox(
|
| 130 |
+
type="password",
|
| 131 |
+
placeholder="API key...",
|
| 132 |
+
label="API Key",
|
| 133 |
+
max_lines=1,
|
| 134 |
+
lines=1,
|
| 135 |
+
container=False,
|
| 136 |
+
)
|
| 137 |
+
with gr.Column(scale=0.8):
|
| 138 |
+
model = gr.Dropdown(
|
| 139 |
+
choices=model_choices,
|
| 140 |
+
value="gpt-4.1",
|
| 141 |
+
label="Model",
|
| 142 |
+
show_label=True,
|
| 143 |
+
)
|
| 144 |
+
with gr.Column(scale=0.6):
|
| 145 |
+
temperature = gr.Slider(
|
| 146 |
+
minimum=0,
|
| 147 |
+
maximum=2,
|
| 148 |
+
value=0.7,
|
| 149 |
+
step=0.01,
|
| 150 |
+
label="Temp",
|
| 151 |
+
show_label=True,
|
| 152 |
+
interactive=True,
|
| 153 |
+
)
|
| 154 |
+
with gr.Column(scale=0.6):
|
| 155 |
+
top_p = gr.Slider(
|
| 156 |
+
minimum=0,
|
| 157 |
+
maximum=1,
|
| 158 |
+
value=1,
|
| 159 |
+
step=0.01,
|
| 160 |
+
label="Top-p",
|
| 161 |
+
show_label=True,
|
| 162 |
+
interactive=True,
|
| 163 |
+
)
|
| 164 |
+
with gr.Column(scale=1.2):
|
| 165 |
+
token_count_box = gr.Textbox(
|
| 166 |
+
label="Tokens | Settings",
|
| 167 |
+
interactive=False,
|
| 168 |
+
value="--",
|
| 169 |
+
show_label=True,
|
| 170 |
+
max_lines=1,
|
| 171 |
+
lines=1,
|
| 172 |
+
container=False,
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# --- Logo-and-instructions row (main content below controls bar) ---
|
| 176 |
with gr.Row():
|
| 177 |
with gr.Column(scale=2):
|
| 178 |
gr.Markdown("""
|
|
|
|
| 183 |
|
| 184 |
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 ('---').
|
| 185 |
|
|
|
|
|
|
|
| 186 |
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.
|
| 187 |
|
| 188 |
This tool is “expert adjacent”—it is designed to support people with some experience in communications work, not to generate communications materials from scratch.
|
|
|
|
| 194 |
- If you received a link to this app, you may also have received an OpenAI API key you can use.
|
| 195 |
- (Optional) If you did not get a key, you can create your own at [OpenAI API Keys](https://platform.openai.com/api-keys).
|
| 196 |
- (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.
|
| 197 |
+
- Copy and paste your key in the box above. Your API key will only be used for this session and is never stored.
|
| 198 |
""")
|
| 199 |
with gr.Column(scale=1):
|
| 200 |
gr.Image(
|
|
|
|
| 202 |
elem_id="logo-img",
|
| 203 |
show_label=False,
|
| 204 |
show_download_button=False,
|
| 205 |
+
show_share_button=False,
|
| 206 |
)
|
| 207 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
with gr.Row():
|
| 209 |
region = gr.Dropdown(
|
| 210 |
choices=REGION_OPTIONS,
|
|
|
|
| 223 |
feedback = gr.Checkbox(
|
| 224 |
label="Request Feedback from LLM?",
|
| 225 |
info="If checked, the LLM will provide additional feedback and suggestions about your message after rewriting it.",
|
| 226 |
+
value=True
|
| 227 |
)
|
| 228 |
|
| 229 |
additional_sysinfo = gr.Textbox(
|
|
|
|
| 240 |
info="Paste or write your actual message here."
|
| 241 |
)
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
submit = gr.Button("Generate Targetted Message", variant="primary")
|
| 244 |
output = gr.Textbox(
|
| 245 |
lines=10,
|
|
|
|
| 249 |
token_pricing_md = gr.Markdown(
|
| 250 |
"<a href='https://platform.openai.com/docs/pricing' target='_blank'>View OpenAI pricing details</a>"
|
| 251 |
)
|
| 252 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
submit.click(
|
| 254 |
fn=run_llm,
|
| 255 |
inputs=[api_key, target, feedback, additional_sysinfo, user_message, region, model, temperature, top_p],
|