Spaces:
Sleeping
Sleeping
ishworrsubedii
commited on
Commit
·
1367fa8
1
Parent(s):
ecda74c
add: batch code and batch download code
Browse files- app.py +62 -22
- batch_code_logic_csv.py +28 -0
- logic.py +0 -4
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
-
"""
|
2 |
-
Created By: ishwor subedi
|
3 |
-
Date: 2024-10-03
|
4 |
-
"""
|
5 |
import os
|
6 |
import streamlit as st
|
7 |
from datetime import datetime
|
8 |
from groq import Groq
|
9 |
from logic import LLMClient, CodeProcessor
|
|
|
|
|
|
|
10 |
|
11 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
12 |
|
@@ -14,32 +13,73 @@ st.title("Code Analysis with LLMs")
|
|
14 |
|
15 |
st.sidebar.title("Input Options")
|
16 |
|
17 |
-
code_input_method = st.sidebar.radio("How would you like to provide your code?",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
code_text = ""
|
20 |
-
if code_input_method == "Paste Code":
|
21 |
-
code_text = st.sidebar.text_area("Paste your code here:")
|
22 |
elif code_input_method == "Upload Code File":
|
23 |
uploaded_file = st.sidebar.file_uploader("Upload your code file", type=["py", "txt"])
|
24 |
if uploaded_file is not None:
|
25 |
code_text = uploaded_file.read().decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
processor = CodeProcessor(llm_obj)
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
else:
|
45 |
-
st.write("Please
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from datetime import datetime
|
4 |
from groq import Groq
|
5 |
from logic import LLMClient, CodeProcessor
|
6 |
+
from batch_code_logic_csv import csv_read_batch_code
|
7 |
+
import zipfile
|
8 |
+
import io
|
9 |
|
10 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
11 |
|
|
|
13 |
|
14 |
st.sidebar.title("Input Options")
|
15 |
|
16 |
+
code_input_method = st.sidebar.radio("How would you like to provide your code?",
|
17 |
+
("Upload CSV file", "Upload Code File"))
|
18 |
+
|
19 |
+
code_dict = {}
|
20 |
+
|
21 |
+
if code_input_method == "Upload CSV file":
|
22 |
+
uploaded_file = st.sidebar.file_uploader("Upload your CSV/Excel file", type=["csv", "xlsx"])
|
23 |
+
if uploaded_file is not None:
|
24 |
+
code_dict = csv_read_batch_code(uploaded_file) # returns a dict with keys and code snippets
|
25 |
|
|
|
|
|
|
|
26 |
elif code_input_method == "Upload Code File":
|
27 |
uploaded_file = st.sidebar.file_uploader("Upload your code file", type=["py", "txt"])
|
28 |
if uploaded_file is not None:
|
29 |
code_text = uploaded_file.read().decode("utf-8")
|
30 |
+
code_dict = {"single_code": code_text} # Wrap in dict for consistency
|
31 |
+
|
32 |
+
model_choice = st.sidebar.selectbox("Select LLM Model",
|
33 |
+
["llama-3.2-90b-text-preview", "llama-3.2-90b-text-preview", "llama3-8b-8192"])
|
34 |
+
|
35 |
+
if code_dict:
|
36 |
+
unique_key = st.sidebar.selectbox("Select a Key for Analysis", list(code_dict.keys()))
|
37 |
+
|
38 |
+
if st.sidebar.button("Analyze Code") and unique_key:
|
39 |
+
llm_obj = LLMClient(client)
|
40 |
+
processor = CodeProcessor(llm_obj)
|
41 |
+
|
42 |
+
code_text = code_dict[unique_key]
|
43 |
+
markdown_output = processor.process_code(code_text, model_choice)
|
44 |
+
|
45 |
+
with st.expander(f"Analysis for {unique_key}"):
|
46 |
+
st.markdown(markdown_output)
|
47 |
+
|
48 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
49 |
+
st.download_button(
|
50 |
+
label=f"Download {unique_key} Result as Markdown",
|
51 |
+
data=markdown_output,
|
52 |
+
file_name=f"code_analysis_{unique_key}_{timestamp}.md",
|
53 |
+
mime="text/markdown"
|
54 |
+
)
|
55 |
|
56 |
+
if st.sidebar.button("Batch Predict"):
|
57 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
58 |
+
all_markdowns = {}
|
59 |
+
for key, code_text in code_dict.items():
|
60 |
+
markdown_output = processor.process_code(code_text, model_choice)
|
61 |
+
all_markdowns[key] = markdown_output
|
62 |
|
63 |
+
with st.expander(f"Analysis for {key}"):
|
64 |
+
st.markdown(markdown_output)
|
|
|
65 |
|
66 |
+
st.download_button(
|
67 |
+
label=f"Download {key} Result as Markdown",
|
68 |
+
data=markdown_output,
|
69 |
+
file_name=f"code_analysis_{key}_{timestamp}.md",
|
70 |
+
mime="text/markdown"
|
71 |
+
)
|
72 |
|
73 |
+
zip_buffer = io.BytesIO()
|
74 |
+
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
|
75 |
+
for key, markdown_output in all_markdowns.items():
|
76 |
+
zip_file.writestr(f"code_analysis_{key}_{timestamp}.md", markdown_output)
|
77 |
|
78 |
+
st.download_button(
|
79 |
+
label="Download All as Zip",
|
80 |
+
data=zip_buffer.getvalue(),
|
81 |
+
file_name=f"code_analysis_batch_{timestamp}.zip",
|
82 |
+
mime="application/zip"
|
83 |
+
)
|
84 |
else:
|
85 |
+
st.write("Please upload your file to analyze.")
|
batch_code_logic_csv.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2024-10-04
|
4 |
+
"""
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
|
8 |
+
def csv_read_batch_code(dataframe):
|
9 |
+
dataframe.dropna()
|
10 |
+
|
11 |
+
dataframe['Logic ID'].unique()
|
12 |
+
|
13 |
+
unique_value_indices = {}
|
14 |
+
|
15 |
+
for logic_id in dataframe['Logic ID'].unique():
|
16 |
+
row_indices = dataframe[dataframe['Logic ID'] == logic_id].index.tolist()
|
17 |
+
unique_value_indices[logic_id] = [min(row_indices), max(row_indices)]
|
18 |
+
|
19 |
+
print(unique_value_indices)
|
20 |
+
|
21 |
+
code_dict = {}
|
22 |
+
|
23 |
+
for logic_id, (min_row, max_row) in unique_value_indices.items():
|
24 |
+
codes = dataframe.loc[min_row:max_row, 'Code'].tolist()
|
25 |
+
|
26 |
+
code_dict[logic_id] = codes
|
27 |
+
|
28 |
+
return code_dict
|
logic.py
CHANGED
@@ -1,7 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
from groq import Groq
|
3 |
-
|
4 |
-
|
5 |
class LLMClient:
|
6 |
def __init__(self, api_client):
|
7 |
self.client = api_client
|
|
|
|
|
|
|
|
|
|
|
1 |
class LLMClient:
|
2 |
def __init__(self, api_client):
|
3 |
self.client = api_client
|