Codeez / app.py
masadonline's picture
Update app.py
8f4ea34 verified
raw
history blame
3.57 kB
import streamlit as st
import ast
import base64
import streamlit.components.v1 as components
from transformers import pipeline
# --- Load summarizer model ---
@st.cache_resource
def load_model():
summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
return summarizer
summarizer = load_model()
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
st.title("πŸ‘“ AR/VR Enhanced Code Visualizer")
# --- Upload Python file ---
uploaded_file = st.file_uploader("πŸ“ Upload your Python file", type=["py"])
# --- Parse code and show output ---
if uploaded_file is not None:
code = uploaded_file.read().decode('utf-8')
st.subheader("πŸ“„ Uploaded Code")
st.code(code, language='python')
# Parse AST
tree = ast.parse(code)
functions = [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
st.subheader("πŸ” Code Structure")
st.write(f"**Functions:** {functions or 'None'}")
st.write(f"**Classes:** {classes or 'None'}")
# Hugging Face Summarization
st.subheader("🧠 Code Explanation (AI)")
prompt = f"This code contains functions: {functions} and classes: {classes}. Explain what this code does."
try:
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
st.success(summary[0]['summary_text'])
except Exception as e:
st.error(f"Summarization failed: {e}")
# Build dynamic A-Frame HTML
def generate_aframe_html(functions, classes):
elements = []
x_pos = -3
for fn in functions:
elements.append(f"""
<a-box position="{x_pos} 1 -3" color="#FFC65D" depth="0.5" height="0.5" width="2"></a-box>
<a-text value="Function: {fn}" position="{x_pos} 1.7 -3" align="center" color="black"></a-text>
""")
x_pos += 3
for cls in classes:
elements.append(f"""
<a-box position="{x_pos} 1 -3" color="#F16775" depth="0.5" height="0.5" width="2"></a-box>
<a-text value="Class: {cls}" position="{x_pos} 1.7 -3" align="center" color="black"></a-text>
""")
x_pos += 3
html = f"""
<!DOCTYPE html>
<html>
<head>
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<a-scene background="color: #ECECEC">
<a-entity position="0 1.6 3">
<a-camera wasd-controls-enabled="true" look-controls-enabled="true"></a-camera>
</a-entity>
<a-light type="ambient" color="#FFF"></a-light>
<a-plane rotation="-90 0 0" width="30" height="30" color="#7BC8A4"></a-plane>
{''.join(elements)}
</a-scene>
</body>
</html>
"""
return html
# Generate and encode A-Frame scene as base64
aframe_html = generate_aframe_html(functions, classes)
encoded_html = base64.b64encode(aframe_html.encode()).decode()
data_url = f"data:text/html;base64,{encoded_html}"
st.subheader("🌐 AR/VR Visualization")
st.info("Below is an interactive 3D visualization of your code structure (functions & classes).")
components.iframe(data_url, height=500, scrolling=True)
else:
st.info("Please upload a Python file to get started.")