Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import ast
|
|
|
|
| 3 |
import streamlit.components.v1 as components
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
|
|
@@ -11,42 +12,41 @@ def load_model():
|
|
| 11 |
|
| 12 |
summarizer = load_model()
|
| 13 |
|
| 14 |
-
# --- Streamlit page config ---
|
| 15 |
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
|
| 16 |
st.title("π AR/VR Enhanced Code Visualizer")
|
| 17 |
|
| 18 |
-
# ---
|
| 19 |
uploaded_file = st.file_uploader("π Upload your Python file", type=["py"])
|
| 20 |
|
|
|
|
| 21 |
if uploaded_file is not None:
|
| 22 |
code = uploaded_file.read().decode('utf-8')
|
| 23 |
st.subheader("π Uploaded Code")
|
| 24 |
st.code(code, language='python')
|
| 25 |
|
| 26 |
-
#
|
| 27 |
tree = ast.parse(code)
|
| 28 |
functions = [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]
|
| 29 |
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
|
| 30 |
|
| 31 |
-
st.subheader("π
|
| 32 |
-
st.write(f"**Functions:** {functions
|
| 33 |
-
st.write(f"**Classes:** {classes
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
st.subheader("π§ Code Explanation")
|
| 37 |
-
prompt = f"This
|
| 38 |
try:
|
| 39 |
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
|
| 40 |
st.success(summary[0]['summary_text'])
|
| 41 |
except Exception as e:
|
| 42 |
st.error(f"Summarization failed: {e}")
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
else:
|
| 52 |
-
st.info("Upload a Python file to begin visualizing and explaining it.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import ast
|
| 3 |
+
import base64
|
| 4 |
import streamlit.components.v1 as components
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# --- Load summarizer model ---
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
|
|
|
|
| 12 |
|
| 13 |
summarizer = load_model()
|
| 14 |
|
|
|
|
| 15 |
st.set_page_config(page_title="AR/VR Code Visualizer", layout="wide")
|
| 16 |
st.title("π AR/VR Enhanced Code Visualizer")
|
| 17 |
|
| 18 |
+
# --- Upload Python file ---
|
| 19 |
uploaded_file = st.file_uploader("π Upload your Python file", type=["py"])
|
| 20 |
|
| 21 |
+
# --- Parse code and show output ---
|
| 22 |
if uploaded_file is not None:
|
| 23 |
code = uploaded_file.read().decode('utf-8')
|
| 24 |
st.subheader("π Uploaded Code")
|
| 25 |
st.code(code, language='python')
|
| 26 |
|
| 27 |
+
# Parse AST
|
| 28 |
tree = ast.parse(code)
|
| 29 |
functions = [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]
|
| 30 |
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
|
| 31 |
|
| 32 |
+
st.subheader("π Code Structure")
|
| 33 |
+
st.write(f"**Functions:** {functions or 'None'}")
|
| 34 |
+
st.write(f"**Classes:** {classes or 'None'}")
|
| 35 |
|
| 36 |
+
# Hugging Face Summarization
|
| 37 |
+
st.subheader("π§ Code Explanation (AI)")
|
| 38 |
+
prompt = f"This code contains functions: {functions} and classes: {classes}. Explain what this code does."
|
| 39 |
try:
|
| 40 |
summary = summarizer(prompt, max_length=60, min_length=15, do_sample=False)
|
| 41 |
st.success(summary[0]['summary_text'])
|
| 42 |
except Exception as e:
|
| 43 |
st.error(f"Summarization failed: {e}")
|
| 44 |
|
| 45 |
+
# Build dynamic A-Frame HTML
|
| 46 |
+
def generate_aframe_html(functions, classes):
|
| 47 |
+
elements = []
|
| 48 |
+
x_pos = -3
|
| 49 |
|
| 50 |
+
for fn in functions:
|
| 51 |
+
elements.append(f"""
|
| 52 |
+
<a-box
|
|
|
|
|
|