Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ def read_file(path):
|
|
| 7 |
with open(path, "r", encoding="utf-8") as f:
|
| 8 |
return f.read().strip()
|
| 9 |
|
| 10 |
-
OPENAI_API_KEY = read_file("openai_api_key.txt")
|
| 11 |
-
client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
| 12 |
SYSTEM_PROMPT_START = read_file("system_prompt_start.txt")
|
| 13 |
SYSTEM_PROMPT_SPAM_FILTER = "\n" + read_file("system_prompt_spam_filter.txt")
|
| 14 |
SYSTEM_PROMPT_FEEDBACK = "\n" + read_file("system_prompt_feedback.txt")
|
|
@@ -43,9 +41,16 @@ def compose_system_prompt(target, additional_sysinfo, consider_spam, feedback, u
|
|
| 43 |
prompt += SYSTEM_PROMPT_FEEDBACK
|
| 44 |
return prompt.strip()
|
| 45 |
|
| 46 |
-
def run_llm(target, consider_spam, feedback, additional_sysinfo, user_message):
|
|
|
|
|
|
|
| 47 |
if not user_message.strip():
|
| 48 |
return "Please enter a message."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
prompt = compose_system_prompt(target, additional_sysinfo, consider_spam, feedback, user_message)
|
| 50 |
try:
|
| 51 |
completion = client.chat.completions.create(
|
|
@@ -62,7 +67,7 @@ def run_llm(target, consider_spam, feedback, additional_sysinfo, user_message):
|
|
| 62 |
target_options = get_target_options()
|
| 63 |
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
|
| 66 |
# Logo-and-instructions row
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column(scale=2):
|
|
@@ -78,6 +83,12 @@ Selecting the Consider Spam Filters button will include instructions to the LLM
|
|
| 78 |
|
| 79 |
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.
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
This tool is “expert adjacent”—it is designed to support people with some experience in communications work, not to generate communications materials from scratch.
|
| 82 |
|
| 83 |
This tool was created by [Organizing for Change website](https://www.organizingforchange.org). For support, contact Sebastian at sebastian@organizingforchange.org.
|
|
@@ -85,6 +96,13 @@ This tool was created by [Organizing for Change website](https://www.organizingf
|
|
| 85 |
with gr.Column(scale=1):
|
| 86 |
gr.Image("logo.png", elem_id="logo-img", show_label=False, show_download_button=False)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
target = gr.Dropdown(
|
| 90 |
choices=target_options,
|
|
@@ -122,12 +140,12 @@ This tool was created by [Organizing for Change website](https://www.organizingf
|
|
| 122 |
output = gr.Textbox(
|
| 123 |
lines=10,
|
| 124 |
label="Message as Modified by LLM for Target Audience",
|
| 125 |
-
info="The LLM’s refined response will appear here. You can regenerate the response and get something new by
|
| 126 |
)
|
| 127 |
|
| 128 |
submit.click(
|
| 129 |
fn=run_llm,
|
| 130 |
-
inputs=[target, consider_spam, feedback, additional_sysinfo, user_message],
|
| 131 |
outputs=output
|
| 132 |
)
|
| 133 |
|
|
|
|
| 7 |
with open(path, "r", encoding="utf-8") as f:
|
| 8 |
return f.read().strip()
|
| 9 |
|
|
|
|
|
|
|
| 10 |
SYSTEM_PROMPT_START = read_file("system_prompt_start.txt")
|
| 11 |
SYSTEM_PROMPT_SPAM_FILTER = "\n" + read_file("system_prompt_spam_filter.txt")
|
| 12 |
SYSTEM_PROMPT_FEEDBACK = "\n" + read_file("system_prompt_feedback.txt")
|
|
|
|
| 41 |
prompt += SYSTEM_PROMPT_FEEDBACK
|
| 42 |
return prompt.strip()
|
| 43 |
|
| 44 |
+
def run_llm(api_key, target, consider_spam, feedback, additional_sysinfo, user_message):
|
| 45 |
+
if not api_key or len(api_key.strip()) < 20:
|
| 46 |
+
return "Please enter a valid OpenAI API key before running."
|
| 47 |
if not user_message.strip():
|
| 48 |
return "Please enter a message."
|
| 49 |
+
# Use the user-supplied API key to create OpenAI client
|
| 50 |
+
try:
|
| 51 |
+
client = openai.OpenAI(api_key=api_key)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Could not initialize OpenAI client: {e}"
|
| 54 |
prompt = compose_system_prompt(target, additional_sysinfo, consider_spam, feedback, user_message)
|
| 55 |
try:
|
| 56 |
completion = client.chat.completions.create(
|
|
|
|
| 67 |
target_options = get_target_options()
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
|
| 71 |
# Logo-and-instructions row
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column(scale=2):
|
|
|
|
| 83 |
|
| 84 |
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.
|
| 85 |
|
| 86 |
+
**API Key Instructions**
|
| 87 |
+
|
| 88 |
+
- If you received a link to this app, you may also have received an OpenAI API key you can use.
|
| 89 |
+
- If you did not get a key, you can create your own at [OpenAI API Keys](https://platform.openai.com/api-keys).
|
| 90 |
+
- Copy and paste your key in the box below. Your API key will only be used for this session and is never stored.
|
| 91 |
+
|
| 92 |
This tool is “expert adjacent”—it is designed to support people with some experience in communications work, not to generate communications materials from scratch.
|
| 93 |
|
| 94 |
This tool was created by [Organizing for Change website](https://www.organizingforchange.org). For support, contact Sebastian at sebastian@organizingforchange.org.
|
|
|
|
| 96 |
with gr.Column(scale=1):
|
| 97 |
gr.Image("logo.png", elem_id="logo-img", show_label=False, show_download_button=False)
|
| 98 |
|
| 99 |
+
with gr.Row():
|
| 100 |
+
api_key = gr.Textbox(
|
| 101 |
+
type="password",
|
| 102 |
+
placeholder="Paste your OpenAI API key here...",
|
| 103 |
+
label="OpenAI API Key",
|
| 104 |
+
info="Enter the key you received, or get one at https://platform.openai.com/api-keys"
|
| 105 |
+
)
|
| 106 |
with gr.Row():
|
| 107 |
target = gr.Dropdown(
|
| 108 |
choices=target_options,
|
|
|
|
| 140 |
output = gr.Textbox(
|
| 141 |
lines=10,
|
| 142 |
label="Message as Modified by LLM for Target Audience",
|
| 143 |
+
info="The LLM’s refined response will appear here. You can regenerate the response and get something new by clicking 'Generate Targetted Message' again."
|
| 144 |
)
|
| 145 |
|
| 146 |
submit.click(
|
| 147 |
fn=run_llm,
|
| 148 |
+
inputs=[api_key, target, consider_spam, feedback, additional_sysinfo, user_message],
|
| 149 |
outputs=output
|
| 150 |
)
|
| 151 |
|