Spaces:
Sleeping
Sleeping
Fix Codex Extractor button binding/status
Browse files
app.py
CHANGED
|
@@ -819,39 +819,66 @@ def run_codex_extraction(
|
|
| 819 |
author_name: str,
|
| 820 |
author_id: str,
|
| 821 |
works_sampled: str,
|
| 822 |
-
) -> tuple[str, str]:
|
| 823 |
"""
|
| 824 |
Gradio handler for the Codex Extraction tab.
|
| 825 |
Returns (report_text, json_output) tuple.
|
| 826 |
"""
|
| 827 |
if file_obj is None:
|
| 828 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 829 |
|
| 830 |
if not author_name.strip():
|
| 831 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 832 |
|
| 833 |
try:
|
| 834 |
report, fp_dict = process_upload(
|
| 835 |
-
file_path=
|
| 836 |
author_name=author_name.strip(),
|
| 837 |
author_id=author_id.strip() or "CA-XXX",
|
| 838 |
works_sampled=works_sampled.strip(),
|
| 839 |
)
|
| 840 |
|
| 841 |
if not fp_dict:
|
| 842 |
-
return report, ""
|
| 843 |
|
| 844 |
# Format JSON output for workbook entry
|
| 845 |
json_out = json.dumps(fp_dict, indent=2)
|
| 846 |
-
|
|
|
|
| 847 |
|
| 848 |
except Exception as e:
|
| 849 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 850 |
|
| 851 |
|
| 852 |
-
def clear_codex_form() -> tuple[None, str, str, str, str, str]:
|
| 853 |
"""Reset the Codex extraction form."""
|
| 854 |
-
return None, "", "CA-XXX", "", "", ""
|
| 855 |
|
| 856 |
|
| 857 |
# ββ WORKBOOK FUNCTIONS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -1008,6 +1035,13 @@ with gr.Blocks(title="TOTEM Studio", css=CSS) as demo:
|
|
| 1008 |
variant="secondary",
|
| 1009 |
scale=1,
|
| 1010 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1011 |
|
| 1012 |
gr.HTML("""
|
| 1013 |
<div style="
|
|
@@ -1088,12 +1122,13 @@ with gr.Blocks(title="TOTEM Studio", css=CSS) as demo:
|
|
| 1088 |
codex_extract_btn.click(
|
| 1089 |
run_codex_extraction,
|
| 1090 |
inputs=[codex_file, codex_author_name, codex_author_id, codex_works],
|
| 1091 |
-
outputs=[codex_report, codex_json],
|
|
|
|
| 1092 |
)
|
| 1093 |
codex_clear_btn.click(
|
| 1094 |
clear_codex_form,
|
| 1095 |
outputs=[codex_file, codex_author_name, codex_author_id, codex_works,
|
| 1096 |
-
codex_report, codex_json],
|
| 1097 |
)
|
| 1098 |
|
| 1099 |
|
|
|
|
| 819 |
author_name: str,
|
| 820 |
author_id: str,
|
| 821 |
works_sampled: str,
|
| 822 |
+
) -> tuple[str, str, str]:
|
| 823 |
"""
|
| 824 |
Gradio handler for the Codex Extraction tab.
|
| 825 |
Returns (report_text, json_output) tuple.
|
| 826 |
"""
|
| 827 |
if file_obj is None:
|
| 828 |
+
return (
|
| 829 |
+
"No file uploaded. Please upload a .txt or .pdf file.",
|
| 830 |
+
"",
|
| 831 |
+
"ERROR: No file uploaded.",
|
| 832 |
+
)
|
| 833 |
|
| 834 |
if not author_name.strip():
|
| 835 |
+
return (
|
| 836 |
+
"Please enter the author's full name before extracting.",
|
| 837 |
+
"",
|
| 838 |
+
"ERROR: Author name is required.",
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
# Gradio may pass a filepath string or a file-like payload depending on runtime.
|
| 842 |
+
file_path = file_obj
|
| 843 |
+
if isinstance(file_obj, dict):
|
| 844 |
+
file_path = file_obj.get("path") or file_obj.get("name")
|
| 845 |
+
elif hasattr(file_obj, "name"):
|
| 846 |
+
file_path = file_obj.name
|
| 847 |
+
|
| 848 |
+
if not file_path:
|
| 849 |
+
return (
|
| 850 |
+
"Unable to read uploaded file path. Please re-upload and try again.",
|
| 851 |
+
"",
|
| 852 |
+
"ERROR: File payload missing path.",
|
| 853 |
+
)
|
| 854 |
|
| 855 |
try:
|
| 856 |
report, fp_dict = process_upload(
|
| 857 |
+
file_path=file_path,
|
| 858 |
author_name=author_name.strip(),
|
| 859 |
author_id=author_id.strip() or "CA-XXX",
|
| 860 |
works_sampled=works_sampled.strip(),
|
| 861 |
)
|
| 862 |
|
| 863 |
if not fp_dict:
|
| 864 |
+
return report, "", "ERROR: Extraction failed. See report for details."
|
| 865 |
|
| 866 |
# Format JSON output for workbook entry
|
| 867 |
json_out = json.dumps(fp_dict, indent=2)
|
| 868 |
+
status = f"SUCCESS: Fingerprint extracted ({fp_dict.get('Sample_Words', 0)} words analysed)."
|
| 869 |
+
return report, json_out, status
|
| 870 |
|
| 871 |
except Exception as e:
|
| 872 |
+
return (
|
| 873 |
+
f"Extraction error: {type(e).__name__}: {str(e)}",
|
| 874 |
+
"",
|
| 875 |
+
f"ERROR: {type(e).__name__}",
|
| 876 |
+
)
|
| 877 |
|
| 878 |
|
| 879 |
+
def clear_codex_form() -> tuple[None, str, str, str, str, str, str]:
|
| 880 |
"""Reset the Codex extraction form."""
|
| 881 |
+
return None, "", "CA-XXX", "", "", "", "Ready."
|
| 882 |
|
| 883 |
|
| 884 |
# ββ WORKBOOK FUNCTIONS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1035 |
variant="secondary",
|
| 1036 |
scale=1,
|
| 1037 |
)
|
| 1038 |
+
codex_status = gr.Textbox(
|
| 1039 |
+
label="Extractor Status",
|
| 1040 |
+
lines=2,
|
| 1041 |
+
interactive=False,
|
| 1042 |
+
value="Ready.",
|
| 1043 |
+
placeholder="Status and extraction errors will appear here.",
|
| 1044 |
+
)
|
| 1045 |
|
| 1046 |
gr.HTML("""
|
| 1047 |
<div style="
|
|
|
|
| 1122 |
codex_extract_btn.click(
|
| 1123 |
run_codex_extraction,
|
| 1124 |
inputs=[codex_file, codex_author_name, codex_author_id, codex_works],
|
| 1125 |
+
outputs=[codex_report, codex_json, codex_status],
|
| 1126 |
+
queue=False,
|
| 1127 |
)
|
| 1128 |
codex_clear_btn.click(
|
| 1129 |
clear_codex_form,
|
| 1130 |
outputs=[codex_file, codex_author_name, codex_author_id, codex_works,
|
| 1131 |
+
codex_report, codex_json, codex_status],
|
| 1132 |
)
|
| 1133 |
|
| 1134 |
|