Spaces:
Running
Running
Commit ·
2d42f7a
1
Parent(s): d055fb3
Add some limits to uploaded files and a max to send to the model
Browse files39 unmodified lines404142434445114 unmodified lines160161162163164165177 unmodified lines343344345346347348249 unmodified lines59859960060160239 unmodified linesUSE_RANDOM_ENDPOINT = Falseendpoint_rotation_count = 0def app_loaded(state, request: gr.Request):message_html = setup_model(DEFAULT_MODEL_NAME, intial=False)114 unmodified linesyield history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, statereturn history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, statetry:# Check if the message is emptyif not message.strip() and len(files) == 0:177 unmodified lineslog_debug(f"sending api_messages to model {model_name}: {api_messages}")stream = openai_client.chat.completions.create(model=model_name,messages=api_messages,249 unmodified linesapi_name=False)demo.queue(default_concurrency_limit=2).launch(ssr_mode=False, show_api=False)log_info("Gradio app launched")39 unmodified lines404142434445464748114 unmodified lines163164165166167168169170171172173177 unmodified lines351352353354355356357358359360361362363364365366367368369370371372373374249 unmodified lines62462562662762839 unmodified linesUSE_RANDOM_ENDPOINT = Falseendpoint_rotation_count = 0# Maximum number of image messages allowed per requestMAX_IMAGE_MESSAGES = 5def app_loaded(state, request: gr.Request):message_html = setup_model(DEFAULT_MODEL_NAME, intial=False)114 unmodified linesyield history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, statereturn history, INPUT_ENABLED, SEND_BUTTON_ENABLED, STOP_BUTTON_DISABLED, BUTTON_ENABLED, state# Enforce maximum number of files/images per requestif len(files) > MAX_IMAGE_MESSAGES:gr.Warning(f"Too many images provided; keeping only the first {MAX_IMAGE_MESSAGES} file(s).")files = files[:MAX_IMAGE_MESSAGES]try:# Check if the message is emptyif not message.strip() and len(files) == 0:177 unmodified lineslog_debug(f"sending api_messages to model {model_name}: {api_messages}")# Ensure we don't send too many images (count only messages whose content is a list of parts)image_msg_indices = [i for i, msg in enumerate(api_messages)if isinstance(msg, dict) and isinstance(msg.get('content'), list)]image_count = len(image_msg_indices)if image_count > MAX_IMAGE_MESSAGES:# Remove oldest image messages until we have MAX_IMAGE_MESSAGES or fewerto_remove = image_count - MAX_IMAGE_MESSAGESremoved = 0for idx in image_msg_indices:if removed >= to_remove:break# Pop considering prior removals shift indicesapi_messages.pop(idx - removed)removed += 1gr.Warning(f"Too many images provided; keeping the latest {MAX_IMAGE_MESSAGES} and dropped {removed} older image message(s).")stream = openai_client.chat.completions.create(model=model_name,messages=api_messages,249 unmodified linesapi_name=False)demo.queue(default_concurrency_limit=2).launch(ssr_mode=False, show_api=False, max_file_size="10mb")log_info("Gradio app launched")
app.py+27-1