Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import requests | |
| import webbrowser | |
| # Define the backend API URL (modify this to your backend's URL) | |
| BACKEND_API_URL = "https://do0rmamu-truck-loading-poc.hf.space/submit_data/" | |
| # Function to process the file and send data to the API | |
| def process_file(file, truck_type, auto_suggest): | |
| try: | |
| # Debugging: Print when file is being processed | |
| print(f"Processing file: {file.name}") | |
| # Read the uploaded file into a Pandas DataFrame | |
| if file.name.endswith('.csv'): | |
| df = pd.read_csv(file.name) | |
| elif file.name.endswith('.xls') or file.name.endswith('.xlsx'): | |
| df = pd.read_excel(file.name) | |
| else: | |
| return "Unsupported file format. Please upload a CSV or Excel file." | |
| print(f"File processed successfully, data:\n{df.head()}") # Print first few rows of the data | |
| # Define a destination mapping | |
| destination_mapping = { | |
| "Destination A": 1, | |
| "Destination B": 2, | |
| "Destination C": 3, | |
| "Destination D": 4, | |
| "Destination E": 5 | |
| } | |
| # Prepare consignments data from the DataFrame | |
| consignments_data = [] | |
| grouped = df.groupby('ConsignmentNo') | |
| for consignment_no, group in grouped: | |
| consignment_boxes = [ | |
| { | |
| 'PieceLength': float(row['PieceLength']), | |
| 'PieceBreadth': float(row['PieceBreadth']), | |
| 'PieceHeight': float(row['PieceHeight']), | |
| 'Priority': int(row.get('Priority', 0)), | |
| 'Destination': row.get('Destination', "Unknown") | |
| } | |
| for _, row in group.iterrows() | |
| ] | |
| consignments_data.append({ | |
| 'ConsignmentNo': str(consignment_no), # Convert ConsignmentNo to string | |
| 'Priority': int(group['Priority'].max()), # Ensure Priority is int | |
| 'Destination': group['Destination'].iloc[0], # Taking the destination of the first box | |
| 'boxes': consignment_boxes | |
| }) | |
| print(f"Consignment data prepared:\n{consignments_data}") | |
| # Prepare the JSON payload to be sent | |
| json_data = { | |
| 'truck_name': truck_type, | |
| 'autoSuggest': auto_suggest, | |
| 'consignments_data': consignments_data, | |
| 'destination_mapping': destination_mapping # Include destination mapping in payload | |
| } | |
| print(f"Sending the following data to backend:\n{json_data}") | |
| # Send the data as JSON to the backend | |
| response = requests.post(BACKEND_API_URL, json=json_data, allow_redirects=False) | |
| print(f"Received response from backend, status code: {response.status_code}") | |
| # Handle redirect (302 Found) | |
| if response.status_code == 302: | |
| redirect_url = response.headers.get('Location') | |
| if redirect_url: | |
| print(f"Redirecting to: {redirect_url}") | |
| webbrowser.open(redirect_url) | |
| return f"Redirecting to visualization: {redirect_url}" | |
| elif response.status_code != 200: | |
| return f"Failed to submit data. Status code: {response.status_code}\nError message: {response.text}" | |
| return "Data submitted successfully." | |
| except Exception as e: | |
| print(f"Exception occurred: {str(e)}") | |
| return f"An error occurred: {str(e)}" | |
| # Gradio interface | |
| def gradio_interface(): | |
| # Define available truck types | |
| truck_types = [ | |
| "TATA ACE", "ASHOK LEYLAND DOST", "MAHINDRA BOLERO PICK UP", | |
| "ASHOK LEYLAND BADA DOST", "TATA 407", "EICHER 14 FEET", | |
| "EICHER 17 FEET", "EICHER 19 FEET", "TATA 22 FEET", | |
| "TATA TRUCK (6 TYRE)", "TAURUS 16 T (10 TYRE)", "TAURUS 21 T (12 TYRE)", | |
| "TAURUS 25 T (14 TYRE)", "CONTAINER 20 FT", "CONTAINER 32 FT SXL", | |
| "CONTAINER 32 FT MXL", "CONTAINER 32 FT SXL / MXL HQ", | |
| "20 FEET OPEN ALL SIDE (ODC)", "28-32 FEET OPEN-TRAILOR JCB ODC", | |
| "32 FEET OPEN-TRAILOR ODC", "40 FEET OPEN-TRAILOR ODC", "SCV", "LCV", | |
| "ICV", "MCV" | |
| ] | |
| with gr.Blocks() as demo: | |
| # Title | |
| gr.Markdown("## Truck Loading Data Submission") | |
| # File Upload Input | |
| file_input = gr.File(label="Upload your consignments file (CSV or Excel)") | |
| # Truck Type Dropdown | |
| truck_type_input = gr.Dropdown(truck_types, label="Select Truck Type", value="TATA ACE") | |
| # Auto-Suggest Checkbox | |
| auto_suggest_input = gr.Checkbox(label="Auto-Suggest Truck", value=False) | |
| # Submit Button | |
| submit_button = gr.Button("Submit Data") | |
| # Output Textbox | |
| output_text = gr.Markdown() | |
| # Define interaction | |
| submit_button.click(process_file, [file_input, truck_type_input, auto_suggest_input], output_text) | |
| return demo | |
| # Run the Gradio interface | |
| gradio_interface().launch() | |