Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,10 @@ import ast
|
|
3 |
import base64
|
4 |
import streamlit.components.v1 as components
|
5 |
from transformers import pipeline
|
|
|
6 |
|
7 |
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
|
8 |
-
st.title("π AR/VR Code Visualizer with Call Relationships")
|
9 |
|
10 |
@st.cache_resource
|
11 |
def load_model():
|
@@ -19,13 +20,12 @@ if uploaded_file:
|
|
19 |
code = uploaded_file.read().decode("utf-8")
|
20 |
st.code(code, language="python")
|
21 |
|
22 |
-
# Parse AST
|
23 |
tree = ast.parse(code)
|
24 |
|
25 |
-
# Find functions and their calls
|
26 |
class FunctionCallVisitor(ast.NodeVisitor):
|
27 |
def __init__(self):
|
28 |
-
self.calls = {}
|
29 |
|
30 |
def visit_FunctionDef(self, node):
|
31 |
caller = node.name
|
@@ -40,24 +40,31 @@ if uploaded_file:
|
|
40 |
call_graph = visitor.calls
|
41 |
all_functions = list(call_graph.keys())
|
42 |
|
43 |
-
# Show
|
44 |
st.subheader("π Call Relationships")
|
45 |
for fn, callees in call_graph.items():
|
46 |
st.write(f"πΉ `{fn}` calls: {', '.join(callees) if callees else 'None'}")
|
47 |
|
48 |
-
#
|
49 |
prompt = f"Explain the structure and purpose of the following functions and how they call each other: {call_graph}"
|
50 |
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
|
51 |
-
|
|
|
52 |
|
53 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def generate_aframe(call_graph):
|
55 |
spacing = 3
|
56 |
boxes = []
|
57 |
lines = []
|
58 |
positions = {}
|
59 |
|
60 |
-
# Assign fixed positions on x-axis
|
61 |
for i, fn in enumerate(call_graph):
|
62 |
x = i * spacing
|
63 |
positions[fn] = (x, 1, -3)
|
@@ -66,7 +73,6 @@ if uploaded_file:
|
|
66 |
<a-text value="{fn}" position="{x} 1.8 -3" align="center" color="#000"></a-text>
|
67 |
''')
|
68 |
|
69 |
-
# Draw arrows between caller β callee
|
70 |
for caller, callees in call_graph.items():
|
71 |
for callee in callees:
|
72 |
if callee in positions:
|
@@ -105,4 +111,4 @@ if uploaded_file:
|
|
105 |
components.iframe(data_url, height=500)
|
106 |
|
107 |
else:
|
108 |
-
st.info("Upload a Python file to visualize
|
|
|
3 |
import base64
|
4 |
import streamlit.components.v1 as components
|
5 |
from transformers import pipeline
|
6 |
+
from gtts import gTTS
|
7 |
|
8 |
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
|
9 |
+
st.title("π AR/VR Code Visualizer with Call Relationships + Narration")
|
10 |
|
11 |
@st.cache_resource
|
12 |
def load_model():
|
|
|
20 |
code = uploaded_file.read().decode("utf-8")
|
21 |
st.code(code, language="python")
|
22 |
|
23 |
+
# Parse AST for functions and call relationships
|
24 |
tree = ast.parse(code)
|
25 |
|
|
|
26 |
class FunctionCallVisitor(ast.NodeVisitor):
|
27 |
def __init__(self):
|
28 |
+
self.calls = {}
|
29 |
|
30 |
def visit_FunctionDef(self, node):
|
31 |
caller = node.name
|
|
|
40 |
call_graph = visitor.calls
|
41 |
all_functions = list(call_graph.keys())
|
42 |
|
43 |
+
# Show call relationships
|
44 |
st.subheader("π Call Relationships")
|
45 |
for fn, callees in call_graph.items():
|
46 |
st.write(f"πΉ `{fn}` calls: {', '.join(callees) if callees else 'None'}")
|
47 |
|
48 |
+
# Hugging Face summarization
|
49 |
prompt = f"Explain the structure and purpose of the following functions and how they call each other: {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)
|
53 |
|
54 |
+
# Voice narration
|
55 |
+
st.subheader("π Voice Narration of Code Summary")
|
56 |
+
tts = gTTS(text=summary_text)
|
57 |
+
tts.save("summary.mp3")
|
58 |
+
audio_file = open("summary.mp3", "rb")
|
59 |
+
st.audio(audio_file.read(), format="audio/mp3")
|
60 |
+
|
61 |
+
# Generate A-Frame scene
|
62 |
def generate_aframe(call_graph):
|
63 |
spacing = 3
|
64 |
boxes = []
|
65 |
lines = []
|
66 |
positions = {}
|
67 |
|
|
|
68 |
for i, fn in enumerate(call_graph):
|
69 |
x = i * spacing
|
70 |
positions[fn] = (x, 1, -3)
|
|
|
73 |
<a-text value="{fn}" position="{x} 1.8 -3" align="center" color="#000"></a-text>
|
74 |
''')
|
75 |
|
|
|
76 |
for caller, callees in call_graph.items():
|
77 |
for callee in callees:
|
78 |
if callee in positions:
|
|
|
111 |
components.iframe(data_url, height=500)
|
112 |
|
113 |
else:
|
114 |
+
st.info("Upload a Python file to visualize its structure, relationships, and get narrated summary.")
|