Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,99 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
-
# Import necessary modules from lib
|
4 |
-
from lib.code_reviewer import CodeReviewer, ReviewManager
|
5 |
-
import streamlit as st
|
6 |
-
import os
|
7 |
-
import glob
|
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 |
-
with st.spinner("
|
45 |
-
|
46 |
-
|
47 |
-
st.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
#
|
59 |
-
st.
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
st.
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
# Import necessary modules from lib
|
4 |
+
from lib.code_reviewer import CodeReviewer, ReviewManager
|
5 |
+
import streamlit as st
|
6 |
+
import os
|
7 |
+
import glob
|
8 |
+
import json
|
9 |
+
from datasets import DatasetDict
|
10 |
+
|
11 |
+
# Sample Training Data for Ansible Code Standards
|
12 |
+
sample_data = [
|
13 |
+
{
|
14 |
+
"prompt": "Review the following Ansible script thoroughly according to best practices:\n\n- Avoid using hard-coded values\n- Ensure idempotency\n- Proper error handling\n\nCode:\n---\n- name: Install nginx\n hosts: web\n tasks:\n - name: Install nginx package\n apt:\n name: nginx\n state: present",
|
15 |
+
"response": "Ensure that the playbook includes proper error handling for the package installation. Consider using a retry mechanism and notify handlers if required. Avoid hard-coding package names where possible."
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"prompt": "Review the following Ansible script thoroughly according to best practices:\n\n- Avoid using hard-coded values\n- Ensure idempotency\n- Use variables\n\nCode:\n---\n- name: Create a directory\n hosts: all\n tasks:\n - name: Create /var/www directory\n file:\n path: /var/www\n state: directory",
|
19 |
+
"response": "It is recommended to use variables for paths such as '/var/www' to ensure flexibility. The task is idempotent, which is good, but consider adding more context to describe the usage of the directory."
|
20 |
+
}
|
21 |
+
]
|
22 |
+
|
23 |
+
# Create a DatasetDict for Training
|
24 |
+
training_dataset = DatasetDict({
|
25 |
+
"train": sample_data,
|
26 |
+
"validation": sample_data
|
27 |
+
})
|
28 |
+
|
29 |
+
# Streamlit UI for inputting GitHub repository details
|
30 |
+
st.title("GitHub Repository Code Reviewer")
|
31 |
+
|
32 |
+
# Input for GitHub repository URL
|
33 |
+
GITHUB_REPO_URL = st.text_input("Enter the GitHub repository URL:")
|
34 |
+
GITHUB_BRANCH = st.text_input("Enter the branch or tag to download (default: main):", "main")
|
35 |
+
|
36 |
+
# Check if the GitHub token is provided as a secret in Streamlit
|
37 |
+
if "GITHUB_TOKEN" not in st.secrets:
|
38 |
+
st.error("GitHub token is not available. Please set it in the secrets.")
|
39 |
+
else:
|
40 |
+
GITHUB_TOKEN = st.secrets["GITHUB_TOKEN"]
|
41 |
+
|
42 |
+
# Add a button for fine-tuning the model
|
43 |
+
if st.button("Fine-Tune Model"):
|
44 |
+
with st.spinner("Fine-tuning the model with provided dataset..."):
|
45 |
+
code_reviewer = CodeReviewer()
|
46 |
+
code_reviewer.fine_tune_model(training_dataset)
|
47 |
+
st.success("Model fine-tuned successfully.")
|
48 |
+
|
49 |
+
if st.button("Review Code") and GITHUB_REPO_URL:
|
50 |
+
# Directory structure setup
|
51 |
+
download_directory = "downloaded_repo"
|
52 |
+
output_directory = "output_reviews"
|
53 |
+
|
54 |
+
# Ensure the directories exist
|
55 |
+
os.makedirs(download_directory, exist_ok=True)
|
56 |
+
os.makedirs(output_directory, exist_ok=True)
|
57 |
+
|
58 |
+
# Initialize the code reviewer and review manager
|
59 |
+
with st.spinner("Initializing CodeReviewer and ReviewManager..."):
|
60 |
+
code_reviewer = CodeReviewer()
|
61 |
+
review_manager = ReviewManager(reviewer=code_reviewer)
|
62 |
+
|
63 |
+
try:
|
64 |
+
# Download GitHub repository
|
65 |
+
with st.spinner("Downloading GitHub repository..."):
|
66 |
+
review_manager.download_repo(GITHUB_REPO_URL, GITHUB_BRANCH, GITHUB_TOKEN, download_directory)
|
67 |
+
st.success("Repository downloaded successfully.")
|
68 |
+
|
69 |
+
# Find all YAML files in the downloaded repository
|
70 |
+
with st.spinner("Searching for YAML files in the downloaded repository..."):
|
71 |
+
yaml_files = glob.glob(os.path.join(download_directory, "**", "*.yml"), recursive=True)
|
72 |
+
yaml_files += glob.glob(os.path.join(download_directory, "**", "*.yaml"), recursive=True)
|
73 |
+
st.info(f"Found {len(yaml_files)} YAML files for review.")
|
74 |
+
|
75 |
+
# Process files and generate reviews
|
76 |
+
with st.spinner("Processing files for review..."):
|
77 |
+
reviews = review_manager.process_files(yaml_files)
|
78 |
+
st.success("Files processed successfully.")
|
79 |
+
|
80 |
+
# Save reviews to JSON
|
81 |
+
output_json_path = os.path.join(output_directory, "code_review_results.json")
|
82 |
+
review_manager.save_reviews_to_json(reviews, output_json_path)
|
83 |
+
|
84 |
+
# Display review results
|
85 |
+
st.success(f"Reviews saved to {output_json_path}")
|
86 |
+
for review in reviews:
|
87 |
+
st.subheader(f"Review for {review['filename']}")
|
88 |
+
st.text(review['review'])
|
89 |
+
|
90 |
+
# Display JSON output on the UI
|
91 |
+
st.subheader("Full JSON Review Output")
|
92 |
+
st.json(reviews)
|
93 |
+
|
94 |
+
# Provide download link for JSON results
|
95 |
+
with open(output_json_path, "r") as json_file:
|
96 |
+
st.download_button("Download JSON Results", json_file, file_name="code_review_results.json")
|
97 |
+
|
98 |
+
except Exception as e:
|
99 |
+
st.error(f"An error occurred: {str(e)}")
|