File size: 4,898 Bytes
c0e81fb
1dea712
c0e81fb
 
 
 
1367fa8
 
 
1f431b3
 
94a57a0
c0e81fb
145f26c
76115d7
 
c0e81fb
 
 
4e33c9b
 
1367fa8
 
 
 
 
 
 
1dea712
1367fa8
1dea712
2d08044
c0e81fb
 
4e33c9b
c0e81fb
 
1dea712
1367fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f431b3
94a57a0
 
 
 
 
 
 
 
 
 
 
c0e81fb
1f431b3
94a57a0
 
 
1f431b3
 
94a57a0
 
 
 
 
 
1f431b3
1367fa8
 
 
94a57a0
1367fa8
 
 
c0e81fb
1367fa8
 
c0e81fb
94a57a0
 
 
 
 
 
 
 
 
 
 
c0e81fb
1f431b3
94a57a0
 
 
1f431b3
 
94a57a0
 
 
 
 
 
1f431b3
1367fa8
 
 
 
c0e81fb
1367fa8
 
 
 
 
 
c0e81fb
1367fa8
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
import os
import pandas as pd
import streamlit as st
from datetime import datetime
from groq import Groq
from logic import LLMClient, CodeProcessor
from batch_code_logic_csv import csv_read_batch_code
import zipfile
import io
import markdown2
import pdfkit
import tempfile

client = Groq(api_key=os.getenv("GROQ_API_KEY"))
llm_obj = LLMClient(client)
processor = CodeProcessor(llm_obj)

st.title("Code Analysis with LLMs")

st.sidebar.title("Input Options")

code_input_method = st.sidebar.radio("How would you like to provide your code?",
                                     ("Upload CSV file", "Upload Code File"))

code_dict = {}

if code_input_method == "Upload CSV file":
    uploaded_file = st.sidebar.file_uploader("Upload your CSV/Excel file", type=["csv", "xlsx"])

    if uploaded_file is not None:
        dataframe = pd.read_csv(uploaded_file)
        code_dict = csv_read_batch_code(dataframe)

elif code_input_method == "Upload Code File":
    uploaded_file = st.sidebar.file_uploader("Upload your code file", type=["py", "txt"])
    if uploaded_file is not None:
        code_text = uploaded_file.read().decode("utf-8")
        code_dict = {"single_code": code_text}

model_choice = st.sidebar.selectbox("Select LLM Model",
                                    ["llama-3.2-90b-text-preview", "llama-3.2-90b-text-preview", "llama3-8b-8192"])

if code_dict:
    unique_key = st.sidebar.selectbox("Select a Key for Analysis", list(code_dict.keys()))

    if st.sidebar.button("Analyze Code") and unique_key:
        code_text = code_dict[unique_key]
        markdown_output = processor.process_code(code_text, model_choice)

        with st.expander(f"Analysis for {unique_key}"):
            st.markdown(markdown_output)

        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")

        with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as tmp_md_file:
            tmp_md_file.write(markdown_output.encode('utf-8'))
            tmp_md_file_path = tmp_md_file.name

        with open(tmp_md_file_path, "r") as md_file:
            st.download_button(
                label=f"Download {unique_key} Result as Markdown",
                data=md_file,
                file_name=f"code_analysis_{unique_key}_{timestamp}.md",
                mime="text/markdown"
            )

        html_output = markdown2.markdown(markdown_output)
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf_file:
            pdf_file_path = tmp_pdf_file.name
            pdfkit.from_string(html_output, pdf_file_path)

        with open(pdf_file_path, "rb") as pdf_file:
            st.download_button(
                label=f"Download {unique_key} Result as PDF",
                data=pdf_file,
                file_name=f"code_analysis_{unique_key}_{timestamp}.pdf",
                mime="application/pdf"
            )

    if st.sidebar.button("Batch Predict"):
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        all_markdowns = {}

        for key, code_text in code_dict.items():
            markdown_output = processor.process_code(code_text, model_choice)
            all_markdowns[key] = markdown_output

            with st.expander(f"Analysis for {key}"):
                st.markdown(markdown_output)

            with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as tmp_md_file:
                tmp_md_file.write(markdown_output.encode('utf-8'))
                tmp_md_file_path = tmp_md_file.name

            with open(tmp_md_file_path, "r") as md_file:
                st.download_button(
                    label=f"Download {key} Result as Markdown",
                    data=md_file,
                    file_name=f"code_analysis_{key}_{timestamp}.md",
                    mime="text/markdown"
                )

            html_output = markdown2.markdown(markdown_output)
            with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf_file:
                pdf_file_path = tmp_pdf_file.name
                pdfkit.from_string(html_output, pdf_file_path)

            with open(pdf_file_path, "rb") as pdf_file:
                st.download_button(
                    label=f"Download {key} Result as PDF",
                    data=pdf_file,
                    file_name=f"code_analysis_{key}_{timestamp}.pdf",
                    mime="application/pdf"
                )

        zip_buffer = io.BytesIO()
        with zipfile.ZipFile(zip_buffer, "w") as zip_file:
            for key, markdown_output in all_markdowns.items():
                zip_file.writestr(f"code_analysis_{key}_{timestamp}.md", markdown_output)

        st.download_button(
            label="Download All as Zip",
            data=zip_buffer.getvalue(),
            file_name=f"code_analysis_batch_{timestamp}.zip",
            mime="application/zip"
        )
else:
    st.write("Please upload your file to analyze.")