Codeez / app.py
masadonline's picture
Update app.py
52fd65e verified
raw
history blame
5.88 kB
import streamlit as st
import ast
import base64
import streamlit.components.v1 as components
from transformers import pipeline
from gtts import gTTS
from io import BytesIO
import qrcode
# Page config
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
st.title("πŸ‘“ AR/VR Code Visualizer with Editing, Interaction, and Export")
@st.cache_resource
def load_model():
return pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
summarizer = load_model()
# Code input area
st.subheader("πŸ“ Write or Paste Your Python Code")
code = st.text_area("Enter your Python code here", height=300)
if code.strip():
st.code(code, language="python")
# Parse code to extract functions and their calls
tree = ast.parse(code)
class FunctionCallVisitor(ast.NodeVisitor):
def __init__(self):
self.calls = {}
def visit_FunctionDef(self, node):
caller = node.name
self.calls[caller] = []
for child in ast.walk(node):
if isinstance(child, ast.Call) and isinstance(child.func, ast.Name):
self.calls[caller].append(child.func.id)
self.generic_visit(node)
visitor = FunctionCallVisitor()
visitor.visit(tree)
call_graph = visitor.calls
# Display call relationships
st.subheader("πŸ“Š Function Calls")
for fn, callees in call_graph.items():
st.write(f"πŸ”Ή `{fn}` calls: {', '.join(callees) if callees else 'None'}")
# AI Summary
prompt = f"Explain the structure and purpose of the following functions and how they call each other: {call_graph}"
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
summary_text = summary[0]['summary_text']
st.success(summary_text)
# Voice narration
st.subheader("πŸ”Š Voice Narration")
tts = gTTS(text=summary_text)
tts.save("summary.mp3")
st.audio("summary.mp3", format="audio/mp3")
# A-Frame scene generation
def generate_aframe(call_graph):
function_data = {
"functions": [],
"relationships": []
}
function_positions = {}
spacing = 3
for i, fn in enumerate(call_graph):
x = i * spacing
function_positions[fn] = (x, 1, -3)
function_data["functions"].append({"name": fn, "position": [x, 1, -3]})
for caller, callees in call_graph.items():
for callee in callees:
if callee in function_positions:
x1, y1, z1 = function_positions[caller]
x2, y2, z2 = function_positions[callee]
function_data["relationships"].append({"start": [x1, y1, z1], "end": [x2, y2, z2]})
js = f"""
<script>
const functionData = {str(function_data).replace("'", '"')};
AFRAME.registerComponent('dynamic-creator', {{
init: function () {{
functionData.functions.forEach(function(fn) {{
const box = document.createElement('a-box');
box.setAttribute('position', fn.position.join(' '));
box.setAttribute('depth', '0.5');
box.setAttribute('height', '0.5');
box.setAttribute('width', '2');
box.setAttribute('color', '#FFC65D');
box.setAttribute('class', 'clickable');
box.setAttribute('onclick', `say('${fn.name}')`);
this.el.appendChild(box);
const text = document.createElement('a-text');
text.setAttribute('value', fn.name);
text.setAttribute('position', `${fn.position[0]} ${fn.position[1] + 1} ${fn.position[2]}`);
text.setAttribute('align', 'center');
text.setAttribute('color', '#000');
this.el.appendChild(text);
}}.bind(this));
functionData.relationships.forEach(function(rel) {{
const line = document.createElement('a-entity');
line.setAttribute('line', `start: ${rel.start.join(' ')}; end: ${rel.end.join(' ')}; color: red`);
this.el.appendChild(line);
}}.bind(this));
}}
}});
function say(text) {{
const utter = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utter);
}}
</script>
"""
html = f"""
<!DOCTYPE html>
<html>
<head>
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-entity dynamic-creator position="0 0 0"></a-entity>
<a-entity position="0 1.6 4">
<a-camera></a-camera>
</a-entity>
<a-light type="ambient" color="#FFF"></a-light>
<a-plane rotation="-90 0 0" width="40" height="40" color="#7BC8A4"></a-plane>
</a-scene>
{js}
</body>
</html>
"""
return html
aframe_html = generate_aframe(call_graph)
b64 = base64.b64encode(aframe_html.encode()).decode()
data_url = f"data:text/html;base64,{b64}"
st.subheader("🌐 Interactive 3D Function Visualizer")
components.iframe(data_url, height=600)
# QR code for Hugging Face Space
st.subheader("πŸ“± AR View on Mobile")
space_url = "https://huggingface.co/spaces/your-space-name" # Replace with actual URL
qr = qrcode.make(space_url)
buf = BytesIO()
qr.save(buf, format="PNG")
st.image(buf.getvalue(), caption="Scan this QR code to view the VR scene in AR on your mobile!")
else:
st.info("Write some Python code above to visualize, narrate, and explore it in 3D/AR.")