Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,7 +102,7 @@ def split_main_and_feedback(llm_output):
|
|
| 102 |
return main_message, feedback_message
|
| 103 |
|
| 104 |
def run_llm(
|
| 105 |
-
api_key, targets, additional_sysinfo, user_message, region, model,
|
| 106 |
):
|
| 107 |
feedback = True # ALWAYS ENABLED
|
| 108 |
|
|
@@ -110,42 +110,52 @@ def run_llm(
|
|
| 110 |
client = openai.OpenAI(api_key=api_key)
|
| 111 |
except Exception as e:
|
| 112 |
return f"Could not initialize OpenAI client: {e}", "", ""
|
| 113 |
-
|
| 114 |
prompt = compose_system_prompt(targets, additional_sysinfo, feedback, user_message, region)
|
| 115 |
if prompt.startswith("Error:"):
|
| 116 |
return prompt, "", ""
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
try:
|
| 119 |
-
|
| 120 |
-
model=model,
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
],
|
| 125 |
-
temperature=temperature,
|
| 126 |
-
top_p=top_p
|
| 127 |
)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
if usage:
|
| 130 |
-
|
| 131 |
-
completion_tokens = usage.completion_tokens
|
| 132 |
-
total_tokens = usage.total_tokens
|
| 133 |
-
token_message = f"Prompt: {prompt_tokens}, Completion: {completion_tokens}, Total: {total_tokens}"
|
| 134 |
else:
|
| 135 |
-
token_message =
|
| 136 |
-
|
| 137 |
-
full_content = completion.choices[0].message.content.strip()
|
| 138 |
main_msg, feedback_msg = split_main_and_feedback(full_content)
|
| 139 |
if not feedback_msg:
|
| 140 |
feedback_msg = "(No feedback section found. The model may not have followed instructions.)"
|
| 141 |
-
|
| 142 |
return main_msg, feedback_msg, token_message
|
| 143 |
-
|
| 144 |
except Exception as e:
|
| 145 |
return f"An error occurred: {e}", "", ""
|
| 146 |
|
| 147 |
target_options = get_target_options()
|
| 148 |
-
model_choices = ["gpt-5","gpt-
|
| 149 |
|
| 150 |
custom_css = """
|
| 151 |
.gradio-container {
|
|
@@ -191,29 +201,23 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 191 |
with gr.Row():
|
| 192 |
with gr.Column():
|
| 193 |
model = gr.Dropdown(
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
with gr.Column():
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
label="Temperature",
|
| 206 |
-
info="Higher values (e.g., 1.0) make output more random; lower values (e.g., 0.2) more focused."
|
| 207 |
-
)
|
| 208 |
with gr.Column():
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
label="Top-p",
|
| 215 |
-
info="Controls nucleus sampling; typical values are 0.8 (diverse) to 1 (default)."
|
| 216 |
-
)
|
| 217 |
with gr.Row():
|
| 218 |
api_key = gr.Textbox(
|
| 219 |
type="password",
|
|
@@ -283,7 +287,7 @@ This tool was created by [Organizing for Change](https://www.organizingforchange
|
|
| 283 |
# --- Connect submit button after outputs are created ---
|
| 284 |
submit.click(
|
| 285 |
fn=run_llm,
|
| 286 |
-
inputs=[api_key, target, additional_sysinfo, user_message, region, model, temperature, top_p],
|
| 287 |
outputs=[output, feedback_box, token_count_box]
|
| 288 |
)
|
| 289 |
|
|
|
|
| 102 |
return main_message, feedback_message
|
| 103 |
|
| 104 |
def run_llm(
|
| 105 |
+
api_key, targets, additional_sysinfo, user_message, region, model, reasoning_effort, verbosity
|
| 106 |
):
|
| 107 |
feedback = True # ALWAYS ENABLED
|
| 108 |
|
|
|
|
| 110 |
client = openai.OpenAI(api_key=api_key)
|
| 111 |
except Exception as e:
|
| 112 |
return f"Could not initialize OpenAI client: {e}", "", ""
|
| 113 |
+
|
| 114 |
prompt = compose_system_prompt(targets, additional_sysinfo, feedback, user_message, region)
|
| 115 |
if prompt.startswith("Error:"):
|
| 116 |
return prompt, "", ""
|
| 117 |
+
|
| 118 |
+
messages = [
|
| 119 |
+
{"role": "system", "content": prompt},
|
| 120 |
+
{"role": "user", "content": user_message}
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
try:
|
| 124 |
+
resp = client.responses.create(
|
| 125 |
+
model=model, # e.g., "gpt-5", "gpt-5-mini", "gpt-5-nano"
|
| 126 |
+
input=messages,
|
| 127 |
+
reasoning={"effort": reasoning_effort}, # "minimal" | "low" | "medium" | "high"
|
| 128 |
+
text={"verbosity": verbosity} # "low" | "medium" | "high"
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
+
|
| 131 |
+
full_content = (getattr(resp, "output_text", None) or "").strip()
|
| 132 |
+
if not full_content:
|
| 133 |
+
parts = []
|
| 134 |
+
for item in getattr(resp, "output", []) or []:
|
| 135 |
+
for part in getattr(item, "content", []) or []:
|
| 136 |
+
if getattr(part, "type", None) in ("output_text", "text"):
|
| 137 |
+
text = getattr(part, "text", None)
|
| 138 |
+
if text:
|
| 139 |
+
parts.append(text)
|
| 140 |
+
full_content = "".join(parts).strip()
|
| 141 |
+
|
| 142 |
+
usage = getattr(resp, "usage", None)
|
| 143 |
if usage:
|
| 144 |
+
token_message = f"Input: {usage.input_tokens}, Output: {usage.output_tokens}, Total: {usage.total_tokens}"
|
|
|
|
|
|
|
|
|
|
| 145 |
else:
|
| 146 |
+
token_message = "Token info unavailable"
|
| 147 |
+
|
|
|
|
| 148 |
main_msg, feedback_msg = split_main_and_feedback(full_content)
|
| 149 |
if not feedback_msg:
|
| 150 |
feedback_msg = "(No feedback section found. The model may not have followed instructions.)"
|
| 151 |
+
|
| 152 |
return main_msg, feedback_msg, token_message
|
| 153 |
+
|
| 154 |
except Exception as e:
|
| 155 |
return f"An error occurred: {e}", "", ""
|
| 156 |
|
| 157 |
target_options = get_target_options()
|
| 158 |
+
model_choices = ["gpt-5","gpt-5-mini"]
|
| 159 |
|
| 160 |
custom_css = """
|
| 161 |
.gradio-container {
|
|
|
|
| 201 |
with gr.Row():
|
| 202 |
with gr.Column():
|
| 203 |
model = gr.Dropdown(
|
| 204 |
+
choices=model_choices,
|
| 205 |
+
value="gpt-5", # default to GPT-5
|
| 206 |
+
label="OpenAI Model",
|
| 207 |
+
info="Select the model to use."
|
| 208 |
+
)
|
| 209 |
with gr.Column():
|
| 210 |
+
reasoning_effort = gr.Dropdown(
|
| 211 |
+
choices=["minimal", "low", "medium", "high"],
|
| 212 |
+
value="minimal", # recommended when migrating from gpt-4.1
|
| 213 |
+
label="Reasoning Effort (GPT-5)"
|
| 214 |
+
)
|
|
|
|
|
|
|
|
|
|
| 215 |
with gr.Column():
|
| 216 |
+
verbosity = gr.Dropdown(
|
| 217 |
+
choices=["low", "medium", "high"],
|
| 218 |
+
value="low", # helps keep outputs concise per your “same length” rule
|
| 219 |
+
label="Verbosity (GPT-5)"
|
| 220 |
+
)
|
|
|
|
|
|
|
|
|
|
| 221 |
with gr.Row():
|
| 222 |
api_key = gr.Textbox(
|
| 223 |
type="password",
|
|
|
|
| 287 |
# --- Connect submit button after outputs are created ---
|
| 288 |
submit.click(
|
| 289 |
fn=run_llm,
|
| 290 |
+
inputs=[api_key, target, additional_sysinfo, user_message, region, model, temperature, top_p, reasoning_effort, verbosity],
|
| 291 |
outputs=[output, feedback_box, token_count_box]
|
| 292 |
)
|
| 293 |
|