Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ifcopenshell
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from tempfile import NamedTemporaryFile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def extract_properties(ifc_file_path, element_class):
|
| 8 |
+
try:
|
| 9 |
+
model = ifcopenshell.open(ifc_file_path)
|
| 10 |
+
|
| 11 |
+
results = []
|
| 12 |
+
elements = model.by_type("IfcElement")
|
| 13 |
+
# Get unique type names
|
| 14 |
+
element_types = sorted(list(set(elem.is_a() for elem in elements)))
|
| 15 |
+
classes_to_check = [element_class] if element_class != "" else sorted(list(set(obj.is_a() for obj in element_types)))
|
| 16 |
+
|
| 17 |
+
for class_name in classes_to_check:
|
| 18 |
+
elements = model.by_type(class_name)
|
| 19 |
+
for element in elements:
|
| 20 |
+
# Get all properties
|
| 21 |
+
properties = {}
|
| 22 |
+
for definition in element.IsDefinedBy:
|
| 23 |
+
if definition.is_a("IfcRelDefinesByProperties"):
|
| 24 |
+
prop_set = definition.RelatingPropertyDefinition
|
| 25 |
+
if prop_set.is_a("IfcPropertySet"):
|
| 26 |
+
for prop in prop_set.HasProperties:
|
| 27 |
+
properties[f"{prop_set.Name}.{prop.Name}"] = prop.NominalValue.wrappedValue if prop.NominalValue else None
|
| 28 |
+
|
| 29 |
+
row = {
|
| 30 |
+
"GlobalId": element.GlobalId,
|
| 31 |
+
"Type": element.is_a(),
|
| 32 |
+
"Name": element.Name,
|
| 33 |
+
**properties
|
| 34 |
+
}
|
| 35 |
+
results.append(row)
|
| 36 |
+
|
| 37 |
+
if not results:
|
| 38 |
+
return pd.DataFrame({"Message": [f"No {element_class if element_class != '' else ''} elements found"]}), None, None
|
| 39 |
+
|
| 40 |
+
df = pd.DataFrame(results)
|
| 41 |
+
# Create temporary files for downloads
|
| 42 |
+
with NamedTemporaryFile(delete=False, suffix='.csv') as tmp_csv:
|
| 43 |
+
df.to_csv(tmp_csv.name, index=False)
|
| 44 |
+
csv_path = tmp_csv.name
|
| 45 |
+
|
| 46 |
+
with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp_excel:
|
| 47 |
+
df.to_excel(tmp_excel.name, index=False)
|
| 48 |
+
excel_path = tmp_excel.name
|
| 49 |
+
|
| 50 |
+
return df, csv_path, excel_path
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return pd.DataFrame({"Error": [f"Processing failed: {str(e)}"]}), None, None
|
| 54 |
+
|
| 55 |
+
def extract_ifc_types(ifc_file):
|
| 56 |
+
"""Extract all IFC types from uploaded file"""
|
| 57 |
+
if not ifc_file:
|
| 58 |
+
return []
|
| 59 |
+
try:
|
| 60 |
+
ifc = ifcopenshell.open(ifc_file.name)
|
| 61 |
+
# Get all elements that are IfcElement or its subtypes
|
| 62 |
+
elements = ifc.by_type("IfcElement")
|
| 63 |
+
# Get unique type names
|
| 64 |
+
element_types = sorted(list(set(elem.is_a() for elem in elements)))
|
| 65 |
+
return element_types
|
| 66 |
+
except:
|
| 67 |
+
return []
|
| 68 |
+
|
| 69 |
+
def update_dropdown(ifc_file):
|
| 70 |
+
"""Update dropdown options when file is uploaded"""
|
| 71 |
+
choices = extract_ifc_types(ifc_file)
|
| 72 |
+
return gr.Dropdown(choices=choices, value="", interactive=True)
|
| 73 |
+
|
| 74 |
+
# Gradio Interface
|
| 75 |
+
with gr.Blocks(title="IFC Property Extractor") as demo:
|
| 76 |
+
gr.Markdown("## 🛠️ IFC Property Extractor")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
ifc_input = gr.File(
|
| 80 |
+
label="1. Upload IFC File",
|
| 81 |
+
file_types=[".ifc"],
|
| 82 |
+
type="filepath"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column():
|
| 87 |
+
class_filter = gr.Dropdown(
|
| 88 |
+
[""],
|
| 89 |
+
value="",
|
| 90 |
+
label="2. Filter by Class",
|
| 91 |
+
interactive=False
|
| 92 |
+
)
|
| 93 |
+
extract_btn = gr.Button("Extract Properties", variant="primary")
|
| 94 |
+
|
| 95 |
+
with gr.Column():
|
| 96 |
+
csv_download = gr.File(label="Download CSV", visible=False)
|
| 97 |
+
excel_download = gr.File(label="Download Excel", visible=False)
|
| 98 |
+
|
| 99 |
+
output_table = gr.Dataframe(
|
| 100 |
+
label="Extracted Properties",
|
| 101 |
+
interactive=True,
|
| 102 |
+
wrap=False,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Update dropdown when file is uploaded
|
| 106 |
+
ifc_input.change(
|
| 107 |
+
fn=update_dropdown,
|
| 108 |
+
inputs=ifc_input,
|
| 109 |
+
outputs=[class_filter]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
extract_btn.click(
|
| 113 |
+
fn=extract_properties,
|
| 114 |
+
inputs=[ifc_input, class_filter],
|
| 115 |
+
outputs=[output_table, csv_download, excel_download]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Show download buttons only when files are available
|
| 119 |
+
extract_btn.click(
|
| 120 |
+
lambda: [gr.File(visible=True), gr.File(visible=True)],
|
| 121 |
+
outputs=[csv_download, excel_download]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
demo.launch()
|