gradio: add try/except logging for UI empty result
Browse files585 unmodified lines586587588589590591592593594595596597598599600601602603604605606607608609610611585 unmodified linesasync def gradio_solve(file_obj, prompt: str, max_new_tokens: float | None) -> str:with request_scope() as rid:if MODEL is None or TOKENIZER is None:raise gr.Error("model_loading")if file_obj is None:raise gr.Error("file is required (image/gif/webp/mp4)")if not prompt or not prompt.strip():raise gr.Error("prompt is required")tokens = DEFAULT_MAX_NEW_TOKENS if max_new_tokens is None else int(max_new_tokens)# file_obj is str path or dict? gr.File returns filepath stringfile_path = file_obj if isinstance(file_obj, str) else file_obj.name if hasattr(file_obj, 'name') else str(file_obj)# Read file bytestry:with open(file_path, "rb") as f:data = f.read()except Exception as e:raise gr.Error(f"read failed: {e}")hint = file_pathframes = await asyncio.to_thread(extract_frames_from_bytes, data, hint)frames = sample_frames(frames, MAX_FRAMES)logger.info(f"Gradio media: frames={len(frames)}")return await run_inference(frames, prompt.strip(), tokens)btn.click(gradio_solve, inputs=[inp_media, inp_prompt, inp_tokens], outputs=out_text, api_name="solve", concurrency_limit=None)585 unmodified lines586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620585 unmodified linesasync def gradio_solve(file_obj, prompt: str, max_new_tokens: float | None) -> str:with request_scope() as rid:try:if MODEL is None or TOKENIZER is None:raise gr.Error("model_loading")if file_obj is None:raise gr.Error("file is required (image/gif/webp/mp4)")if not prompt or not prompt.strip():raise gr.Error("prompt is required")tokens = DEFAULT_MAX_NEW_TOKENS if max_new_tokens is None else int(max_new_tokens)file_path = file_obj if isinstance(file_obj, str) else file_obj.name if hasattr(file_obj, 'name') else str(file_obj)try:with open(file_path, "rb") as f:data = f.read()except Exception as e:logger.error(f"Gradio read failed {file_path}: {e}", exc_info=True)raise gr.Error(f"read failed: {e}")hint = file_pathframes = await asyncio.to_thread(extract_frames_from_bytes, data, hint)frames = sample_frames(frames, MAX_FRAMES)logger.info(f"Gradio media: frames={len(frames)}, hint={hint}")result = await run_inference(frames, prompt.strip(), tokens)logger.info(f"Gradio result len={len(result) if result else 0} preview={(result[:80] if result else '')!r}")if not result:raise gr.Error("empty result from model")return resultexcept gr.Error:raiseexcept Exception as e:logger.error(f"Gradio solve unhandled: {e}", exc_info=True)raise gr.Error(f"solve failed: {str(e)[:200]}")btn.click(gradio_solve, inputs=[inp_media, inp_prompt, inp_tokens], outputs=out_text, api_name="solve", concurrency_limit=None)
app.py+27-18