File size: 4,160 Bytes
fcaf4ec
 
d2c1f1a
 
fcaf4ec
 
 
 
 
d2c1f1a
fcaf4ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2c1f1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcaf4ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2c1f1a
 
 
 
 
 
 
fcaf4ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed62793
d2c1f1a
fcaf4ec
 
 
 
d2c1f1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import gradio as gr
import pandas as pd
import random
from datetime import datetime, timedelta
from groq import Groq

# Initialize Groq API
client = Groq(api_key="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")  # Replace with your Groq API key

# Predefined resource inference logic
def infer_resources(schedule):
    resource_map = {
        "Excavation": {"labor": 10, "equipment": "Excavator", "material": "Soil"},
        "Foundation": {"labor": 15, "equipment": "Concrete Mixer", "material": "Concrete"},
        "Framing": {"labor": 20, "equipment": "Cranes", "material": "Steel"},
        "Finishing": {"labor": 5, "equipment": "Hand Tools", "material": "Paint"}
    }

    inferred_resources = []
    for _, row in schedule.iterrows():
        task = row["task"]
        resources = resource_map.get(task, {"labor": 5, "equipment": "General", "material": "Standard"})
        inferred_resources.append({
            "task": task,
            "labor": resources["labor"],
            "equipment": resources["equipment"],
            "material": resources["material"]
        })

    return pd.DataFrame(inferred_resources)

# Fill missing columns
def fill_missing_columns(schedule):
    # Generate random dates if missing
    if "start_date" not in schedule.columns:
        schedule["start_date"] = [
            (datetime.now() + timedelta(days=random.randint(1, 30))).strftime("%Y-%m-%d")
            for _ in range(len(schedule))
        ]
    if "end_date" not in schedule.columns:
        schedule["end_date"] = [
            (datetime.strptime(start, "%Y-%m-%d") + timedelta(days=random.randint(5, 15))).strftime("%Y-%m-%d")
            for start in schedule["start_date"]
        ]
    return schedule

# Mock optimization logic
def mock_optimize_schedule(schedule_with_resources):
    optimized_schedule = []
    conflicts = []

    for _, row in schedule_with_resources.iterrows():
        task = row["task"]
        start_date = row["start_date"]
        end_date = row["end_date"]
        labor = row["labor"]
        equipment = row["equipment"]
        material = row["material"]

        # Check for conflicts (mock logic)
        if labor > 20:  # Example conflict condition
            conflicts.append(f"Task '{task}' exceeds labor capacity.")
        
        optimized_schedule.append({
            "task": task,
            "start_date": start_date,
            "end_date": end_date,
            "labor": labor,
            "equipment": equipment,
            "material": material,
            "conflict": "Yes" if f"Task '{task}' exceeds labor capacity." in conflicts else "No"
        })

    return pd.DataFrame(optimized_schedule), conflicts

# Main function for resource optimization
def optimize_resources(schedule_file):
    try:
        # Load schedule file
        schedule = pd.read_csv(schedule_file.name)

        # Ensure the 'task' column exists
        if "task" not in schedule.columns:
            raise ValueError("The uploaded schedule must contain a 'task' column.")

        # Fill missing columns
        schedule = fill_missing_columns(schedule)

        # Infer resources
        inferred_resources = infer_resources(schedule)
        schedule_with_resources = pd.concat([schedule, inferred_resources], axis=1)

        # Perform optimization (mocked for now)
        optimized_schedule_df, conflicts = mock_optimize_schedule(schedule_with_resources)

        return optimized_schedule_df, "\n".join(conflicts) if conflicts else "No conflicts detected."
    except Exception as e:
        return f"Error: {e}"

# Define Gradio interface
interface = gr.Interface(
    fn=optimize_resources,
    inputs=[
        gr.File(label="Upload Schedule File (CSV)")
    ],
    outputs=[
        gr.Dataframe(label="Optimized Schedule"),  # Tabular output
        gr.Textbox(label="Conflicts")  # Text output for conflict details
    ],
    title="Smart Construction Resource Loading",
    description="Upload a construction schedule with at least a 'task' column. The app will dynamically infer other details and optimize the schedule."
)

# Launch the app
if __name__ == "__main__":
    interface.launch()