ClinAssist Bot commited on
Commit ·
ff643be
1
Parent(s): 8d5d147
Fix upload-trigger API path and harden Gradio endpoint compatibility
Browse files- app.py +32 -5
- src/upload_io.py +6 -0
- tests/test_upload_io.py +5 -0
app.py
CHANGED
|
@@ -5,6 +5,7 @@ Tactical monochrome interface with robust upload and model routing.
|
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
|
|
|
| 8 |
import tempfile
|
| 9 |
from pathlib import Path
|
| 10 |
from typing import Any
|
|
@@ -711,9 +712,25 @@ with gr.Blocks(
|
|
| 711 |
export_file = gr.File(label="Download report artifact", visible=False)
|
| 712 |
export_status = gr.Markdown("**Export:** No artifact generated yet.", elem_classes=["status-line"])
|
| 713 |
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
|
| 718 |
btn_generate.click(
|
| 719 |
fn=generate_report,
|
|
@@ -749,12 +766,22 @@ with gr.Blocks(
|
|
| 749 |
export_file,
|
| 750 |
export_status,
|
| 751 |
],
|
|
|
|
|
|
|
| 752 |
)
|
| 753 |
|
| 754 |
gr.HTML('<div class="tac-custom-footer">© 2026 NorthernTribe Research, Inc. All rights reserved.</div>')
|
| 755 |
|
| 756 |
|
| 757 |
-
|
|
|
|
|
|
|
|
|
|
| 758 |
|
| 759 |
if __name__ == "__main__":
|
| 760 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
| 8 |
+
import inspect
|
| 9 |
import tempfile
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import Any
|
|
|
|
| 712 |
export_file = gr.File(label="Download report artifact", visible=False)
|
| 713 |
export_status = gr.Markdown("**Export:** No artifact generated yet.", elem_classes=["status-line"])
|
| 714 |
|
| 715 |
+
# Prefer `change` for broad Gradio compatibility; keep `upload` as fallback.
|
| 716 |
+
file_upload.change(
|
| 717 |
+
fn=on_file_upload,
|
| 718 |
+
inputs=file_upload,
|
| 719 |
+
outputs=[inp, ingest_status],
|
| 720 |
+
queue=False,
|
| 721 |
+
api_name="ingest_file",
|
| 722 |
+
)
|
| 723 |
+
if hasattr(file_upload, "upload"):
|
| 724 |
+
file_upload.upload(
|
| 725 |
+
fn=on_file_upload,
|
| 726 |
+
inputs=file_upload,
|
| 727 |
+
outputs=[inp, ingest_status],
|
| 728 |
+
queue=False,
|
| 729 |
+
api_name="ingest_file_upload",
|
| 730 |
+
)
|
| 731 |
+
|
| 732 |
+
btn_sample_text.click(fn=on_sample_text, outputs=[inp, ingest_status], queue=False, api_name="load_sample_text")
|
| 733 |
+
btn_sample_json.click(fn=on_sample_json, outputs=[inp, ingest_status], queue=False, api_name="load_sample_json")
|
| 734 |
|
| 735 |
btn_generate.click(
|
| 736 |
fn=generate_report,
|
|
|
|
| 766 |
export_file,
|
| 767 |
export_status,
|
| 768 |
],
|
| 769 |
+
queue=False,
|
| 770 |
+
api_name="clear_console",
|
| 771 |
)
|
| 772 |
|
| 773 |
gr.HTML('<div class="tac-custom-footer">© 2026 NorthernTribe Research, Inc. All rights reserved.</div>')
|
| 774 |
|
| 775 |
|
| 776 |
+
_queue_kwargs: dict[str, Any] = {"max_size": 16}
|
| 777 |
+
if "api_open" in inspect.signature(demo.queue).parameters:
|
| 778 |
+
_queue_kwargs["api_open"] = True
|
| 779 |
+
demo.queue(**_queue_kwargs)
|
| 780 |
|
| 781 |
if __name__ == "__main__":
|
| 782 |
+
_launch_kwargs: dict[str, Any] = {"server_name": "0.0.0.0"}
|
| 783 |
+
if "show_api" in inspect.signature(demo.launch).parameters:
|
| 784 |
+
_launch_kwargs["show_api"] = True
|
| 785 |
+
if "show_error" in inspect.signature(demo.launch).parameters:
|
| 786 |
+
_launch_kwargs["show_error"] = True
|
| 787 |
+
demo.launch(**_launch_kwargs)
|
src/upload_io.py
CHANGED
|
@@ -15,6 +15,12 @@ def resolve_uploaded_path(file_obj: Any) -> Path | None:
|
|
| 15 |
if file_obj is None:
|
| 16 |
return None
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if isinstance(file_obj, (str, Path)):
|
| 19 |
return Path(file_obj)
|
| 20 |
|
|
|
|
| 15 |
if file_obj is None:
|
| 16 |
return None
|
| 17 |
|
| 18 |
+
# Some Gradio payloads return a list even for single-file mode.
|
| 19 |
+
if isinstance(file_obj, list):
|
| 20 |
+
if not file_obj:
|
| 21 |
+
return None
|
| 22 |
+
return resolve_uploaded_path(file_obj[0])
|
| 23 |
+
|
| 24 |
if isinstance(file_obj, (str, Path)):
|
| 25 |
return Path(file_obj)
|
| 26 |
|
tests/test_upload_io.py
CHANGED
|
@@ -39,3 +39,8 @@ def test_read_uploaded_text_with_missing_file():
|
|
| 39 |
content, status = read_uploaded_text("/tmp/this_file_should_not_exist_123.log")
|
| 40 |
assert content == ""
|
| 41 |
assert "path not found" in status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
content, status = read_uploaded_text("/tmp/this_file_should_not_exist_123.log")
|
| 40 |
assert content == ""
|
| 41 |
assert "path not found" in status
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_resolve_uploaded_path_from_list_payload():
|
| 45 |
+
p = resolve_uploaded_path(["/tmp/example-from-list.log"])
|
| 46 |
+
assert p == Path("/tmp/example-from-list.log")
|