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 random
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
# Initialize Groq API
|
| 8 |
+
client = Groq(api_key="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") # Replace with your Groq API key
|
| 9 |
+
|
| 10 |
+
# Predefined resource inference logic
|
| 11 |
+
def infer_resources(schedule):
|
| 12 |
+
resource_map = {
|
| 13 |
+
"Excavation": {"labor": 10, "equipment": "Excavator", "material": "Soil"},
|
| 14 |
+
"Foundation": {"labor": 15, "equipment": "Concrete Mixer", "material": "Concrete"},
|
| 15 |
+
"Framing": {"labor": 20, "equipment": "Cranes", "material": "Steel"},
|
| 16 |
+
"Finishing": {"labor": 5, "equipment": "Hand Tools", "material": "Paint"}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
inferred_resources = []
|
| 20 |
+
for _, row in schedule.iterrows():
|
| 21 |
+
task = row["task"]
|
| 22 |
+
resources = resource_map.get(task, {"labor": 5, "equipment": "General", "material": "Standard"})
|
| 23 |
+
inferred_resources.append({
|
| 24 |
+
"task": task,
|
| 25 |
+
"labor": resources["labor"],
|
| 26 |
+
"equipment": resources["equipment"],
|
| 27 |
+
"material": resources["material"]
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
return pd.DataFrame(inferred_resources)
|
| 31 |
+
|
| 32 |
+
# Fill missing columns
|
| 33 |
+
def fill_missing_columns(schedule):
|
| 34 |
+
# Generate random dates if missing
|
| 35 |
+
if "start_date" not in schedule.columns:
|
| 36 |
+
schedule["start_date"] = [
|
| 37 |
+
(datetime.now() + timedelta(days=random.randint(1, 30))).strftime("%Y-%m-%d")
|
| 38 |
+
for _ in range(len(schedule))
|
| 39 |
+
]
|
| 40 |
+
if "end_date" not in schedule.columns:
|
| 41 |
+
schedule["end_date"] = [
|
| 42 |
+
(datetime.strptime(start, "%Y-%m-%d") + timedelta(days=random.randint(5, 15))).strftime("%Y-%m-%d")
|
| 43 |
+
for start in schedule["start_date"]
|
| 44 |
+
]
|
| 45 |
+
return schedule
|
| 46 |
+
|
| 47 |
+
# Mock optimization logic
|
| 48 |
+
def mock_optimize_schedule(schedule_with_resources):
|
| 49 |
+
optimized_schedule = []
|
| 50 |
+
conflicts = []
|
| 51 |
+
|
| 52 |
+
for _, row in schedule_with_resources.iterrows():
|
| 53 |
+
task = row["task"]
|
| 54 |
+
start_date = row["start_date"]
|
| 55 |
+
end_date = row["end_date"]
|
| 56 |
+
labor = row["labor"]
|
| 57 |
+
equipment = row["equipment"]
|
| 58 |
+
material = row["material"]
|
| 59 |
+
|
| 60 |
+
# Check for conflicts (mock logic)
|
| 61 |
+
if labor > 20: # Example conflict condition
|
| 62 |
+
conflicts.append(f"Task '{task}' exceeds labor capacity.")
|
| 63 |
+
|
| 64 |
+
optimized_schedule.append({
|
| 65 |
+
"task": task,
|
| 66 |
+
"start_date": start_date,
|
| 67 |
+
"end_date": end_date,
|
| 68 |
+
"labor": labor,
|
| 69 |
+
"equipment": equipment,
|
| 70 |
+
"material": material,
|
| 71 |
+
"conflict": "Yes" if f"Task '{task}' exceeds labor capacity." in conflicts else "No"
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
return pd.DataFrame(optimized_schedule), conflicts
|
| 75 |
+
|
| 76 |
+
# Main function for resource optimization
|
| 77 |
+
def optimize_resources(schedule_file):
|
| 78 |
+
try:
|
| 79 |
+
# Load schedule file
|
| 80 |
+
schedule = pd.read_csv(schedule_file.name)
|
| 81 |
+
|
| 82 |
+
# Ensure the 'task' column exists
|
| 83 |
+
if "task" not in schedule.columns:
|
| 84 |
+
raise ValueError("The uploaded schedule must contain a 'task' column.")
|
| 85 |
+
|
| 86 |
+
# Fill missing columns
|
| 87 |
+
schedule = fill_missing_columns(schedule)
|
| 88 |
+
|
| 89 |
+
# Infer resources
|
| 90 |
+
inferred_resources = infer_resources(schedule)
|
| 91 |
+
schedule_with_resources = pd.concat([schedule, inferred_resources], axis=1)
|
| 92 |
+
|
| 93 |
+
# Perform optimization (mocked for now)
|
| 94 |
+
optimized_schedule_df, conflicts = mock_optimize_schedule(schedule_with_resources)
|
| 95 |
+
|
| 96 |
+
return optimized_schedule_df, "\n".join(conflicts) if conflicts else "No conflicts detected."
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return f"Error: {e}"
|
| 99 |
+
|
| 100 |
+
# Define Gradio interface
|
| 101 |
+
interface = gr.Interface(
|
| 102 |
+
fn=optimize_resources,
|
| 103 |
+
inputs=[
|
| 104 |
+
gr.File(label="Upload Schedule File (CSV)")
|
| 105 |
+
],
|
| 106 |
+
outputs=[
|
| 107 |
+
gr.Dataframe(label="Optimized Schedule"), # Tabular output
|
| 108 |
+
gr.Textbox(label="Conflicts") # Text output for conflict details
|
| 109 |
+
],
|
| 110 |
+
title="Dynamic Intelligent Resource Loading",
|
| 111 |
+
description="Upload a construction schedule with at least a 'task' column. The app will dynamically infer other details and optimize the schedule."
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Launch the app
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
interface.launch()
|