rawanessam commited on
Commit
14c2c0e
·
verified ·
1 Parent(s): 75bf5dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -13
app.py CHANGED
@@ -1,24 +1,64 @@
1
  import gradio as gr
2
  from MLStructFP import DbLoader
 
 
3
  import os
4
 
5
- def visualize_floor_plan(json_path):
6
- """Load and visualize a floor plan from the dataset."""
7
  try:
 
 
 
 
 
 
8
  db = DbLoader(json_path)
9
- summary = db.tabulate() # Get a summary of the dataset
10
- return summary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  except Exception as e:
12
- return f"Error: {str(e)}"
13
 
14
  # Gradio Interface
15
- demo = gr.Interface(
16
- fn=visualize_floor_plan,
17
- inputs=gr.File(label="Upload `fp.json` from MLSTRUCT-FP"),
18
- outputs=gr.Textbox(label="Dataset Summary"),
19
- title="MLSTRUCT-FP: Floor Plan Dataset Viewer",
20
- description="Upload `fp.json` to visualize floor plan data.",
21
- examples=[["test/data/fp.json"]] # Optional: Add example file
22
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  demo.launch()
 
1
  import gradio as gr
2
  from MLStructFP import DbLoader
3
+ import matplotlib.pyplot as plt
4
+ import tempfile
5
  import os
6
 
7
+ def visualize_floor_plan(json_file):
8
+ """Process uploaded fp.json and display floor plan + summary."""
9
  try:
10
+ # Save uploaded file temporarily
11
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as f:
12
+ f.write(json_file.read())
13
+ json_path = f.name
14
+
15
+ # Load dataset
16
  db = DbLoader(json_path)
17
+ floor_id = list(db.floor.keys())[0] # Get first floor
18
+ floor = db.floor[floor_id]
19
+
20
+ # Generate plot
21
+ plt.figure(figsize=(10, 8))
22
+ floor.plot_complex()
23
+ plt.title(f"Floor Plan ID: {floor_id}")
24
+ plot_path = "floor_plot.png"
25
+ plt.savefig(plot_path, bbox_inches="tight")
26
+ plt.close()
27
+
28
+ # Clean up
29
+ os.unlink(json_path)
30
+
31
+ return plot_path, db.tabulate()
32
+
33
  except Exception as e:
34
+ return None, f"Error: {str(e)}"
35
 
36
  # Gradio Interface
37
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
38
+ gr.Markdown("""
39
+ # 🏠 MLSTRUCT-FP Explorer
40
+ *Visualize architectural floor plans from the [MLSTRUCT-FP dataset](https://github.com/MLSTRUCT/MLSTRUCT-FP)*
41
+ """)
42
+
43
+ with gr.Row():
44
+ with gr.Column():
45
+ upload = gr.File(label="Upload fp.json", type="binary")
46
+ submit = gr.Button("Analyze", variant="primary")
47
+ with gr.Column():
48
+ plot = gr.Image(label="Floor Plan Visualization")
49
+ summary = gr.Textbox(label="Dataset Summary", interactive=False)
50
+
51
+ submit.click(
52
+ visualize_floor_plan,
53
+ inputs=upload,
54
+ outputs=[plot, summary]
55
+ )
56
+
57
+ gr.Examples(
58
+ examples=[[os.path.join("examples", "sample_fp.json")]], # Add example file
59
+ inputs=upload,
60
+ outputs=[plot, summary],
61
+ label="Try the example below ↓"
62
+ )
63
 
64
  demo.launch()