typicaleoxx/code-explainer-ai
0
1# this file defines the Streamlit user interface2# it allows a user to paste code and receive an AI generated analysis3 4 5import streamlit as st6 7from prompt_template import build_prompt8from ai_client import generate_explanation9from parser import parse_analysis10from static_analyzer import analyze_code_structure11 12 13# configure page settings14st.set_page_config(page_title="Code Intelligence AI", layout="wide")15 16 17# main title displayed in the browser18st.title("Code Intelligence AI")19 20 21# short explanation of what the tool does22st.markdown(23 """24Paste a code snippet and receive a structured engineering analysis.25 26The AI will analyze:27 28• Code purpose 29• Logic breakdown 30• Execution walkthrough 31• Code quality issues 32• Performance analysis 33• Security concerns 34• Refactored version35"""36)37 38 39# create a large input box for the user to paste code40code_input = st.text_area("Paste your code here", height=300)41 42 43# analyze button triggers the AI analysis44if st.button("Analyze Code"):45 46 # ensure the user entered code47 if code_input.strip() == "":48 st.warning("Please paste some code before analyzing.")49 50 else:51 52 # show the submitted code with syntax highlighting53 st.subheader("Submitted Code")54 st.code(code_input, language="python")55 56 # run static AST analysis on the submitted code57 structure_analysis = analyze_code_structure(code_input)58 59 # display static analysis results to the user60 st.subheader("Static Code Analysis")61 62 if "error" in structure_analysis:63 st.error(structure_analysis["error"])64 st.stop()65 66 else:67 st.json(structure_analysis)68 69 # build the prompt sent to the AI model including AST signals70 prompt = build_prompt(code_input, structure_analysis)71 72 # show loading spinner while the AI processes the request73 with st.spinner("Analyzing code..."):74 75 # send prompt to the AI model76 result = generate_explanation(prompt)77 78 # parse the AI response into structured sections79 analysis = parse_analysis(result)80 81 # render the parsed sections in the UI82 83 st.subheader("Code Summary")84 st.write(analysis["Code Summary"])85 86 st.subheader("Logic Explanation")87 st.write(analysis["Logic Explanation"])88 89 st.subheader("Execution Walkthrough")90 st.write(analysis["Execution Walkthrough"])91 92 st.subheader("Code Quality Issues")93 st.write(analysis["Code Quality Issues"])94 95 st.subheader("Performance Analysis")96 st.write(analysis["Performance Analysis"])97 98 st.subheader("Security Concerns")99 st.write(analysis["Security Concerns"])100 101 st.subheader("Improved Version")102 st.code(analysis["Improved Version"], language="python")103 