Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +24 -15
- static/index.html +105 -0
app.py
CHANGED
|
@@ -1,25 +1,34 @@
|
|
| 1 |
|
| 2 |
from fastapi import FastAPI, UploadFile, File
|
| 3 |
-
from fastapi.responses import
|
| 4 |
-
from fastapi.
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
allow_origins=["*"], allow_credentials=True,
|
| 12 |
-
allow_methods=["*"], allow_headers=["*"],
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
@app.
|
| 16 |
-
def
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def parse_dcm_assembly(file_bytes):
|
| 25 |
lines = file_bytes.decode("utf-8").splitlines()
|
|
|
|
| 1 |
|
| 2 |
from fastapi import FastAPI, UploadFile, File
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
import os, json
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Mount frontend files
|
| 10 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
@app.post("/upload")
|
| 13 |
+
async def upload(file1: UploadFile = File(...), file2: UploadFile = File(None)):
|
| 14 |
+
content1 = await file1.read()
|
| 15 |
+
data1 = parse_dcm_assembly(content1)
|
| 16 |
|
| 17 |
+
result = [("red", "blue", data1)]
|
| 18 |
+
|
| 19 |
+
if file2:
|
| 20 |
+
content2 = await file2.read()
|
| 21 |
+
data2 = parse_dcm_assembly(content2)
|
| 22 |
+
result.append(("purple", "orange", data2))
|
| 23 |
+
|
| 24 |
+
with open("static/parsed_data.json", "w") as f:
|
| 25 |
+
json.dump({"files": result}, f)
|
| 26 |
+
|
| 27 |
+
return {"status": "ok"}
|
| 28 |
+
|
| 29 |
+
@app.get("/parsed_data.json")
|
| 30 |
+
def get_parsed():
|
| 31 |
+
return FileResponse("static/parsed_data.json")
|
| 32 |
|
| 33 |
def parse_dcm_assembly(file_bytes):
|
| 34 |
lines = file_bytes.decode("utf-8").splitlines()
|
static/index.html
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
<!DOCTYPE html>
|
| 3 |
+
<html lang="en">
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8" />
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 7 |
+
<title>DCM Assembly Visualizer</title>
|
| 8 |
+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|
| 9 |
+
<style>
|
| 10 |
+
body { font-family: sans-serif; padding: 20px; }
|
| 11 |
+
#plot { width: 100%; height: 700px; }
|
| 12 |
+
</style>
|
| 13 |
+
</head>
|
| 14 |
+
<body>
|
| 15 |
+
<h1>DCM Assembly Visualizer</h1>
|
| 16 |
+
<input type="file" id="file1" accept=".txt"> File A<br>
|
| 17 |
+
<input type="file" id="file2" accept=".txt"> File B<br><br>
|
| 18 |
+
<button onclick="uploadFiles()">Visualize</button>
|
| 19 |
+
<div id="plot"></div>
|
| 20 |
+
|
| 21 |
+
<script>
|
| 22 |
+
async function uploadFiles() {
|
| 23 |
+
const file1 = document.getElementById("file1").files[0];
|
| 24 |
+
const file2 = document.getElementById("file2").files[0];
|
| 25 |
+
|
| 26 |
+
if (!file1 && !file2) {
|
| 27 |
+
alert("Please select at least one file.");
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
const traces = [];
|
| 32 |
+
|
| 33 |
+
if (file1) {
|
| 34 |
+
const data1 = await sendFile(file1);
|
| 35 |
+
traces.push(...extractTraces(data1, 'File A', 'red', 'blue'));
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
if (file2) {
|
| 39 |
+
const data2 = await sendFile(file2);
|
| 40 |
+
traces.push(...extractTraces(data2, 'File B', 'purple', 'orange'));
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
Plotly.newPlot('plot', traces, {
|
| 44 |
+
margin: { t: 30 },
|
| 45 |
+
scene: { aspectmode: 'data' },
|
| 46 |
+
title: '3D Assembly Viewer'
|
| 47 |
+
});
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
async function sendFile(file) {
|
| 51 |
+
const formData = new FormData();
|
| 52 |
+
formData.append("file", file);
|
| 53 |
+
const res = await fetch("/parse", {
|
| 54 |
+
method: "POST",
|
| 55 |
+
body: formData
|
| 56 |
+
});
|
| 57 |
+
return await res.json();
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
function extractTraces(data, prefix, baseColor, normalColor) {
|
| 61 |
+
const traces = [];
|
| 62 |
+
|
| 63 |
+
for (const [tag, g] of Object.entries(data.geometry)) {
|
| 64 |
+
const [x, y, z] = g.base_point;
|
| 65 |
+
const [nx, ny, nz] = g.normal;
|
| 66 |
+
|
| 67 |
+
traces.push({
|
| 68 |
+
type: 'scatter3d',
|
| 69 |
+
mode: 'markers+text',
|
| 70 |
+
x: [x], y: [y], z: [z],
|
| 71 |
+
marker: { color: baseColor, size: 5 },
|
| 72 |
+
text: [`${prefix}: ${g.label || tag}`],
|
| 73 |
+
textposition: 'top center'
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
traces.push({
|
| 77 |
+
type: 'scatter3d',
|
| 78 |
+
mode: 'lines',
|
| 79 |
+
x: [x, x + nx],
|
| 80 |
+
y: [y, y + ny],
|
| 81 |
+
z: [z, z + nz],
|
| 82 |
+
line: { color: normalColor, width: 3 }
|
| 83 |
+
});
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
for (const c of data.constraints) {
|
| 87 |
+
const g1 = data.geometry[c.from];
|
| 88 |
+
const g2 = data.geometry[c.to];
|
| 89 |
+
if (g1 && g2) {
|
| 90 |
+
const [x1, y1, z1] = g1.base_point;
|
| 91 |
+
const [x2, y2, z2] = g2.base_point;
|
| 92 |
+
traces.push({
|
| 93 |
+
type: 'scatter3d',
|
| 94 |
+
mode: 'lines',
|
| 95 |
+
x: [x1, x2], y: [y1, y2], z: [z1, z2],
|
| 96 |
+
line: { color: 'green', dash: 'dot', width: 2 }
|
| 97 |
+
});
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
return traces;
|
| 102 |
+
}
|
| 103 |
+
</script>
|
| 104 |
+
</body>
|
| 105 |
+
</html>
|