Spaces:
Running
Running
Delete backup2-upload-app.py
Browse files- backup2-upload-app.py +0 -129
backup2-upload-app.py
DELETED
@@ -1,129 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
import base64
|
4 |
-
from pathlib import Path
|
5 |
-
import shutil
|
6 |
-
|
7 |
-
def load_aframe_and_extras():
|
8 |
-
return """
|
9 |
-
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
10 |
-
<script src="https://unpkg.com/[email protected]/dist/aframe-event-set-component.min.js"></script>
|
11 |
-
<script>
|
12 |
-
AFRAME.registerComponent('draggable', {
|
13 |
-
init: function () {
|
14 |
-
this.el.setAttribute('class', 'raycastable');
|
15 |
-
this.el.setAttribute('cursor-listener', '');
|
16 |
-
this.dragHandler = this.dragMove.bind(this);
|
17 |
-
this.el.sceneEl.addEventListener('mousemove', this.dragHandler);
|
18 |
-
this.el.addEventListener('mousedown', this.onDragStart.bind(this));
|
19 |
-
this.el.addEventListener('mouseup', this.onDragEnd.bind(this));
|
20 |
-
this.camera = document.querySelector('[camera]');
|
21 |
-
},
|
22 |
-
remove: function () {
|
23 |
-
this.el.removeAttribute('cursor-listener');
|
24 |
-
this.el.sceneEl.removeEventListener('mousemove', this.dragHandler);
|
25 |
-
},
|
26 |
-
onDragStart: function (evt) {
|
27 |
-
this.isDragging = true;
|
28 |
-
this.el.emit('dragstart');
|
29 |
-
},
|
30 |
-
onDragEnd: function (evt) {
|
31 |
-
this.isDragging = false;
|
32 |
-
this.el.emit('dragend');
|
33 |
-
},
|
34 |
-
dragMove: function (evt) {
|
35 |
-
if (!this.isDragging) return;
|
36 |
-
var camera = this.camera;
|
37 |
-
var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5);
|
38 |
-
vector.unproject(camera);
|
39 |
-
var dir = vector.sub(camera.position).normalize();
|
40 |
-
var distance = -camera.position.y / dir.y;
|
41 |
-
var pos = camera.position.clone().add(dir.multiplyScalar(distance));
|
42 |
-
this.el.setAttribute('position', pos);
|
43 |
-
}
|
44 |
-
});
|
45 |
-
</script>
|
46 |
-
"""
|
47 |
-
|
48 |
-
def create_aframe_entity(file_path, file_type, position):
|
49 |
-
if file_type == 'obj':
|
50 |
-
return f'<a-entity position="{position}" obj-model="obj: #{Path(file_path).stem}" class="raycastable" draggable></a-entity>'
|
51 |
-
elif file_type == 'glb':
|
52 |
-
return f'<a-entity position="{position}" gltf-model="#{Path(file_path).stem}" class="raycastable" draggable></a-entity>'
|
53 |
-
elif file_type in ['webp', 'png']:
|
54 |
-
return f'<a-image position="{position}" src="#{Path(file_path).stem}" width="1" height="1" class="raycastable" draggable></a-image>'
|
55 |
-
elif file_type == 'mp4':
|
56 |
-
return f'<a-video position="{position}" src="#{Path(file_path).stem}" width="1" height="1" class="raycastable" draggable></a-video>'
|
57 |
-
return ''
|
58 |
-
|
59 |
-
def encode_file(file_path):
|
60 |
-
with open(file_path, "rb") as file:
|
61 |
-
return base64.b64encode(file.read()).decode()
|
62 |
-
|
63 |
-
def main():
|
64 |
-
st.set_page_config(layout="wide")
|
65 |
-
|
66 |
-
with st.sidebar:
|
67 |
-
st.title("3D File Viewer 🌐")
|
68 |
-
|
69 |
-
st.markdown("### 🎨 Create Assets")
|
70 |
-
st.markdown("[Open 3D Animation Toolkit](https://huggingface.co/spaces/awacke1/3d_animation_toolkit)", unsafe_allow_html=True)
|
71 |
-
|
72 |
-
st.markdown("### 📁 Directory")
|
73 |
-
directory = st.text_input("Enter path:", ".", key="directory_input")
|
74 |
-
|
75 |
-
st.markdown("### ⬆️ Upload")
|
76 |
-
uploaded_files = st.file_uploader("Add files:", accept_multiple_files=True, key="file_uploader")
|
77 |
-
|
78 |
-
st.markdown("### ℹ️ Instructions")
|
79 |
-
st.write("- Click and drag to move objects")
|
80 |
-
st.write("- Use mouse wheel to zoom")
|
81 |
-
st.write("- Right-click and drag to rotate")
|
82 |
-
|
83 |
-
if not os.path.isdir(directory):
|
84 |
-
st.sidebar.error("Invalid directory path")
|
85 |
-
return
|
86 |
-
|
87 |
-
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
88 |
-
|
89 |
-
if uploaded_files:
|
90 |
-
for uploaded_file in uploaded_files:
|
91 |
-
file_extension = Path(uploaded_file.name).suffix.lower()[1:]
|
92 |
-
if file_extension in file_types:
|
93 |
-
with open(os.path.join(directory, uploaded_file.name), "wb") as f:
|
94 |
-
shutil.copyfileobj(uploaded_file, f)
|
95 |
-
st.sidebar.success(f"Uploaded: {uploaded_file.name}")
|
96 |
-
else:
|
97 |
-
st.sidebar.warning(f"Skipped unsupported file: {uploaded_file.name}")
|
98 |
-
|
99 |
-
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
100 |
-
|
101 |
-
aframe_scene = """
|
102 |
-
<a-scene embedded style="height: 600px; width: 100%;">
|
103 |
-
<a-entity camera="userHeight: 1.6" position="0 2 2" rotation="-45 0 0" cursor="rayOrigin: mouse" raycaster="objects: .raycastable"></a-entity>
|
104 |
-
"""
|
105 |
-
|
106 |
-
assets = "<a-assets>"
|
107 |
-
entities = ""
|
108 |
-
|
109 |
-
for i, file in enumerate(files):
|
110 |
-
file_path = os.path.join(directory, file)
|
111 |
-
file_type = file.split('.')[-1]
|
112 |
-
encoded_file = encode_file(file_path)
|
113 |
-
|
114 |
-
if file_type in ['obj', 'glb']:
|
115 |
-
assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
|
116 |
-
elif file_type in ['webp', 'png', 'mp4']:
|
117 |
-
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
|
118 |
-
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
|
119 |
-
|
120 |
-
position = f"{i} 0 {i}"
|
121 |
-
entities += create_aframe_entity(file_path, file_type, position)
|
122 |
-
|
123 |
-
assets += "</a-assets>"
|
124 |
-
aframe_scene += assets + entities + "</a-scene>"
|
125 |
-
|
126 |
-
st.components.v1.html(load_aframe_and_extras() + aframe_scene, height=600)
|
127 |
-
|
128 |
-
if __name__ == "__main__":
|
129 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|