Update app.py
Browse files
app.py
CHANGED
@@ -4,74 +4,35 @@ import traceback
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
import gradio as gr
|
7 |
-
from typing import Tuple, Optional, Any
|
8 |
|
9 |
# Import config first to ensure JAVA_HOME is set early
|
10 |
-
import config
|
11 |
|
12 |
-
from main_analyzer import analyze_pdf
|
13 |
# Import language_tool_python only for the test in __main__ if needed
|
14 |
-
# import language_tool_python
|
15 |
-
|
16 |
-
def process_upload(file_data_binary: Optional[Any]) -> Tuple[str, Optional[str]]: # Use Optional[Any] for Gradio File type="binary"
|
17 |
-
if file_data_binary is None or not hasattr(file_data_binary, 'read'): # Check if it's a file-like object
|
18 |
-
# Gradio's binary type for gr.File returns a tempfile._TemporaryFileWrapper object
|
19 |
-
# If it's None, no file was uploaded.
|
20 |
-
# If it's not None but doesn't have 'read', it's an unexpected type.
|
21 |
-
# However, gradio usually passes the bytes directly if type="binary" was used in older versions
|
22 |
-
# or a TemporaryFileWrapper which is file-like.
|
23 |
-
# For robustness, let's check if it's bytes.
|
24 |
-
if isinstance(file_data_binary, bytes):
|
25 |
-
pass # Good, it's bytes
|
26 |
-
elif file_data_binary is None:
|
27 |
-
return json.dumps({"error": "No file uploaded or file data is None"}, indent=2), None
|
28 |
-
elif not hasattr(file_data_binary, 'read'): # It's not None, not bytes, not file-like
|
29 |
-
return json.dumps({"error": f"Unexpected file data type: {type(file_data_binary)}"}), None
|
30 |
-
# If it has 'read', it's a file-like object, proceed.
|
31 |
-
|
32 |
-
# analyze_pdf now handles stream-to-temp-file logic internally via original_pdf_access_path
|
33 |
-
# So we can pass the file_data_binary (which is a file-like object from Gradio) directly.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
try:
|
43 |
print(f"App: Processing uploaded file...")
|
44 |
-
|
45 |
-
# analyze_pdf expects a path or a file-like object with read() and seek()
|
46 |
-
|
47 |
-
# Gradio with type="binary" gives a tempfile._TemporaryFileWrapper.
|
48 |
-
# This object is already file-like and can be passed directly.
|
49 |
-
# No need to create another temp file here in app.py if main_analyzer handles it.
|
50 |
-
|
51 |
-
# analyze_pdf will create its own temp file if it receives a stream.
|
52 |
-
results_dict, _ = analyze_pdf(file_data_binary)
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
results_json = json.dumps(results_dict, indent=2, ensure_ascii=False)
|
61 |
-
return results_json, None
|
62 |
|
63 |
except Exception as e:
|
64 |
print(f"Error in process_upload: {e}\n{traceback.format_exc()}")
|
65 |
error_message = json.dumps({"error": str(e), "traceback": traceback.format_exc()}, indent=2)
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
return error_message, None
|
73 |
-
# No finally block needed here for temp file, as analyze_pdf handles its own if it creates one
|
74 |
-
# and Gradio handles the temp file wrapper it provides.
|
75 |
|
76 |
|
77 |
def create_interface():
|
@@ -79,41 +40,50 @@ def create_interface():
|
|
79 |
with gr.Row():
|
80 |
file_input = gr.File(
|
81 |
label="Upload PDF",
|
82 |
-
file_types=[".pdf"]
|
83 |
-
# type="binary"
|
84 |
-
|
85 |
)
|
86 |
-
|
87 |
with gr.Row():
|
88 |
-
|
|
|
89 |
with gr.Row():
|
90 |
results_output = gr.JSON(
|
91 |
-
label="Analysis Results",
|
92 |
show_label=True
|
93 |
)
|
94 |
-
|
95 |
-
pdf_output = gr.File(
|
96 |
label="Annotated PDF (Placeholder - View Coordinates in JSON)",
|
97 |
show_label=True,
|
98 |
-
interactive=False
|
99 |
)
|
100 |
-
|
101 |
analyze_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
print("\n--- Launching Gradio Interface ---")
|
103 |
# config.set_java_home() is called when config.py is imported.
|
104 |
|
105 |
# Optional: Test LanguageTool initialization
|
106 |
try:
|
107 |
-
import language_tool_python
|
108 |
lt_test = language_tool_python.LanguageTool('en-US')
|
109 |
lt_test.close()
|
110 |
print("App: LanguageTool initialized successfully for test.")
|
111 |
except Exception as lt_e:
|
112 |
print(f"App: Warning: Could not initialize LanguageTool for test. Language checks might fail: {lt_e}")
|
113 |
print("Please ensure Java is installed and JAVA_HOME is correctly set (see config.py).")
|
114 |
-
|
115 |
app_interface = create_interface()
|
116 |
app_interface.launch(
|
117 |
-
share=False,
|
118 |
-
# server_port=7860
|
119 |
-
)
|
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
import gradio as gr
|
7 |
+
from typing import Tuple, Optional, Any # Added Any for file_data_binary
|
8 |
|
9 |
# Import config first to ensure JAVA_HOME is set early
|
10 |
+
import config
|
11 |
|
12 |
+
from main_analyzer import analyze_pdf
|
13 |
# Import language_tool_python only for the test in __main__ if needed
|
14 |
+
# import language_tool_python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
+
def process_upload(file_data_binary: Optional[Any]) -> Tuple[str, Optional[str]]:
|
18 |
+
if file_data_binary is None or not hasattr(file_data_binary, 'read'):
|
19 |
+
if isinstance(file_data_binary, bytes):
|
20 |
+
pass # Good, it's bytes
|
21 |
+
elif file_data_binary is None:
|
22 |
+
return json.dumps({"error": "No file uploaded or file data is None"}, indent=2), None
|
23 |
+
elif not hasattr(file_data_binary, 'read'):
|
24 |
+
return json.dumps({"error": f"Unexpected file data type: {type(file_data_binary)}"}), None
|
25 |
|
26 |
try:
|
27 |
print(f"App: Processing uploaded file...")
|
28 |
+
results_dict, _ = analyze_pdf(file_data_binary)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
results_json = json.dumps(results_dict, indent=2, ensure_ascii=False)
|
30 |
+
return results_json, None
|
31 |
|
32 |
except Exception as e:
|
33 |
print(f"Error in process_upload: {e}\n{traceback.format_exc()}")
|
34 |
error_message = json.dumps({"error": str(e), "traceback": traceback.format_exc()}, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return error_message, None
|
|
|
|
|
36 |
|
37 |
|
38 |
def create_interface():
|
|
|
40 |
with gr.Row():
|
41 |
file_input = gr.File(
|
42 |
label="Upload PDF",
|
43 |
+
file_types=[".pdf"]
|
44 |
+
# type="binary"
|
|
|
45 |
)
|
46 |
+
|
47 |
with gr.Row():
|
48 |
+
analyze_btn = gr.Button("Analyze PDF")
|
49 |
+
|
50 |
with gr.Row():
|
51 |
results_output = gr.JSON(
|
52 |
+
label="Analysis Results",
|
53 |
show_label=True
|
54 |
)
|
55 |
+
|
56 |
+
pdf_output = gr.File(
|
57 |
label="Annotated PDF (Placeholder - View Coordinates in JSON)",
|
58 |
show_label=True,
|
59 |
+
interactive=False
|
60 |
)
|
61 |
+
|
62 |
analyze_btn.click(
|
63 |
+
fn=process_upload,
|
64 |
+
inputs=file_input,
|
65 |
+
outputs=[results_output, pdf_output]
|
66 |
+
)
|
67 |
+
|
68 |
+
return interface
|
69 |
+
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
print("\n--- Launching Gradio Interface ---")
|
73 |
# config.set_java_home() is called when config.py is imported.
|
74 |
|
75 |
# Optional: Test LanguageTool initialization
|
76 |
try:
|
77 |
+
import language_tool_python
|
78 |
lt_test = language_tool_python.LanguageTool('en-US')
|
79 |
lt_test.close()
|
80 |
print("App: LanguageTool initialized successfully for test.")
|
81 |
except Exception as lt_e:
|
82 |
print(f"App: Warning: Could not initialize LanguageTool for test. Language checks might fail: {lt_e}")
|
83 |
print("Please ensure Java is installed and JAVA_HOME is correctly set (see config.py).")
|
84 |
+
|
85 |
app_interface = create_interface()
|
86 |
app_interface.launch(
|
87 |
+
share=False,
|
88 |
+
# server_port=7860
|
89 |
+
)
|