File size: 4,464 Bytes
2a1cdbf
 
be94910
 
 
 
 
2a1cdbf
be94910
 
 
 
 
2a1cdbf
be94910
 
2a1cdbf
be94910
 
 
 
 
 
 
 
2a1cdbf
be94910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a1cdbf
be94910
 
 
 
 
 
 
 
 
2a1cdbf
be94910
 
 
 
 
 
2a1cdbf
be94910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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!")