Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
-
import
|
|
|
|
|
5 |
|
6 |
# --- Authentication Function ---
|
7 |
def authenticate_user(username, password):
|
@@ -14,9 +17,7 @@ def authenticate_user(username, password):
|
|
14 |
# --- Core Application Logic ---
|
15 |
def analyze_wod(file_obj, wod_type):
|
16 |
"""
|
17 |
-
This function
|
18 |
-
In a real application, this is where you would put your PDF parsing,
|
19 |
-
text extraction, and validation logic.
|
20 |
|
21 |
Args:
|
22 |
file_obj: The uploaded file object from Gradio.
|
@@ -25,59 +26,84 @@ def analyze_wod(file_obj, wod_type):
|
|
25 |
Returns:
|
26 |
A pandas DataFrame with the analysis results.
|
27 |
"""
|
28 |
-
#
|
29 |
-
if
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
# Simulate a processing delay
|
35 |
-
time.sleep(2)
|
36 |
-
|
37 |
-
# --- Dummy Data Generation ---
|
38 |
-
# This data simulates the results you would get from a real analysis.
|
39 |
-
# We include a "Fail" case to demonstrate how it would look.
|
40 |
-
data = {
|
41 |
-
"Requirement": [
|
42 |
-
"Merchant Front Photo",
|
43 |
-
"EDC Component Photo",
|
44 |
-
"EDC Placement Photo",
|
45 |
-
"Terminal Data Verification",
|
46 |
-
"Timestamped Photos Required",
|
47 |
-
"Clear Sales Draft",
|
48 |
-
"Date Consistency Check",
|
49 |
-
"PIC Signature/Handover",
|
50 |
-
"BAST/Handover Document",
|
51 |
-
],
|
52 |
-
"Reason / Location": [
|
53 |
-
"Available on Page 2 of the submission.",
|
54 |
-
"Found in Attachment 1, Photo A.",
|
55 |
-
"Available on Page 2, second photo.",
|
56 |
-
"Confirmed on the test transaction receipt, Page 3.",
|
57 |
-
"Timestamps are visible on all photos on Page 2.",
|
58 |
-
"Document not found; attachment is missing.",
|
59 |
-
"Dates on the cover sheet and Page 3 match.",
|
60 |
-
"Signature is on the Handover Form, Page 4.",
|
61 |
-
"The BAST is signed and available on Page 4.",
|
62 |
-
],
|
63 |
-
"Status": [
|
64 |
-
"PASS",
|
65 |
-
"PASS",
|
66 |
-
"PASS",
|
67 |
-
"PASS",
|
68 |
-
"PASS",
|
69 |
-
"FAIL",
|
70 |
-
"PASS",
|
71 |
-
"PASS",
|
72 |
-
"PASS",
|
73 |
-
],
|
74 |
-
}
|
75 |
|
76 |
-
#
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# --- Gradio User Interface Definition ---
|
83 |
# Using gr.Blocks() for a custom layout that matches the elegant design.
|
@@ -102,9 +128,9 @@ with gr.Blocks(
|
|
102 |
|
103 |
# Dropdown for WOD Type
|
104 |
type_input = gr.Dropdown(
|
105 |
-
["REPLACEMENT", "THERMAL", "VISIT", "PREVENTIVE_MAINTENANCE", "INSTALLATION", "WITHDRAWAL"],
|
106 |
label="Type",
|
107 |
-
value="
|
108 |
info="Select the type of work order."
|
109 |
)
|
110 |
|
@@ -117,12 +143,12 @@ with gr.Blocks(
|
|
117 |
|
118 |
# DataFrame to display the output, with styling for the 'Status' column
|
119 |
results_output = gr.DataFrame(
|
120 |
-
headers=["Requirement", "Reason
|
121 |
datatype=["str", "str", "str"],
|
122 |
-
# This part styles the 'Status' column based on its value
|
123 |
-
# It applies a green background for 'PASS' and a red one for 'FAIL'
|
124 |
interactive=False,
|
125 |
-
|
|
|
|
|
126 |
)
|
127 |
|
128 |
# Define the interaction: clicking the button calls the function
|
@@ -136,6 +162,9 @@ with gr.Blocks(
|
|
136 |
if __name__ == "__main__":
|
137 |
# The launch() command creates a web server with authentication enabled
|
138 |
# Users must provide the correct username and password to access the app
|
|
|
|
|
|
|
139 |
demo.launch(
|
140 |
auth=authenticate_user, # Enable authentication
|
141 |
auth_message="Please enter your credentials to access the WOD Analyzer",
|
|
|
1 |
import os
|
2 |
+
import json
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
+
from python_request import process_wod_document
|
6 |
+
|
7 |
+
from dummy import output_test
|
8 |
|
9 |
# --- Authentication Function ---
|
10 |
def authenticate_user(username, password):
|
|
|
17 |
# --- Core Application Logic ---
|
18 |
def analyze_wod(file_obj, wod_type):
|
19 |
"""
|
20 |
+
This function analyzes a Work Order Document using the remote API.
|
|
|
|
|
21 |
|
22 |
Args:
|
23 |
file_obj: The uploaded file object from Gradio.
|
|
|
26 |
Returns:
|
27 |
A pandas DataFrame with the analysis results.
|
28 |
"""
|
29 |
+
# Check if user has selected a valid WOD type
|
30 |
+
if wod_type == "-- WOD type --" or wod_type is None:
|
31 |
+
# Show warning dialog and return empty DataFrame
|
32 |
+
gr.Warning("Please select a WOD type first!")
|
33 |
+
return pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Check if file is uploaded
|
36 |
+
if file_obj is None:
|
37 |
+
gr.Warning("Please upload a PDF file first!")
|
38 |
+
return pd.DataFrame()
|
39 |
|
40 |
+
print(f"Analyzing '{file_obj.name}' (Type: {wod_type})...")
|
41 |
+
|
42 |
+
try:
|
43 |
+
# In modern Gradio versions, file_obj is already a path string
|
44 |
+
# We can use it directly or get the path from it
|
45 |
+
if hasattr(file_obj, 'name') and os.path.isfile(file_obj.name):
|
46 |
+
# file_obj has a .name attribute pointing to the temporary file
|
47 |
+
temp_file_path = file_obj.name
|
48 |
+
cleanup_needed = False
|
49 |
+
else:
|
50 |
+
# Fallback: assume file_obj is a path string
|
51 |
+
temp_file_path = str(file_obj)
|
52 |
+
cleanup_needed = False
|
53 |
+
|
54 |
+
# Process the document using the API
|
55 |
+
#api_response = process_wod_document(temp_file_path, wod_type)
|
56 |
+
|
57 |
+
api_response = json.loads(output_test)
|
58 |
+
|
59 |
+
# Clean up temporary file if we created one
|
60 |
+
if cleanup_needed:
|
61 |
+
os.unlink(temp_file_path)
|
62 |
+
|
63 |
+
# Check if API call was successful
|
64 |
+
if api_response.get("status") != "success":
|
65 |
+
error_msg = api_response.get("message", "Unknown error occurred")
|
66 |
+
gr.Error(f"API Error: {error_msg}")
|
67 |
+
return pd.DataFrame()
|
68 |
+
|
69 |
+
# Parse the API response
|
70 |
+
results = api_response.get("results", {})
|
71 |
+
summary = results.get("summary", {})
|
72 |
+
|
73 |
+
# Convert API response to DataFrame format
|
74 |
+
requirements = []
|
75 |
+
reasons = []
|
76 |
+
statuses = []
|
77 |
+
|
78 |
+
for requirement_name, details in summary.items():
|
79 |
+
requirements.append(requirement_name)
|
80 |
+
reasons.append(details.get("reasoning", ""))
|
81 |
+
# Convert true/false to PASS/FAIL
|
82 |
+
status_bool = details.get("status", "false")
|
83 |
+
if isinstance(status_bool, str):
|
84 |
+
status = "PASS" if status_bool.lower() == "true" else "FAIL"
|
85 |
+
else:
|
86 |
+
status = "PASS" if status_bool else "FAIL"
|
87 |
+
statuses.append(status)
|
88 |
+
|
89 |
+
# Create DataFrame
|
90 |
+
df = pd.DataFrame({
|
91 |
+
"Requirement": requirements,
|
92 |
+
"Reason": reasons,
|
93 |
+
"Status": statuses
|
94 |
+
})
|
95 |
+
|
96 |
+
# Show success message with prediction
|
97 |
+
prediction = results.get("prediction", "Unknown")
|
98 |
+
gr.Info(f"Analysis completed! Overall prediction: {prediction}")
|
99 |
+
|
100 |
+
return df
|
101 |
+
|
102 |
+
except Exception as e:
|
103 |
+
error_msg = f"Error processing document: {str(e)}"
|
104 |
+
print(error_msg)
|
105 |
+
gr.Error(error_msg)
|
106 |
+
return pd.DataFrame()
|
107 |
|
108 |
# --- Gradio User Interface Definition ---
|
109 |
# Using gr.Blocks() for a custom layout that matches the elegant design.
|
|
|
128 |
|
129 |
# Dropdown for WOD Type
|
130 |
type_input = gr.Dropdown(
|
131 |
+
["-- WOD type --", "REPLACEMENT", "THERMAL", "VISIT", "PREVENTIVE_MAINTENANCE", "INSTALLATION", "WITHDRAWAL"],
|
132 |
label="Type",
|
133 |
+
value="-- WOD type --",
|
134 |
info="Select the type of work order."
|
135 |
)
|
136 |
|
|
|
143 |
|
144 |
# DataFrame to display the output, with styling for the 'Status' column
|
145 |
results_output = gr.DataFrame(
|
146 |
+
headers=["Requirement", "Reason", "Status"],
|
147 |
datatype=["str", "str", "str"],
|
|
|
|
|
148 |
interactive=False,
|
149 |
+
max_height=1250,
|
150 |
+
column_widths=[30, 60, 10],
|
151 |
+
wrap=True
|
152 |
)
|
153 |
|
154 |
# Define the interaction: clicking the button calls the function
|
|
|
162 |
if __name__ == "__main__":
|
163 |
# The launch() command creates a web server with authentication enabled
|
164 |
# Users must provide the correct username and password to access the app
|
165 |
+
|
166 |
+
# demo.launch(debug=True)
|
167 |
+
|
168 |
demo.launch(
|
169 |
auth=authenticate_user, # Enable authentication
|
170 |
auth_message="Please enter your credentials to access the WOD Analyzer",
|