Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ 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
|
@@ -14,18 +14,26 @@ from main_analyzer import analyze_pdf
|
|
14 |
# import language_tool_python
|
15 |
|
16 |
|
17 |
-
def process_upload(file_data_binary: Optional[
|
18 |
-
if
|
19 |
-
if
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
return json.dumps({"error": f"Unexpected file data type: {type(file_data_binary)}"}), None
|
25 |
|
|
|
26 |
try:
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
results_json = json.dumps(results_dict, indent=2, ensure_ascii=False)
|
30 |
return results_json, None
|
31 |
|
@@ -33,6 +41,14 @@ def process_upload(file_data_binary: Optional[Any]) -> Tuple[str, Optional[str]]
|
|
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():
|
@@ -41,7 +57,7 @@ def create_interface():
|
|
41 |
file_input = gr.File(
|
42 |
label="Upload PDF",
|
43 |
file_types=[".pdf"],
|
44 |
-
type="binary"
|
45 |
)
|
46 |
|
47 |
with gr.Row():
|
@@ -86,4 +102,4 @@ if __name__ == "__main__":
|
|
86 |
app_interface.launch(
|
87 |
share=False,
|
88 |
# server_port=7860
|
89 |
-
)
|
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
import gradio as gr
|
7 |
+
from typing import Tuple, Optional, Any # Any for file_data_binary for now, though bytes is expected
|
8 |
|
9 |
# Import config first to ensure JAVA_HOME is set early
|
10 |
import config
|
|
|
14 |
# import language_tool_python
|
15 |
|
16 |
|
17 |
+
def process_upload(file_data_binary: Optional[bytes]) -> Tuple[str, Optional[str]]: # Explicitly Optional[bytes]
|
18 |
+
if not isinstance(file_data_binary, bytes):
|
19 |
+
if file_data_binary is None:
|
20 |
+
error_msg = "No file uploaded or file data is None."
|
21 |
+
else:
|
22 |
+
error_msg = f"Unexpected file data type: {type(file_data_binary)}. Expected bytes."
|
23 |
+
return json.dumps({"error": error_msg}, indent=2), None
|
|
|
24 |
|
25 |
+
temp_pdf_path = None
|
26 |
try:
|
27 |
+
# Create a temporary file to store the uploaded PDF bytes
|
28 |
+
# delete=False is used because analyze_pdf will open it by path.
|
29 |
+
# We are responsible for deleting it in the finally block.
|
30 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
31 |
+
temp_pdf_path = temp_file.name
|
32 |
+
temp_file.write(file_data_binary)
|
33 |
+
# The file is closed when exiting the 'with' block, but still exists due to delete=False.
|
34 |
+
|
35 |
+
print(f"App: Processing PDF via temporary file: {temp_pdf_path}")
|
36 |
+
results_dict, _ = analyze_pdf(temp_pdf_path) # Pass the path to the temporary file
|
37 |
results_json = json.dumps(results_dict, indent=2, ensure_ascii=False)
|
38 |
return results_json, None
|
39 |
|
|
|
41 |
print(f"Error in process_upload: {e}\n{traceback.format_exc()}")
|
42 |
error_message = json.dumps({"error": str(e), "traceback": traceback.format_exc()}, indent=2)
|
43 |
return error_message, None
|
44 |
+
finally:
|
45 |
+
# Clean up the temporary file if it was created
|
46 |
+
if temp_pdf_path and os.path.exists(temp_pdf_path):
|
47 |
+
try:
|
48 |
+
os.remove(temp_pdf_path)
|
49 |
+
print(f"App: Cleaned up temporary PDF file: {temp_pdf_path}")
|
50 |
+
except Exception as e_clean:
|
51 |
+
print(f"App: Error cleaning up temporary PDF file {temp_pdf_path}: {e_clean}")
|
52 |
|
53 |
|
54 |
def create_interface():
|
|
|
57 |
file_input = gr.File(
|
58 |
label="Upload PDF",
|
59 |
file_types=[".pdf"],
|
60 |
+
type="binary" # This ensures file_data_binary is bytes
|
61 |
)
|
62 |
|
63 |
with gr.Row():
|
|
|
102 |
app_interface.launch(
|
103 |
share=False,
|
104 |
# server_port=7860
|
105 |
+
)
|