vsagar100 commited on
Commit
782d1a7
·
verified ·
1 Parent(s): 1c7a008

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -73
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
- # Streamlit UI for inputting GitHub repository details
10
- st.title("GitHub Repository Code Reviewer")
11
- # Variables for GitHub repository details
12
- REPO_OWNER = "vsagar100"
13
- REPO_NAME = st.text_input("Enter the GitHub repository name:")
14
- GITHUB_BRANCH = st.text_input("Enter the branch or tag to download (default: main):", "main")
15
- GITHUB_TOKEN = "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" #os.getenv("GITHUB_TOKEN") # Specify your GitHub token
16
-
17
- # GitHub API endpoint to download the repo as a zip file
18
- #repo_url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/zipball/main"
19
- GITHUB_REPO_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/zipball/{GITHUB_BRANCH}"
20
-
21
- if st.button("Review Code") and GITHUB_REPO_URL:
22
- # Directory structure setup
23
- download_directory = "downloaded_repo"
24
- output_directory = "output_reviews"
25
-
26
- # Ensure the directories exist
27
- os.makedirs(download_directory, exist_ok=True)
28
- os.makedirs(output_directory, exist_ok=True)
29
-
30
- # Initialize the code reviewer and review manager
31
- with st.spinner("Initializing CodeReviewer and ReviewManager..."):
32
- code_reviewer = CodeReviewer()
33
- review_manager = ReviewManager(reviewer=code_reviewer)
34
-
35
-
36
- try:
37
- # Download GitHub repository
38
- with st.spinner("Downloading GitHub repository..."):
39
- review_manager.download_repo(GITHUB_REPO_URL, GITHUB_TOKEN, download_directory)
40
- st.success("Repository downloaded successfully.")
41
-
42
-
43
- # Find all YAML files in the downloaded repository
44
- with st.spinner("Searching for YAML files in the downloaded repository..."):
45
- yaml_files = glob.glob(os.path.join(download_directory, "**", "*.yml"), recursive=True)
46
- yaml_files += glob.glob(os.path.join(download_directory, "**", "*.yaml"), recursive=True)
47
- st.info(f"Found {len(yaml_files)} YAML files for review.")
48
-
49
- # Process files and generate reviews
50
- with st.spinner("Processing files for review..."):
51
- reviews = review_manager.process_files(yaml_files)
52
- st.success("Files processed successfully.")
53
-
54
- # Save reviews to JSON
55
- output_json_path = os.path.join(output_directory, "code_review_results.json")
56
- review_manager.save_reviews_to_json(reviews, output_json_path)
57
-
58
- # Display review results
59
- st.success(f"Reviews saved to {output_json_path}")
60
- for review in reviews:
61
- st.subheader(f"Review for {review['filename']}")
62
- st.text(review['review'])
63
-
64
- # Display JSON output on the UI
65
- st.subheader("Full JSON Review Output")
66
- st.json(reviews)
67
-
68
- # Provide download link for JSON results
69
- with open(output_json_path, "r") as json_file:
70
- st.download_button("Download JSON Results", json_file, file_name="code_review_results.json")
71
-
72
- except Exception as e:
73
- st.error(f"An error occurred: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)}")