rawanessam commited on
Commit
8ce96e2
·
verified ·
1 Parent(s): 5ff3a3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -55
app.py CHANGED
@@ -1,64 +1,25 @@
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()
 
1
  import gradio as gr
 
 
 
2
  import os
3
+ from MLStructFP.db import DbLoader
4
 
5
+ # Path to the sample data (adjust if needed)
6
+ DB_PATH = os.path.join(os.path.dirname(__file__), "test", "data", "fp.json")
 
 
 
 
 
7
 
8
+ # Load the database once
9
+ db = DbLoader(DB_PATH)
 
 
10
 
11
+ def get_floor_ids():
12
+ return [f.id for f in db.floors]
 
 
 
 
 
13
 
14
+ def plot_floor(floor_id):
15
+ floor = db[floor_id]
16
+ fig = floor.plot_basic()
17
+ return fig
18
 
19
+ with gr.Blocks() as demo:
20
+ gr.Markdown("# MLStructFP Floor Plan Demo")
21
+ floor_id = gr.Dropdown(choices=get_floor_ids(), label="Select Floor ID")
22
+ plot = gr.Plot(label="Floor Plan Plot")
23
+ floor_id.change(fn=plot_floor, inputs=floor_id, outputs=plot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  demo.launch()