Update app.py
Browse files
app.py
CHANGED
|
@@ -1,249 +1,237 @@
|
|
| 1 |
import os
|
| 2 |
-
import traceback
|
| 3 |
-
from typing import Any
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
GENERATION_MODELS = [
|
| 10 |
-
"gpt-4.1-mini",
|
| 11 |
-
"gpt-4.1",
|
| 12 |
-
"gpt-4o-mini",
|
| 13 |
"gpt-5.5",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
]
|
| 15 |
|
| 16 |
REASONING_MODELS = [
|
| 17 |
"gpt-5.5",
|
| 18 |
-
"
|
| 19 |
-
"
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
""
|
| 25 |
-
Hugging Face Spaces exposes Secrets as environment variables.
|
| 26 |
-
Add your OpenAI key in Space Settings as OPENAI_API_KEY.
|
| 27 |
-
The lowercase fallback is included only to help during local testing.
|
| 28 |
-
"""
|
| 29 |
-
api_key = os.getenv("OPENAI_API_KEY") or os.getenv("openai_api_key")
|
| 30 |
-
if not api_key:
|
| 31 |
-
return None
|
| 32 |
-
return OpenAI(api_key=api_key)
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
for item in getattr(response, "output", []) or []:
|
| 43 |
-
content = getattr(item, "content", None)
|
| 44 |
-
if content is None and isinstance(item, dict):
|
| 45 |
-
content = item.get("content", [])
|
| 46 |
|
| 47 |
-
for part in content or []:
|
| 48 |
-
if isinstance(part, dict):
|
| 49 |
-
text = part.get("text") or part.get("output_text")
|
| 50 |
-
else:
|
| 51 |
-
text = getattr(part, "text", None) or getattr(part, "output_text", None)
|
| 52 |
-
if text:
|
| 53 |
-
chunks.append(str(text))
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
-
def
|
| 59 |
"""
|
| 60 |
-
|
| 61 |
-
To avoid the common 400 error, this app does not send those controls to GPT-5.x models.
|
| 62 |
"""
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
lines = [f"--- {title} ---"]
|
| 68 |
-
for key, value in settings.items():
|
| 69 |
-
lines.append(f"{key}: {value}")
|
| 70 |
-
lines.append("------------------------\n")
|
| 71 |
-
return "\n".join(lines)
|
| 72 |
|
| 73 |
|
| 74 |
def run_generation(
|
| 75 |
-
prompt
|
| 76 |
-
model
|
| 77 |
-
system_message
|
| 78 |
-
temperature
|
| 79 |
-
top_p
|
| 80 |
-
max_output_tokens
|
| 81 |
-
frequency_penalty
|
| 82 |
-
presence_penalty
|
| 83 |
-
show_settings
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
return (
|
| 88 |
-
"Missing API key.\n\n"
|
| 89 |
-
"In Hugging Face Spaces, go to Settings → Secrets and add:\n"
|
| 90 |
-
"Name: OPENAI_API_KEY\n"
|
| 91 |
-
"Value: your OpenAI API key"
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
"input": prompt,
|
| 101 |
-
"max_output_tokens": int(max_output_tokens),
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
settings_note = ""
|
| 105 |
-
if is_gpt5_family(model):
|
| 106 |
-
settings_note = (
|
| 107 |
-
"Note: GPT-5 family models can reject custom sampling controls. "
|
| 108 |
-
"Temperature, top_p, frequency_penalty, and presence_penalty were not sent.\n\n"
|
| 109 |
-
)
|
| 110 |
-
else:
|
| 111 |
-
params.update(
|
| 112 |
-
{
|
| 113 |
-
"temperature": float(temperature),
|
| 114 |
-
"top_p": float(top_p),
|
| 115 |
-
"frequency_penalty": float(frequency_penalty),
|
| 116 |
-
"presence_penalty": float(presence_penalty),
|
| 117 |
-
}
|
| 118 |
-
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if show_settings:
|
| 125 |
-
settings =
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
if is_gpt5_family(model):
|
| 131 |
-
settings
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
else:
|
| 137 |
-
settings
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
}
|
| 144 |
-
)
|
| 145 |
-
return settings_note + format_settings("Generation Settings", settings) + text
|
| 146 |
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"Tip: If you selected a GPT-5 family model, try keeping generation controls at default "
|
| 154 |
-
"or use the Reasoning Controls tab.\n\n"
|
| 155 |
-
f"Technical details:\n{traceback.format_exc()}"
|
| 156 |
-
)
|
| 157 |
|
| 158 |
|
| 159 |
def run_reasoning(
|
| 160 |
-
prompt
|
| 161 |
-
model
|
| 162 |
-
reasoning_effort
|
| 163 |
-
max_output_tokens
|
| 164 |
-
show_settings
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
return (
|
| 169 |
-
"Missing API key.\n\n"
|
| 170 |
-
"In Hugging Face Spaces, go to Settings → Secrets and add:\n"
|
| 171 |
-
"Name: OPENAI_API_KEY\n"
|
| 172 |
-
"Value: your OpenAI API key"
|
| 173 |
-
)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
"input": prompt,
|
| 181 |
-
"reasoning": {"effort": reasoning_effort},
|
| 182 |
-
"max_output_tokens": int(max_output_tokens),
|
| 183 |
-
}
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
text = extract_output_text(response)
|
| 188 |
|
| 189 |
if show_settings:
|
| 190 |
-
settings =
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
return (
|
| 202 |
-
"OpenAI API error:\n"
|
| 203 |
-
f"{exc}\n\n"
|
| 204 |
-
"Tip: Make sure your account has access to the selected model, or try another model "
|
| 205 |
-
"from the dropdown.\n\n"
|
| 206 |
-
f"Technical details:\n{traceback.format_exc()}"
|
| 207 |
-
)
|
| 208 |
|
|
|
|
| 209 |
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
.gradio-container {
|
| 212 |
-
max-width:
|
| 213 |
margin: auto !important;
|
| 214 |
}
|
| 215 |
-
|
|
|
|
| 216 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
}
|
|
|
|
| 218 |
.output-box textarea {
|
| 219 |
-
font-family:
|
| 220 |
}
|
| 221 |
"""
|
| 222 |
|
| 223 |
|
| 224 |
-
with gr.Blocks(
|
| 225 |
-
title="OpenAI LLM Controls",
|
| 226 |
-
theme=gr.themes.Soft(),
|
| 227 |
-
css=custom_css,
|
| 228 |
-
) as demo:
|
| 229 |
gr.Markdown(
|
| 230 |
"""
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
""",
|
| 236 |
-
elem_id="main-title",
|
| 237 |
)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
"""
|
| 245 |
-
)
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
with gr.Row():
|
| 248 |
with gr.Column(scale=1):
|
| 249 |
gen_prompt = gr.Textbox(
|
|
@@ -251,53 +239,66 @@ with gr.Blocks(
|
|
| 251 |
label="Prompt",
|
| 252 |
value="Write a short LinkedIn post explaining why business leaders should learn AI. Maximum 120 words.",
|
| 253 |
)
|
|
|
|
| 254 |
gen_model = gr.Dropdown(
|
| 255 |
-
GENERATION_MODELS,
|
| 256 |
label="Model",
|
| 257 |
-
value=
|
|
|
|
|
|
|
| 258 |
)
|
| 259 |
-
|
|
|
|
| 260 |
lines=3,
|
| 261 |
label="System Message",
|
| 262 |
value="You are a helpful AI instructor. Keep answers clear and practical.",
|
| 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 |
gen_button = gr.Button("Generate", variant="primary")
|
| 302 |
|
| 303 |
with gr.Column(scale=1):
|
|
@@ -305,7 +306,6 @@ with gr.Blocks(
|
|
| 305 |
lines=22,
|
| 306 |
label="Output",
|
| 307 |
elem_classes=["output-box"],
|
| 308 |
-
show_copy_button=True,
|
| 309 |
)
|
| 310 |
|
| 311 |
gen_button.click(
|
|
@@ -313,55 +313,67 @@ with gr.Blocks(
|
|
| 313 |
inputs=[
|
| 314 |
gen_prompt,
|
| 315 |
gen_model,
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
],
|
| 324 |
outputs=gen_output,
|
| 325 |
)
|
| 326 |
|
| 327 |
with gr.Tab("Reasoning Controls"):
|
| 328 |
-
gr.Markdown(
|
| 329 |
-
"""
|
| 330 |
-
Use this tab for analysis, recommendations, technical trade-offs, planning, and decision-making tasks.
|
| 331 |
-
"""
|
| 332 |
-
)
|
| 333 |
-
|
| 334 |
with gr.Row():
|
| 335 |
with gr.Column(scale=1):
|
| 336 |
reason_prompt = gr.Textbox(
|
| 337 |
lines=9,
|
| 338 |
label="Prompt",
|
| 339 |
-
value=
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
)
|
|
|
|
| 347 |
reason_model = gr.Dropdown(
|
| 348 |
-
REASONING_MODELS,
|
| 349 |
label="Model",
|
| 350 |
-
value=
|
|
|
|
|
|
|
| 351 |
)
|
| 352 |
-
|
| 353 |
-
|
|
|
|
| 354 |
label="Reasoning Effort",
|
| 355 |
value="medium",
|
| 356 |
)
|
| 357 |
-
|
|
|
|
| 358 |
minimum=100,
|
| 359 |
maximum=8000,
|
| 360 |
-
step=
|
| 361 |
-
value=
|
| 362 |
label="Max Output Tokens",
|
| 363 |
)
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
reason_button = gr.Button("Reason", variant="primary")
|
| 366 |
|
| 367 |
with gr.Column(scale=1):
|
|
@@ -369,7 +381,6 @@ with gr.Blocks(
|
|
| 369 |
lines=22,
|
| 370 |
label="Output",
|
| 371 |
elem_classes=["output-box"],
|
| 372 |
-
show_copy_button=True,
|
| 373 |
)
|
| 374 |
|
| 375 |
reason_button.click(
|
|
@@ -377,17 +388,20 @@ with gr.Blocks(
|
|
| 377 |
inputs=[
|
| 378 |
reason_prompt,
|
| 379 |
reason_model,
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
],
|
| 384 |
outputs=reason_output,
|
| 385 |
)
|
| 386 |
|
| 387 |
|
| 388 |
if __name__ == "__main__":
|
| 389 |
-
demo.queue()
|
| 390 |
demo.launch(
|
|
|
|
|
|
|
| 391 |
server_name="0.0.0.0",
|
| 392 |
-
server_port=int(os.getenv("PORT",
|
| 393 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
|
| 6 |
+
# =========================
|
| 7 |
+
# Hugging Face Secret
|
| 8 |
+
# =========================
|
| 9 |
+
# Add this in Hugging Face Spaces:
|
| 10 |
+
# Settings → Secrets → New secret
|
| 11 |
+
# Name: OPENAI_API_KEY
|
| 12 |
+
# Value: your OpenAI API key
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
DEFAULT_GENERATION_MODEL = os.getenv("OPENAI_GENERATION_MODEL", "gpt-5.5")
|
| 16 |
+
DEFAULT_REASONING_MODEL = os.getenv("OPENAI_REASONING_MODEL", "gpt-5.5")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
GENERATION_MODELS = [
|
|
|
|
|
|
|
|
|
|
| 20 |
"gpt-5.5",
|
| 21 |
+
"gpt-5.1",
|
| 22 |
+
"gpt-5-mini",
|
| 23 |
+
"gpt-4.1",
|
| 24 |
+
"gpt-4.1-mini",
|
| 25 |
]
|
| 26 |
|
| 27 |
REASONING_MODELS = [
|
| 28 |
"gpt-5.5",
|
| 29 |
+
"gpt-5.1",
|
| 30 |
+
"gpt-5-mini",
|
| 31 |
+
"gpt-5-pro",
|
| 32 |
]
|
| 33 |
|
| 34 |
|
| 35 |
+
def get_openai_client():
|
| 36 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
if not api_key:
|
| 39 |
+
raise ValueError(
|
| 40 |
+
"OPENAI_API_KEY is missing. "
|
| 41 |
+
"Please add it in Hugging Face Spaces → Settings → Secrets."
|
| 42 |
+
)
|
| 43 |
|
| 44 |
+
return OpenAI(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
def is_gpt5_family(model: str) -> bool:
|
| 48 |
+
return model.startswith("gpt-5")
|
| 49 |
|
| 50 |
|
| 51 |
+
def extract_output_text(response):
|
| 52 |
"""
|
| 53 |
+
Safely extract text from OpenAI Responses API output.
|
|
|
|
| 54 |
"""
|
| 55 |
+
if hasattr(response, "output_text") and response.output_text:
|
| 56 |
+
return response.output_text
|
| 57 |
+
|
| 58 |
+
chunks = []
|
| 59 |
|
| 60 |
+
if hasattr(response, "output") and response.output:
|
| 61 |
+
for item in response.output:
|
| 62 |
+
if hasattr(item, "content") and item.content:
|
| 63 |
+
for content in item.content:
|
| 64 |
+
if hasattr(content, "text") and content.text:
|
| 65 |
+
chunks.append(content.text)
|
| 66 |
|
| 67 |
+
return "\n".join(chunks).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
def run_generation(
|
| 71 |
+
prompt,
|
| 72 |
+
model,
|
| 73 |
+
system_message,
|
| 74 |
+
temperature,
|
| 75 |
+
top_p,
|
| 76 |
+
max_output_tokens,
|
| 77 |
+
frequency_penalty,
|
| 78 |
+
presence_penalty,
|
| 79 |
+
show_settings,
|
| 80 |
+
):
|
| 81 |
+
try:
|
| 82 |
+
client = get_openai_client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
request_params = {
|
| 85 |
+
"model": model,
|
| 86 |
+
"instructions": system_message,
|
| 87 |
+
"input": prompt,
|
| 88 |
+
"max_output_tokens": int(max_output_tokens),
|
| 89 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# GPT-5 family models may reject custom temperature/top_p/penalties.
|
| 92 |
+
# Keep defaults for GPT-5 models to avoid unsupported_value errors.
|
| 93 |
+
if not is_gpt5_family(model):
|
| 94 |
+
request_params["temperature"] = float(temperature)
|
| 95 |
+
request_params["top_p"] = float(top_p)
|
| 96 |
+
request_params["frequency_penalty"] = float(frequency_penalty)
|
| 97 |
+
request_params["presence_penalty"] = float(presence_penalty)
|
| 98 |
+
|
| 99 |
+
response = client.responses.create(**request_params)
|
| 100 |
+
output = extract_output_text(response)
|
| 101 |
+
|
| 102 |
+
if not output:
|
| 103 |
+
output = "No output generated."
|
| 104 |
|
| 105 |
if show_settings:
|
| 106 |
+
settings = f"""
|
| 107 |
+
MODEL SETTINGS
|
| 108 |
+
--------------
|
| 109 |
+
Model: {model}
|
| 110 |
+
Max Output Tokens: {max_output_tokens}
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
if is_gpt5_family(model):
|
| 114 |
+
settings += """
|
| 115 |
+
Temperature: default only for GPT-5 family
|
| 116 |
+
Top P: default only for GPT-5 family
|
| 117 |
+
Frequency Penalty: default only for GPT-5 family
|
| 118 |
+
Presence Penalty: default only for GPT-5 family
|
| 119 |
+
"""
|
| 120 |
else:
|
| 121 |
+
settings += f"""
|
| 122 |
+
Temperature: {temperature}
|
| 123 |
+
Top P: {top_p}
|
| 124 |
+
Frequency Penalty: {frequency_penalty}
|
| 125 |
+
Presence Penalty: {presence_penalty}
|
| 126 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
settings += "\nOUTPUT\n------\n"
|
| 129 |
+
return settings + output
|
| 130 |
|
| 131 |
+
return output
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return f"Error:\n{str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
|
| 137 |
def run_reasoning(
|
| 138 |
+
prompt,
|
| 139 |
+
model,
|
| 140 |
+
reasoning_effort,
|
| 141 |
+
max_output_tokens,
|
| 142 |
+
show_settings,
|
| 143 |
+
):
|
| 144 |
+
try:
|
| 145 |
+
client = get_openai_client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
request_params = {
|
| 148 |
+
"model": model,
|
| 149 |
+
"input": prompt,
|
| 150 |
+
"max_output_tokens": int(max_output_tokens),
|
| 151 |
+
"reasoning": {
|
| 152 |
+
"effort": reasoning_effort
|
| 153 |
+
},
|
| 154 |
+
}
|
| 155 |
|
| 156 |
+
response = client.responses.create(**request_params)
|
| 157 |
+
output = extract_output_text(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
if not output:
|
| 160 |
+
output = "No output generated."
|
|
|
|
| 161 |
|
| 162 |
if show_settings:
|
| 163 |
+
settings = f"""
|
| 164 |
+
REASONING SETTINGS
|
| 165 |
+
------------------
|
| 166 |
+
Model: {model}
|
| 167 |
+
Reasoning Effort: {reasoning_effort}
|
| 168 |
+
Max Output Tokens: {max_output_tokens}
|
| 169 |
+
|
| 170 |
+
OUTPUT
|
| 171 |
+
------
|
| 172 |
+
"""
|
| 173 |
+
return settings + output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
return output
|
| 176 |
|
| 177 |
+
except Exception as e:
|
| 178 |
+
return f"Error:\n{str(e)}"
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
CSS = """
|
| 182 |
.gradio-container {
|
| 183 |
+
max-width: 1200px !important;
|
| 184 |
margin: auto !important;
|
| 185 |
}
|
| 186 |
+
|
| 187 |
+
.main-title {
|
| 188 |
text-align: center;
|
| 189 |
+
margin-bottom: 20px;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.helper-box {
|
| 193 |
+
padding: 14px;
|
| 194 |
+
border-radius: 12px;
|
| 195 |
+
background: #f7f7f8;
|
| 196 |
+
border: 1px solid #e5e7eb;
|
| 197 |
+
margin-bottom: 16px;
|
| 198 |
}
|
| 199 |
+
|
| 200 |
.output-box textarea {
|
| 201 |
+
font-family: monospace !important;
|
| 202 |
}
|
| 203 |
"""
|
| 204 |
|
| 205 |
|
| 206 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
gr.Markdown(
|
| 208 |
"""
|
| 209 |
+
<div class="main-title">
|
| 210 |
+
|
| 211 |
+
# LLM Generation & Reasoning Controls
|
| 212 |
+
|
| 213 |
+
Experiment with OpenAI model settings using a simple Gradio interface.
|
| 214 |
|
| 215 |
+
</div>
|
| 216 |
+
"""
|
|
|
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
+
gr.Markdown(
|
| 220 |
+
"""
|
| 221 |
+
<div class="helper-box">
|
| 222 |
+
|
| 223 |
+
<b>Important:</b> Add your OpenAI key in Hugging Face Spaces Secrets as:
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
<code>OPENAI_API_KEY</code>
|
| 226 |
+
|
| 227 |
+
GPT-5 family models may only support default values for temperature, top-p, and penalties.
|
| 228 |
+
This app automatically skips those settings for GPT-5 models to avoid API errors.
|
| 229 |
+
|
| 230 |
+
</div>
|
| 231 |
+
"""
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
with gr.Tab("Generation Controls"):
|
| 235 |
with gr.Row():
|
| 236 |
with gr.Column(scale=1):
|
| 237 |
gen_prompt = gr.Textbox(
|
|
|
|
| 239 |
label="Prompt",
|
| 240 |
value="Write a short LinkedIn post explaining why business leaders should learn AI. Maximum 120 words.",
|
| 241 |
)
|
| 242 |
+
|
| 243 |
gen_model = gr.Dropdown(
|
| 244 |
+
choices=GENERATION_MODELS,
|
| 245 |
label="Model",
|
| 246 |
+
value=DEFAULT_GENERATION_MODEL
|
| 247 |
+
if DEFAULT_GENERATION_MODEL in GENERATION_MODELS
|
| 248 |
+
else "gpt-5.5",
|
| 249 |
)
|
| 250 |
+
|
| 251 |
+
gen_system_message = gr.Textbox(
|
| 252 |
lines=3,
|
| 253 |
label="System Message",
|
| 254 |
value="You are a helpful AI instructor. Keep answers clear and practical.",
|
| 255 |
)
|
| 256 |
+
|
| 257 |
+
gen_temperature = gr.Slider(
|
| 258 |
+
minimum=0.0,
|
| 259 |
+
maximum=2.0,
|
| 260 |
+
step=0.01,
|
| 261 |
+
value=0.7,
|
| 262 |
+
label="Temperature",
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
gen_top_p = gr.Slider(
|
| 266 |
+
minimum=0.0,
|
| 267 |
+
maximum=1.0,
|
| 268 |
+
step=0.01,
|
| 269 |
+
value=1.0,
|
| 270 |
+
label="Top P",
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
gen_max_output_tokens = gr.Slider(
|
| 274 |
+
minimum=50,
|
| 275 |
+
maximum=4000,
|
| 276 |
+
step=50,
|
| 277 |
+
value=500,
|
| 278 |
+
label="Max Output Tokens",
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
gen_frequency_penalty = gr.Slider(
|
| 282 |
+
minimum=-2.0,
|
| 283 |
+
maximum=2.0,
|
| 284 |
+
step=0.01,
|
| 285 |
+
value=0.0,
|
| 286 |
+
label="Frequency Penalty",
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
gen_presence_penalty = gr.Slider(
|
| 290 |
+
minimum=-2.0,
|
| 291 |
+
maximum=2.0,
|
| 292 |
+
step=0.01,
|
| 293 |
+
value=0.0,
|
| 294 |
+
label="Presence Penalty",
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
gen_show_settings = gr.Checkbox(
|
| 298 |
+
value=True,
|
| 299 |
+
label="Show Settings",
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
gen_button = gr.Button("Generate", variant="primary")
|
| 303 |
|
| 304 |
with gr.Column(scale=1):
|
|
|
|
| 306 |
lines=22,
|
| 307 |
label="Output",
|
| 308 |
elem_classes=["output-box"],
|
|
|
|
| 309 |
)
|
| 310 |
|
| 311 |
gen_button.click(
|
|
|
|
| 313 |
inputs=[
|
| 314 |
gen_prompt,
|
| 315 |
gen_model,
|
| 316 |
+
gen_system_message,
|
| 317 |
+
gen_temperature,
|
| 318 |
+
gen_top_p,
|
| 319 |
+
gen_max_output_tokens,
|
| 320 |
+
gen_frequency_penalty,
|
| 321 |
+
gen_presence_penalty,
|
| 322 |
+
gen_show_settings,
|
| 323 |
],
|
| 324 |
outputs=gen_output,
|
| 325 |
)
|
| 326 |
|
| 327 |
with gr.Tab("Reasoning Controls"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
with gr.Row():
|
| 329 |
with gr.Column(scale=1):
|
| 330 |
reason_prompt = gr.Textbox(
|
| 331 |
lines=9,
|
| 332 |
label="Prompt",
|
| 333 |
+
value="""A telecom company wants to build an AI customer support assistant.
|
| 334 |
+
|
| 335 |
+
They have:
|
| 336 |
+
- 50,000 past support tickets
|
| 337 |
+
- A FAQ website
|
| 338 |
+
- Billing policies
|
| 339 |
+
- A small developer team
|
| 340 |
+
|
| 341 |
+
Should they start with:
|
| 342 |
+
1. Simple prompt-based chatbot
|
| 343 |
+
2. RAG chatbot
|
| 344 |
+
3. Fine-tuning
|
| 345 |
+
4. Agent with tools
|
| 346 |
+
|
| 347 |
+
Give a practical recommendation with trade-offs.""",
|
| 348 |
)
|
| 349 |
+
|
| 350 |
reason_model = gr.Dropdown(
|
| 351 |
+
choices=REASONING_MODELS,
|
| 352 |
label="Model",
|
| 353 |
+
value=DEFAULT_REASONING_MODEL
|
| 354 |
+
if DEFAULT_REASONING_MODEL in REASONING_MODELS
|
| 355 |
+
else "gpt-5.5",
|
| 356 |
)
|
| 357 |
+
|
| 358 |
+
reason_effort = gr.Radio(
|
| 359 |
+
choices=["low", "medium", "high"],
|
| 360 |
label="Reasoning Effort",
|
| 361 |
value="medium",
|
| 362 |
)
|
| 363 |
+
|
| 364 |
+
reason_max_output_tokens = gr.Slider(
|
| 365 |
minimum=100,
|
| 366 |
maximum=8000,
|
| 367 |
+
step=100,
|
| 368 |
+
value=1000,
|
| 369 |
label="Max Output Tokens",
|
| 370 |
)
|
| 371 |
+
|
| 372 |
+
reason_show_settings = gr.Checkbox(
|
| 373 |
+
value=True,
|
| 374 |
+
label="Show Settings",
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
reason_button = gr.Button("Reason", variant="primary")
|
| 378 |
|
| 379 |
with gr.Column(scale=1):
|
|
|
|
| 381 |
lines=22,
|
| 382 |
label="Output",
|
| 383 |
elem_classes=["output-box"],
|
|
|
|
| 384 |
)
|
| 385 |
|
| 386 |
reason_button.click(
|
|
|
|
| 388 |
inputs=[
|
| 389 |
reason_prompt,
|
| 390 |
reason_model,
|
| 391 |
+
reason_effort,
|
| 392 |
+
reason_max_output_tokens,
|
| 393 |
+
reason_show_settings,
|
| 394 |
],
|
| 395 |
outputs=reason_output,
|
| 396 |
)
|
| 397 |
|
| 398 |
|
| 399 |
if __name__ == "__main__":
|
|
|
|
| 400 |
demo.launch(
|
| 401 |
+
theme=gr.themes.Soft(),
|
| 402 |
+
css=CSS,
|
| 403 |
server_name="0.0.0.0",
|
| 404 |
+
server_port=int(os.getenv("PORT", 7860)),
|
| 405 |
+
debug=False,
|
| 406 |
+
share=False,
|
| 407 |
+
)
|