File size: 3,565 Bytes
8c52bbf
 
cfe4dff
8c52bbf
 
 
cfe4dff
8c52bbf
 
 
 
 
 
 
4a33878
 
 
cfe4dff
4a33878
 
cfe4dff
8c52bbf
 
 
 
 
cfe4dff
8c52bbf
 
 
 
cfe4dff
 
 
8c52bbf
cfe4dff
 
 
4a33878
 
 
 
 
 
cfe4dff
 
 
 
4a33878
cfe4dff
 
8f4ea34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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.")