Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
import webbrowser
|
5 |
+
|
6 |
+
# Define the backend API URL (modify this to your backend's URL)
|
7 |
+
BACKEND_API_URL = "https://do0rmamu-truck-loading-poc.hf.space/submit_data/"
|
8 |
+
|
9 |
+
# Function to process the file and send data to the API
|
10 |
+
def process_file(file, truck_type, auto_suggest):
|
11 |
+
try:
|
12 |
+
# Debugging: Print when file is being processed
|
13 |
+
print(f"Processing file: {file.name}")
|
14 |
+
|
15 |
+
# Read the uploaded file into a Pandas DataFrame
|
16 |
+
if file.name.endswith('.csv'):
|
17 |
+
df = pd.read_csv(file.name)
|
18 |
+
elif file.name.endswith('.xls') or file.name.endswith('.xlsx'):
|
19 |
+
df = pd.read_excel(file.name)
|
20 |
+
else:
|
21 |
+
return "Unsupported file format. Please upload a CSV or Excel file."
|
22 |
+
|
23 |
+
print(f"File processed successfully, data:\n{df.head()}") # Print first few rows of the data
|
24 |
+
|
25 |
+
# Prepare consignments data from the DataFrame
|
26 |
+
consignments_data = []
|
27 |
+
grouped = df.groupby('ConsignmentNo')
|
28 |
+
for consignment_no, group in grouped:
|
29 |
+
consignment_boxes = [
|
30 |
+
{
|
31 |
+
'PieceLength': row['PieceLength'],
|
32 |
+
'PieceBreadth': row['PieceBreadth'],
|
33 |
+
'PieceHeight': row['PieceHeight'],
|
34 |
+
'Priority': row.get('Priority', 0)
|
35 |
+
}
|
36 |
+
for _, row in group.iterrows()
|
37 |
+
]
|
38 |
+
consignments_data.append({
|
39 |
+
'ConsignmentNo': str(consignment_no), # Convert ConsignmentNo to string
|
40 |
+
'Priority': group['Priority'].max(),
|
41 |
+
'boxes': consignment_boxes
|
42 |
+
})
|
43 |
+
|
44 |
+
print(f"Consignment data prepared:\n{consignments_data}")
|
45 |
+
|
46 |
+
# Prepare the JSON payload to be sent
|
47 |
+
json_data = {
|
48 |
+
'truck_name': truck_type,
|
49 |
+
'autoSuggest': auto_suggest,
|
50 |
+
'consignments_data': consignments_data
|
51 |
+
}
|
52 |
+
|
53 |
+
print(f"Sending the following data to backend:\n{json_data}")
|
54 |
+
|
55 |
+
# Send the data as JSON to the backend
|
56 |
+
response = requests.post(BACKEND_API_URL, json=json_data, allow_redirects=False)
|
57 |
+
|
58 |
+
print(f"Received response from backend, status code: {response.status_code}")
|
59 |
+
|
60 |
+
# Handle redirect (302 Found)
|
61 |
+
if response.status_code == 302:
|
62 |
+
redirect_url = response.headers.get('Location')
|
63 |
+
if redirect_url:
|
64 |
+
print(f"Redirecting to: {redirect_url}")
|
65 |
+
webbrowser.open(redirect_url)
|
66 |
+
return f"Redirecting to visualization: {redirect_url}"
|
67 |
+
elif response.status_code != 200:
|
68 |
+
return f"Failed to submit data. Status code: {response.status_code}\nError message: {response.text}"
|
69 |
+
|
70 |
+
return "Data submitted successfully."
|
71 |
+
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Exception occurred: {str(e)}")
|
74 |
+
return f"An error occurred: {str(e)}"
|
75 |
+
|
76 |
+
# Gradio interface
|
77 |
+
def gradio_interface():
|
78 |
+
# Define available truck types
|
79 |
+
truck_types = [
|
80 |
+
"TATA ACE", "ASHOK LEYLAND DOST", "MAHINDRA BOLERO PICK UP",
|
81 |
+
"ASHOK LEYLAND BADA DOST", "TATA 407", "EICHER 14 FEET",
|
82 |
+
"EICHER 17 FEET", "EICHER 19 FEET", "TATA 22 FEET",
|
83 |
+
"TATA TRUCK (6 TYRE)", "TAURUS 16 T (10 TYRE)", "TAURUS 21 T (12 TYRE)",
|
84 |
+
"TAURUS 25 T (14 TYRE)", "CONTAINER 20 FT", "CONTAINER 32 FT SXL",
|
85 |
+
"CONTAINER 32 FT MXL", "CONTAINER 32 FT SXL / MXL HQ",
|
86 |
+
"20 FEET OPEN ALL SIDE (ODC)", "28-32 FEET OPEN-TRAILOR JCB ODC",
|
87 |
+
"32 FEET OPEN-TRAILOR ODC", "40 FEET OPEN-TRAILOR ODC", "SCV", "LCV",
|
88 |
+
"ICV", "MCV"
|
89 |
+
]
|
90 |
+
|
91 |
+
with gr.Blocks() as demo:
|
92 |
+
# Title
|
93 |
+
gr.Markdown("## Truck Loading Data Submission")
|
94 |
+
|
95 |
+
# File Upload Input
|
96 |
+
file_input = gr.File(label="Upload your consignments file (CSV or Excel)")
|
97 |
+
|
98 |
+
# Truck Type Dropdown
|
99 |
+
truck_type_input = gr.Dropdown(truck_types, label="Select Truck Type")
|
100 |
+
|
101 |
+
# Auto-Suggest Checkbox
|
102 |
+
auto_suggest_input = gr.Checkbox(label="Auto-Suggest Truck", value=False)
|
103 |
+
|
104 |
+
# Submit Button
|
105 |
+
submit_button = gr.Button("Submit Data")
|
106 |
+
|
107 |
+
# Output Textbox
|
108 |
+
output_text = gr.Markdown()
|
109 |
+
|
110 |
+
# Define interaction
|
111 |
+
submit_button.click(process_file, [file_input, truck_type_input, auto_suggest_input], output_text)
|
112 |
+
|
113 |
+
return demo
|
114 |
+
|
115 |
+
# Run the Gradio interface
|
116 |
+
gradio_interface().launch()
|