OrganizingForChange commited on
Commit
309c263
·
verified ·
1 Parent(s): 063129c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -17
app.py CHANGED
@@ -83,32 +83,41 @@ def compose_system_prompt(targets, additional_sysinfo, consider_spam, feedback,
83
  prompt += SYSTEM_PROMPT_FEEDBACK
84
  return prompt.strip()
85
 
86
- def run_llm(api_key, targets, feedback, additional_sysinfo, user_message, region):
87
- if not api_key or len(api_key.strip()) < 20:
88
- return "Please enter a valid OpenAI API key before running."
89
- if not user_message.strip():
90
- return "Please enter a message."
91
- if not targets or len(targets) == 0:
92
- return "Please select at least one target audience."
93
- # Use the user-supplied API key to create OpenAI client
94
  try:
95
  client = openai.OpenAI(api_key=api_key)
96
  except Exception as e:
97
- return f"Could not initialize OpenAI client: {e}"
 
98
  prompt = compose_system_prompt(targets, additional_sysinfo, feedback, user_message, region)
99
  if prompt.startswith("Error:"):
100
- return prompt
 
101
  try:
102
  completion = client.chat.completions.create(
103
- model="gpt-4.1",
104
  messages=[
105
  {"role": "system", "content": prompt},
106
  {"role": "user", "content": user_message}
107
- ]
 
 
108
  )
109
- return completion.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
110
  except Exception as e:
111
- return f"An error occurred: {e}"
112
 
113
  target_options = get_target_options()
114
 
@@ -190,17 +199,48 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
190
  info="Paste or write your actual message here."
191
  )
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  submit = gr.Button("Generate Targetted Message", variant="primary")
194
  output = gr.Textbox(
195
  lines=10,
196
  label="Message as Modified by LLM for Target Audience(s)",
197
  info="The LLM’s refined response will appear here. You can regenerate the response and get something new by clicking 'Generate Targetted Message' again."
198
  )
199
-
 
 
 
 
200
  submit.click(
201
  fn=run_llm,
202
- inputs=[api_key, target, feedback, additional_sysinfo, user_message, region],
203
- outputs=output
204
  )
205
 
206
  if __name__ == "__main__":
 
83
  prompt += SYSTEM_PROMPT_FEEDBACK
84
  return prompt.strip()
85
 
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:
94
+ return f"Could not initialize OpenAI client: {e}", ""
95
+
96
  prompt = compose_system_prompt(targets, additional_sysinfo, feedback, user_message, region)
97
  if prompt.startswith("Error:"):
98
+ return prompt, ""
99
+
100
  try:
101
  completion = client.chat.completions.create(
102
+ model=model,
103
  messages=[
104
  {"role": "system", "content": prompt},
105
  {"role": "user", "content": user_message}
106
+ ],
107
+ temperature=temperature,
108
+ top_p=top_p
109
  )
110
+ usage = getattr(completion, 'usage', None)
111
+ if usage:
112
+ prompt_tokens = usage.prompt_tokens
113
+ completion_tokens = usage.completion_tokens
114
+ total_tokens = usage.total_tokens
115
+ token_message = f"Prompt tokens: {prompt_tokens}, Completion tokens: {completion_tokens}, Total tokens: {total_tokens}"
116
+ else:
117
+ token_message = "Token usage information unavailable."
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
 
 
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,
232
  label="Message as Modified by LLM for Target Audience(s)",
233
  info="The LLM’s refined response will appear here. You can regenerate the response and get something new by clicking 'Generate Targetted Message' again."
234
  )
235
+ token_count_box = gr.Textbox(
236
+ label="Token Usage",
237
+ interactive=False,
238
+ value="Tokens exchanged will appear here."
239
+ )
240
  submit.click(
241
  fn=run_llm,
242
+ inputs=[api_key, target, feedback, additional_sysinfo, user_message, region, model, temperature, top_p],
243
+ outputs=[output, token_count_box]
244
  )
245
 
246
  if __name__ == "__main__":