Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import time | |
# --- Authentication Function --- | |
def authenticate_user(username, password): | |
""" | |
Simple authentication function. | |
In production, you should use more secure methods like hashed passwords. | |
""" | |
return username == "demo" and password == "demo" | |
# --- Core Application Logic --- | |
def analyze_wod(file_obj, wod_type): | |
""" | |
This function simulates the analysis of a Work Order Document. | |
In a real application, this is where you would put your PDF parsing, | |
text extraction, and validation logic. | |
Args: | |
file_obj: The uploaded file object from Gradio. | |
wod_type: The selected type of Work Order Document. | |
Returns: | |
A pandas DataFrame with the analysis results. | |
""" | |
# Print to console to show the function is running | |
if file_obj is not None: | |
print(f"Analyzing '{file_obj.name}' (Type: {wod_type})...") | |
else: | |
print(f"Analyzing with no file uploaded (Type: {wod_type})...") | |
# Simulate a processing delay | |
time.sleep(2) | |
# --- Dummy Data Generation --- | |
# This data simulates the results you would get from a real analysis. | |
# We include a "Fail" case to demonstrate how it would look. | |
data = { | |
"Requirement": [ | |
"Merchant Front Photo", | |
"EDC Component Photo", | |
"EDC Placement Photo", | |
"Terminal Data Verification", | |
"Timestamped Photos Required", | |
"Clear Sales Draft", | |
"Date Consistency Check", | |
"PIC Signature/Handover", | |
"BAST/Handover Document", | |
], | |
"Reason / Location": [ | |
"Available on Page 2 of the submission.", | |
"Found in Attachment 1, Photo A.", | |
"Available on Page 2, second photo.", | |
"Confirmed on the test transaction receipt, Page 3.", | |
"Timestamps are visible on all photos on Page 2.", | |
"Document not found; attachment is missing.", | |
"Dates on the cover sheet and Page 3 match.", | |
"Signature is on the Handover Form, Page 4.", | |
"The BAST is signed and available on Page 4.", | |
], | |
"Status": [ | |
"PASS", | |
"PASS", | |
"PASS", | |
"PASS", | |
"PASS", | |
"FAIL", | |
"PASS", | |
"PASS", | |
"PASS", | |
], | |
} | |
# Create a pandas DataFrame from the dummy data | |
df = pd.DataFrame(data) | |
# Return the DataFrame to be displayed in the Gradio interface | |
return df | |
# --- Gradio User Interface Definition --- | |
# Using gr.Blocks() for a custom layout that matches the elegant design. | |
with gr.Blocks( | |
#theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), | |
css=".gradio-container {max-width: 960px !important; margin: auto !important;}" | |
) as demo: | |
# Main Title and Description | |
gr.Markdown( | |
""" | |
# WOD Analyzer | |
Upload a Work Order Document to automatically check for requirements. | |
""" | |
) | |
# Input Section | |
with gr.Row(): | |
# File Upload Component | |
file_input = gr.File(label="Upload WOD PDF") | |
# Dropdown for WOD Type | |
type_input = gr.Dropdown( | |
["REPLACEMENT", "NEW", "MAINTENANCE", "PULL OUT"], | |
label="Type", | |
value="REPLACEMENT", | |
info="Select the type of work order." | |
) | |
# Action Button | |
analyze_btn = gr.Button("Analyze Document", variant="primary") | |
# Results Section | |
gr.Markdown("---") | |
gr.Markdown("## Results") | |
# DataFrame to display the output, with styling for the 'Status' column | |
results_output = gr.DataFrame( | |
headers=["Requirement", "Reason / Location", "Status"], | |
datatype=["str", "str", "str"], | |
# This part styles the 'Status' column based on its value | |
# It applies a green background for 'PASS' and a red one for 'FAIL' | |
interactive=False, | |
col_count=(3, "fixed"), | |
) | |
# Define the interaction: clicking the button calls the function | |
analyze_btn.click( | |
fn=analyze_wod, | |
inputs=[file_input, type_input], | |
outputs=[results_output] | |
) | |
# --- Launch the Application with Authentication --- | |
if __name__ == "__main__": | |
# The launch() command creates a web server with authentication enabled | |
# Users must provide the correct username and password to access the app | |
demo.launch( | |
auth=authenticate_user, # Enable authentication | |
auth_message="Please enter your credentials to access the WOD Analyzer", | |
share=True, | |
ssr_mode=False, | |
) |