Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,11 @@ import base64
|
|
4 |
import streamlit.components.v1 as components
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
7 |
-
from io import BytesIO
|
8 |
import qrcode
|
|
|
9 |
|
10 |
-
# Page config
|
11 |
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
|
12 |
-
st.title("π AR/VR Code Visualizer with
|
13 |
|
14 |
@st.cache_resource
|
15 |
def load_model():
|
@@ -17,14 +16,13 @@ def load_model():
|
|
17 |
|
18 |
summarizer = load_model()
|
19 |
|
20 |
-
# Code input area
|
21 |
st.subheader("π Write or Paste Your Python Code")
|
22 |
code = st.text_area("Enter your Python code here", height=300)
|
23 |
|
24 |
if code.strip():
|
25 |
st.code(code, language="python")
|
26 |
|
27 |
-
#
|
28 |
tree = ast.parse(code)
|
29 |
|
30 |
class FunctionCallVisitor(ast.NodeVisitor):
|
@@ -43,13 +41,12 @@ if code.strip():
|
|
43 |
visitor.visit(tree)
|
44 |
call_graph = visitor.calls
|
45 |
|
46 |
-
# Display call relationships
|
47 |
st.subheader("π Function Calls")
|
48 |
for fn, callees in call_graph.items():
|
49 |
st.write(f"πΉ `{fn}` calls: {', '.join(callees) if callees else 'None'}")
|
50 |
|
51 |
-
# AI
|
52 |
-
prompt = f"Explain
|
53 |
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
|
54 |
summary_text = summary[0]['summary_text']
|
55 |
st.success(summary_text)
|
@@ -60,7 +57,7 @@ if code.strip():
|
|
60 |
tts.save("summary.mp3")
|
61 |
st.audio("summary.mp3", format="audio/mp3")
|
62 |
|
63 |
-
# A-Frame
|
64 |
def generate_aframe(call_graph):
|
65 |
function_data = {
|
66 |
"functions": [],
|
@@ -85,39 +82,39 @@ if code.strip():
|
|
85 |
<script>
|
86 |
const functionData = {str(function_data).replace("'", '"')};
|
87 |
|
88 |
-
AFRAME.registerComponent('dynamic-creator', {{
|
89 |
-
init: function () {{
|
90 |
-
functionData.functions.forEach(function(fn) {{
|
91 |
-
const box = document.createElement('a-box');
|
92 |
-
box.setAttribute('position', fn.position.join(' '));
|
93 |
-
box.setAttribute('depth', '0.5');
|
94 |
-
box.setAttribute('height', '0.5');
|
95 |
-
box.setAttribute('width', '2');
|
96 |
-
box.setAttribute('color', '#FFC65D');
|
97 |
-
box.setAttribute('class', 'clickable');
|
98 |
-
box.setAttribute('onclick', `say('${fn.name}')`);
|
99 |
-
this.el.appendChild(box);
|
100 |
-
|
101 |
-
const text = document.createElement('a-text');
|
102 |
-
text.setAttribute('value', fn.name);
|
103 |
-
text.setAttribute('position', `${fn.position[0]} ${fn.position[1] + 1} ${fn.position[2]}`);
|
104 |
-
text.setAttribute('align', 'center');
|
105 |
-
text.setAttribute('color', '#000');
|
106 |
-
this.el.appendChild(text);
|
107 |
-
}}.bind(this));
|
108 |
-
|
109 |
-
functionData.relationships.forEach(function(rel) {{
|
110 |
-
const line = document.createElement('a-entity');
|
111 |
-
line.setAttribute('line', `start: ${rel.start.join(' ')}; end: ${rel.end.join(' ')}; color: red`);
|
112 |
-
this.el.appendChild(line);
|
113 |
-
}}.bind(this));
|
114 |
-
}}
|
115 |
-
}});
|
116 |
-
|
117 |
function say(text) {{
|
118 |
const utter = new SpeechSynthesisUtterance(text);
|
119 |
speechSynthesis.speak(utter);
|
120 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
</script>
|
122 |
"""
|
123 |
|
@@ -125,18 +122,20 @@ if code.strip():
|
|
125 |
<!DOCTYPE html>
|
126 |
<html>
|
127 |
<head>
|
|
|
128 |
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
|
|
|
|
|
|
|
129 |
</head>
|
130 |
<body>
|
131 |
-
<a-scene>
|
132 |
-
<a-entity
|
133 |
-
|
134 |
-
<a-camera></a-camera>
|
135 |
</a-entity>
|
136 |
<a-light type="ambient" color="#FFF"></a-light>
|
137 |
<a-plane rotation="-90 0 0" width="40" height="40" color="#7BC8A4"></a-plane>
|
138 |
</a-scene>
|
139 |
-
{js}
|
140 |
</body>
|
141 |
</html>
|
142 |
"""
|
@@ -149,12 +148,18 @@ if code.strip():
|
|
149 |
st.subheader("π Interactive 3D Function Visualizer")
|
150 |
components.iframe(data_url, height=600)
|
151 |
|
152 |
-
# QR
|
153 |
st.subheader("π± AR View on Mobile")
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
qr.
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
else:
|
160 |
st.info("Write some Python code above to visualize, narrate, and explore it in 3D/AR.")
|
|
|
4 |
import streamlit.components.v1 as components
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
|
|
7 |
import qrcode
|
8 |
+
from io import BytesIO
|
9 |
|
|
|
10 |
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
|
11 |
+
st.title("π AR/VR Code Visualizer with Interaction, Narration, and AR View")
|
12 |
|
13 |
@st.cache_resource
|
14 |
def load_model():
|
|
|
16 |
|
17 |
summarizer = load_model()
|
18 |
|
|
|
19 |
st.subheader("π Write or Paste Your Python Code")
|
20 |
code = st.text_area("Enter your Python code here", height=300)
|
21 |
|
22 |
if code.strip():
|
23 |
st.code(code, language="python")
|
24 |
|
25 |
+
# AST parsing to get function call relationships
|
26 |
tree = ast.parse(code)
|
27 |
|
28 |
class FunctionCallVisitor(ast.NodeVisitor):
|
|
|
41 |
visitor.visit(tree)
|
42 |
call_graph = visitor.calls
|
43 |
|
|
|
44 |
st.subheader("π Function Calls")
|
45 |
for fn, callees in call_graph.items():
|
46 |
st.write(f"πΉ `{fn}` calls: {', '.join(callees) if callees else 'None'}")
|
47 |
|
48 |
+
# AI summary
|
49 |
+
prompt = f"Explain this call graph structure: {call_graph}"
|
50 |
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
|
51 |
summary_text = summary[0]['summary_text']
|
52 |
st.success(summary_text)
|
|
|
57 |
tts.save("summary.mp3")
|
58 |
st.audio("summary.mp3", format="audio/mp3")
|
59 |
|
60 |
+
# A-Frame 3D HTML generator
|
61 |
def generate_aframe(call_graph):
|
62 |
function_data = {
|
63 |
"functions": [],
|
|
|
82 |
<script>
|
83 |
const functionData = {str(function_data).replace("'", '"')};
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
function say(text) {{
|
86 |
const utter = new SpeechSynthesisUtterance(text);
|
87 |
speechSynthesis.speak(utter);
|
88 |
}}
|
89 |
+
|
90 |
+
document.addEventListener('DOMContentLoaded', () => {{
|
91 |
+
functionData.functions.forEach(function(fn) {{
|
92 |
+
const box = document.createElement('a-box');
|
93 |
+
box.setAttribute('position', fn.position.join(' '));
|
94 |
+
box.setAttribute('depth', '0.5');
|
95 |
+
box.setAttribute('height', '0.5');
|
96 |
+
box.setAttribute('width', '2');
|
97 |
+
box.setAttribute('color', '#FFC65D');
|
98 |
+
box.setAttribute('class', 'clickable');
|
99 |
+
box.setAttribute('event-set__enter', '_event: mouseenter; color: #00CED1');
|
100 |
+
box.setAttribute('event-set__leave', '_event: mouseleave; color: #FFC65D');
|
101 |
+
box.setAttribute('onclick', 'say(\"' + fn.name + '\")');
|
102 |
+
document.querySelector('a-scene').appendChild(box);
|
103 |
+
|
104 |
+
const text = document.createElement('a-text');
|
105 |
+
text.setAttribute('value', fn.name);
|
106 |
+
text.setAttribute('position', `${{fn.position[0]}} ${{fn.position[1] + 1}} ${{fn.position[2]}}`);
|
107 |
+
text.setAttribute('align', 'center');
|
108 |
+
text.setAttribute('color', '#000');
|
109 |
+
document.querySelector('a-scene').appendChild(text);
|
110 |
+
}});
|
111 |
+
|
112 |
+
functionData.relationships.forEach(function(rel) {{
|
113 |
+
const el = document.createElement('a-entity');
|
114 |
+
el.setAttribute('line', `start: ${{rel.start.join(' ')}}; end: ${{rel.end.join(' ')}}; color: red`);
|
115 |
+
document.querySelector('a-scene').appendChild(el);
|
116 |
+
}});
|
117 |
+
}});
|
118 |
</script>
|
119 |
"""
|
120 |
|
|
|
122 |
<!DOCTYPE html>
|
123 |
<html>
|
124 |
<head>
|
125 |
+
<meta charset="utf-8">
|
126 |
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
|
127 |
+
<script src="https://cdn.jsdelivr.net/gh/donmccurdy/[email protected]/dist/aframe-extras.min.js"></script>
|
128 |
+
<script src="https://unpkg.com/[email protected]/dist/aframe-screenshot-component.min.js"></script>
|
129 |
+
{js}
|
130 |
</head>
|
131 |
<body>
|
132 |
+
<a-scene screenshot>
|
133 |
+
<a-entity position="0 1.6 3">
|
134 |
+
<a-camera wasd-controls-enabled="true" look-controls-enabled="true"></a-camera>
|
|
|
135 |
</a-entity>
|
136 |
<a-light type="ambient" color="#FFF"></a-light>
|
137 |
<a-plane rotation="-90 0 0" width="40" height="40" color="#7BC8A4"></a-plane>
|
138 |
</a-scene>
|
|
|
139 |
</body>
|
140 |
</html>
|
141 |
"""
|
|
|
148 |
st.subheader("π Interactive 3D Function Visualizer")
|
149 |
components.iframe(data_url, height=600)
|
150 |
|
151 |
+
# QR Code
|
152 |
st.subheader("π± AR View on Mobile")
|
153 |
+
qr = qrcode.QRCode(version=1, box_size=10, border=5)
|
154 |
+
space_url = "https://huggingface.co/spaces/your-username/your-space-name" # Replace this
|
155 |
+
qr.add_data(space_url)
|
156 |
+
qr.make(fit=True)
|
157 |
+
|
158 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
159 |
+
img_byte_arr = BytesIO()
|
160 |
+
img.save(img_byte_arr, format='PNG')
|
161 |
+
img_bytes = img_byte_arr.getvalue()
|
162 |
+
st.image(img_bytes, caption="Scan to view in AR on mobile")
|
163 |
+
|
164 |
else:
|
165 |
st.info("Write some Python code above to visualize, narrate, and explore it in 3D/AR.")
|