import streamlit as st import os import tempfile import shutil from code_analyzer import CodeAnalyzer import plotly.express as px import pandas as pd st.set_page_config( page_title="Code Analyzer", page_icon="🔍", layout="wide" ) st.title("🔍 Code Project Analyzer") st.write("Upload your code files and analyze them with AI-powered insights") def create_metrics_chart(metrics): """Create a bar chart for code metrics""" df = pd.DataFrame({ 'Metric': list(metrics.keys()), 'Value': list(metrics.values()) }) fig = px.bar(df, x='Metric', y='Value', title='Code Metrics') return fig def display_tech_stack(tech_stack): """Display technology stack in an organized way""" st.subheader("🛠️ Technology Stack") cols = st.columns(3) with cols[0]: st.write("**Languages**") if tech_stack["languages"]: for lang in tech_stack["languages"]: st.write(f"- {lang}") else: st.write("No languages detected") with cols[1]: st.write("**Frameworks**") if tech_stack["frameworks"]: for framework in tech_stack["frameworks"]: st.write(f"- {framework}") else: st.write("No frameworks detected") with cols[2]: st.write("**Dependencies**") if tech_stack["dependencies"]: for dep in tech_stack["dependencies"]: st.write(f"- {dep}") else: st.write("No dependencies detected") def save_uploaded_files(uploaded_files): """Save uploaded files to a temporary directory""" temp_dir = tempfile.mkdtemp() for uploaded_file in uploaded_files: file_path = os.path.join(temp_dir, uploaded_file.name) os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "wb") as f: f.write(uploaded_file.getbuffer()) return temp_dir # File upload section uploaded_files = st.file_uploader( "Upload your code files", accept_multiple_files=True, type=['py', 'java', 'js', 'jsx', 'ts', 'tsx'] ) # Questions input st.subheader("📝 Analysis Questions") default_questions = """What is the project's abstract? What is the system architecture? What are the software requirements? What are the hardware requirements?""" questions = st.text_area( "Enter your questions (one per line)", value=default_questions, height=150 ) analyze_button = st.button("🔍 Analyze Code") if analyze_button and uploaded_files: with st.spinner("Analyzing your code..."): # Save uploaded files temp_dir = save_uploaded_files(uploaded_files) # Save questions to a temporary file questions_file = os.path.join(temp_dir, "questions.txt") with open(questions_file, "w") as f: f.write(questions) try: # Run analysis analyzer = CodeAnalyzer() results = analyzer.analyze_project(temp_dir, questions_file) # Display results in tabs tab1, tab2, tab3 = st.tabs(["📊 Overview", "💻 Code Metrics", "❓ Q&A"]) with tab1: st.subheader("🎯 Project Objective") st.write(results["objective"]) display_tech_stack(results["tech_stack"]) with tab2: st.subheader("📊 Code Metrics") metrics_chart = create_metrics_chart(results["metrics"]) st.plotly_chart(metrics_chart, use_container_width=True) # Complexity assessment complexity = "Low" if results["metrics"]["complexity_score"] < 10 else \ "Medium" if results["metrics"]["complexity_score"] < 30 else "High" st.info(f"Project Complexity: {complexity}") with tab3: st.subheader("❓ Analysis Results") for question, answer in results["answers"].items(): with st.expander(question): st.write(answer) except Exception as e: st.error(f"An error occurred during analysis: {str(e)}") finally: # Cleanup shutil.rmtree(temp_dir) else: if analyze_button: st.warning("Please upload some code files first!")