Pushpak21 commited on
Commit
1480875
·
verified ·
1 Parent(s): 59b0d5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -35
app.py CHANGED
@@ -1,12 +1,13 @@
1
- import streamlit as st
2
- import os
 
3
  import json
4
 
5
- st.set_page_config(page_title="DCM Assembly Visualizer", layout="wide")
6
- st.title("DCM Assembly Visualizer")
7
 
8
  def parse_dcm_assembly(uploaded_file):
9
- lines = uploaded_file.read().decode("utf-8").splitlines()
10
 
11
  geometry = {}
12
  parts = []
@@ -16,19 +17,16 @@ def parse_dcm_assembly(uploaded_file):
16
  temp_part = {}
17
  temp_constraint = {}
18
  current_class = None
19
- current_id = None
20
 
21
- plane_label_map = {} # tag → label (from WORK_PLANE)
22
- plane_point_map = {} # tag → base point + normal
23
 
24
  for line in lines:
25
  line = line.strip()
26
-
27
  if line.startswith("CLASS"):
28
  parts_line = line.split()
29
  current_class = parts_line[1]
30
  current_id = int(parts_line[2])
31
-
32
  if current_class == "PLANE":
33
  temp_geom = {"tag": current_id}
34
  elif current_class == "WORK_PLANE":
@@ -72,7 +70,6 @@ def parse_dcm_assembly(uploaded_file):
72
  if "from" in temp_constraint and "to" in temp_constraint:
73
  constraints[(temp_constraint["from"], temp_constraint["to"])] = temp_constraint["label"]
74
 
75
- # Build final geometry dict
76
  final_geometry = {}
77
  for tag, data in plane_point_map.items():
78
  final_geometry[tag] = {
@@ -81,7 +78,6 @@ def parse_dcm_assembly(uploaded_file):
81
  "label": plane_label_map.get(tag, "")
82
  }
83
 
84
- # Build final constraint list
85
  constraint_list = [{"from": f, "to": t, "label": lbl} for (f, t), lbl in constraints.items()]
86
 
87
  return {
@@ -90,26 +86,9 @@ def parse_dcm_assembly(uploaded_file):
90
  "constraints": constraint_list
91
  }
92
 
93
-
94
- # === File Upload ===
95
- file1 = st.file_uploader("Upload File A", type=["txt"])
96
- file2 = st.file_uploader("Upload File B", type=["txt"])
97
-
98
- parsed_files = []
99
-
100
- if file1:
101
- parsed_files.append(("red", "blue", parse_dcm_assembly(file1)))
102
- if file2:
103
- parsed_files.append(("purple", "orange", parse_dcm_assembly(file2)))
104
-
105
- # === Draw Button + Iframe Viewer ===
106
- if parsed_files:
107
- if st.button("🎯 Visualize"):
108
- os.makedirs("static", exist_ok=True)
109
- with open("static/parsed_data.json", "w") as f:
110
- json.dump({"files": parsed_files}, f, indent=2)
111
-
112
- st.components.v1.html(
113
- '<iframe src="static/index.html" width="100%" height="800" style="border:none;"></iframe>',
114
- height=800,
115
- )
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import FileResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
  import json
5
 
6
+ app = FastAPI()
7
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
8
 
9
  def parse_dcm_assembly(uploaded_file):
10
+ lines = uploaded_file.file.read().decode("utf-8").splitlines()
11
 
12
  geometry = {}
13
  parts = []
 
17
  temp_part = {}
18
  temp_constraint = {}
19
  current_class = None
 
20
 
21
+ plane_label_map = {}
22
+ plane_point_map = {}
23
 
24
  for line in lines:
25
  line = line.strip()
 
26
  if line.startswith("CLASS"):
27
  parts_line = line.split()
28
  current_class = parts_line[1]
29
  current_id = int(parts_line[2])
 
30
  if current_class == "PLANE":
31
  temp_geom = {"tag": current_id}
32
  elif current_class == "WORK_PLANE":
 
70
  if "from" in temp_constraint and "to" in temp_constraint:
71
  constraints[(temp_constraint["from"], temp_constraint["to"])] = temp_constraint["label"]
72
 
 
73
  final_geometry = {}
74
  for tag, data in plane_point_map.items():
75
  final_geometry[tag] = {
 
78
  "label": plane_label_map.get(tag, "")
79
  }
80
 
 
81
  constraint_list = [{"from": f, "to": t, "label": lbl} for (f, t), lbl in constraints.items()]
82
 
83
  return {
 
86
  "constraints": constraint_list
87
  }
88
 
89
+ @app.post("/upload")
90
+ async def upload_file(file: UploadFile = File(...)):
91
+ data = parse_dcm_assembly(file)
92
+ with open("static/parsed_data.json", "w") as f:
93
+ json.dump({"files": [("red", "blue", data)]}, f)
94
+ return JSONResponse(content={"status": "ok"})