Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import base64
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
def load_aframe():
|
| 7 |
+
return """
|
| 8 |
+
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def create_aframe_entity(file_path, file_type, position):
|
| 12 |
+
if file_type == 'obj':
|
| 13 |
+
return f'<a-entity position="{position}" obj-model="obj: #{Path(file_path).stem}"></a-entity>'
|
| 14 |
+
elif file_type == 'glb':
|
| 15 |
+
return f'<a-entity position="{position}" gltf-model="#{Path(file_path).stem}"></a-entity>'
|
| 16 |
+
elif file_type in ['webp', 'png']:
|
| 17 |
+
return f'<a-image position="{position}" src="#{Path(file_path).stem}" width="1" height="1"></a-image>'
|
| 18 |
+
elif file_type == 'mp4':
|
| 19 |
+
return f'<a-video position="{position}" src="#{Path(file_path).stem}" width="1" height="1"></a-video>'
|
| 20 |
+
return ''
|
| 21 |
+
|
| 22 |
+
def encode_file(file_path):
|
| 23 |
+
with open(file_path, "rb") as file:
|
| 24 |
+
return base64.b64encode(file.read()).decode()
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
st.title("Local File 3D Viewer")
|
| 28 |
+
|
| 29 |
+
directory = st.text_input("Enter the directory path:", ".")
|
| 30 |
+
if not os.path.isdir(directory):
|
| 31 |
+
st.error("Invalid directory path")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
|
| 35 |
+
files = [f for f in os.listdir(directory) if f.split('.')[-1] in file_types]
|
| 36 |
+
|
| 37 |
+
aframe_scene = """
|
| 38 |
+
<a-scene embedded style="height: 500px; width: 100%;">
|
| 39 |
+
<a-entity camera="userHeight: 1.6" position="0 2 2" rotation="-45 0 0"></a-entity>
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
assets = "<a-assets>"
|
| 43 |
+
entities = ""
|
| 44 |
+
|
| 45 |
+
for i, file in enumerate(files):
|
| 46 |
+
file_path = os.path.join(directory, file)
|
| 47 |
+
file_type = file.split('.')[-1]
|
| 48 |
+
encoded_file = encode_file(file_path)
|
| 49 |
+
|
| 50 |
+
if file_type in ['obj', 'glb']:
|
| 51 |
+
assets += f'<a-asset-item id="{Path(file).stem}" src="data:application/octet-stream;base64,{encoded_file}"></a-asset-item>'
|
| 52 |
+
elif file_type in ['webp', 'png', 'mp4']:
|
| 53 |
+
mime_type = f"image/{file_type}" if file_type in ['webp', 'png'] else "video/mp4"
|
| 54 |
+
assets += f'<{file_type} id="{Path(file).stem}" src="data:{mime_type};base64,{encoded_file}"></{file_type}>'
|
| 55 |
+
|
| 56 |
+
position = f"{i} 0 {i}"
|
| 57 |
+
entities += create_aframe_entity(file_path, file_type, position)
|
| 58 |
+
|
| 59 |
+
assets += "</a-assets>"
|
| 60 |
+
aframe_scene += assets + entities + "</a-scene>"
|
| 61 |
+
|
| 62 |
+
st.components.v1.html(load_aframe() + aframe_scene, height=500)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|