Spaces:
Sleeping
Sleeping
import streamlit as st | |
import ast | |
import base64 | |
import streamlit.components.v1 as components | |
from transformers import pipeline | |
# --- Load summarizer model --- | |
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 | |