Spaces:
Running
Running
Add some limits to uploaded files and a max to send to the model
Browse files
app.py
CHANGED
|
@@ -40,6 +40,9 @@ openai_client = None
|
|
| 40 |
USE_RANDOM_ENDPOINT = False
|
| 41 |
endpoint_rotation_count = 0
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def app_loaded(state, request: gr.Request):
|
| 45 |
message_html = setup_model(DEFAULT_MODEL_NAME, intial=False)
|
|
@@ -160,6 +163,11 @@ def run_chat_inference(history, message, state):
|
|
| 160 |
yield history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
| 161 |
return history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
try:
|
| 164 |
# Check if the message is empty
|
| 165 |
if not message.strip() and len(files) == 0:
|
|
@@ -343,6 +351,24 @@ def run_chat_inference(history, message, state):
|
|
| 343 |
|
| 344 |
log_debug(f"sending api_messages to model {model_name}: {api_messages}")
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
stream = openai_client.chat.completions.create(
|
| 347 |
model=model_name,
|
| 348 |
messages=api_messages,
|
|
@@ -598,5 +624,5 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 598 |
api_name=False
|
| 599 |
)
|
| 600 |
|
| 601 |
-
demo.queue(default_concurrency_limit=2).launch(ssr_mode=False, show_api=False)
|
| 602 |
log_info("Gradio app launched")
|
|
|
|
| 40 |
USE_RANDOM_ENDPOINT = False
|
| 41 |
endpoint_rotation_count = 0
|
| 42 |
|
| 43 |
+
# Maximum number of image messages allowed per request
|
| 44 |
+
MAX_IMAGE_MESSAGES = 5
|
| 45 |
+
|
| 46 |
|
| 47 |
def app_loaded(state, request: gr.Request):
|
| 48 |
message_html = setup_model(DEFAULT_MODEL_NAME, intial=False)
|
|
|
|
| 163 |
yield history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
| 164 |
return history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state
|
| 165 |
|
| 166 |
+
# Enforce maximum number of files/images per request
|
| 167 |
+
if len(files) > MAX_IMAGE_MESSAGES:
|
| 168 |
+
gr.Warning(f"Too many images provided; keeping only the first {MAX_IMAGE_MESSAGES} file(s).")
|
| 169 |
+
files = files[:MAX_IMAGE_MESSAGES]
|
| 170 |
+
|
| 171 |
try:
|
| 172 |
# Check if the message is empty
|
| 173 |
if not message.strip() and len(files) == 0:
|
|
|
|
| 351 |
|
| 352 |
log_debug(f"sending api_messages to model {model_name}: {api_messages}")
|
| 353 |
|
| 354 |
+
# Ensure we don't send too many images (count only messages whose content is a list of parts)
|
| 355 |
+
image_msg_indices = [
|
| 356 |
+
i for i, msg in enumerate(api_messages)
|
| 357 |
+
if isinstance(msg, dict) and isinstance(msg.get('content'), list)
|
| 358 |
+
]
|
| 359 |
+
image_count = len(image_msg_indices)
|
| 360 |
+
if image_count > MAX_IMAGE_MESSAGES:
|
| 361 |
+
# Remove oldest image messages until we have MAX_IMAGE_MESSAGES or fewer
|
| 362 |
+
to_remove = image_count - MAX_IMAGE_MESSAGES
|
| 363 |
+
removed = 0
|
| 364 |
+
for idx in image_msg_indices:
|
| 365 |
+
if removed >= to_remove:
|
| 366 |
+
break
|
| 367 |
+
# Pop considering prior removals shift indices
|
| 368 |
+
api_messages.pop(idx - removed)
|
| 369 |
+
removed += 1
|
| 370 |
+
gr.Warning(f"Too many images provided; keeping the latest {MAX_IMAGE_MESSAGES} and dropped {removed} older image message(s).")
|
| 371 |
+
|
| 372 |
stream = openai_client.chat.completions.create(
|
| 373 |
model=model_name,
|
| 374 |
messages=api_messages,
|
|
|
|
| 624 |
api_name=False
|
| 625 |
)
|
| 626 |
|
| 627 |
+
demo.queue(default_concurrency_limit=2).launch(ssr_mode=False, show_api=False, max_file_size="10mb")
|
| 628 |
log_info("Gradio app launched")
|