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""" """) x_pos += 3 for cls in classes: elements.append(f""" """) x_pos += 3 html = f""" {''.join(elements)} """ 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.")