imkhan107 commited on
Commit
c21f3cc
·
1 Parent(s): 5f8ffbf

initial commit

Browse files
Files changed (5) hide show
  1. app.py +34 -0
  2. gemini.py +33 -0
  3. handler.py +14 -0
  4. prompts.py +90 -0
  5. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from handler import *
3
+
4
+ def main():
5
+ """ Creates a Streamlit page for code analysis """
6
+ prompt=None
7
+ code=None
8
+ # Title and description
9
+ st.set_page_config(page_title="CodeBatuk: Code Review Tool", page_icon="")
10
+ st.title("Code Batuk")
11
+ st.subheader("A code Analysis Tool")
12
+
13
+ col1, col2 = st.columns([3,1])
14
+ with col1:
15
+ # Text input fields
16
+ code = st.text_area(label="Enter your code here:", height=200)
17
+ #prompt = st.text_area(label="Custom prompt (optional):")
18
+ with col2:
19
+ # Radio buttons for analysis type
20
+ analysis_type = st.radio("Choose analysis type:",
21
+ ("Code review", "Code refinement", "Unit tests"))
22
+
23
+ # Submit button
24
+ if st.button("Submit"):
25
+ # Process the code and prompt based on analysis_type (implementation omitted for brevity)
26
+ if analysis_type == "Code review":
27
+ res=code_review(code, prompt)
28
+ #st.text_area(label="Result",value=res, height=300)
29
+ st.markdown(res)
30
+ st.success(f"Code analysis for {analysis_type} submitted successfully!")
31
+
32
+ if __name__ == "__main__":
33
+ main()
34
+ #git clone https://huggingface.co/spaces/imkhan107/codebatuk
gemini.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from typing import Optional
5
+
6
+ class GeminiModel:
7
+ """
8
+ This class is used to interact with the google llm models for text generation.
9
+
10
+ Args:
11
+ model: The name of the model to be used. Defaults to 'text-bison@002'.
12
+ max_output_tokens: The maximum number of tokens to generate. Defaults to 1024.
13
+ top_P: The probability of generating the next token. Defaults to 1.0.
14
+ temperature: The temperature of the model. Defaults to 0.0.
15
+ top_k: The number of top tokens to consider. Defaults to 5.
16
+ """
17
+
18
+ model=None
19
+ def __init__(self,
20
+ model: Optional[str] = 'gemini-pro',
21
+ max_output_tokens: Optional[int] = 1024,
22
+ top_p: Optional[float] = 1.0,
23
+ temperature: Optional[float] = 0.0,
24
+ top_k: Optional[int] = 5
25
+ ):
26
+ load_dotenv()
27
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
28
+ self.model=genai.GenerativeModel(model)
29
+
30
+ def execute(self,prompt):
31
+ print(prompt)
32
+ response=self.model.generate_content(prompt)
33
+ return response
handler.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gemini import GeminiModel
2
+ from prompts import *
3
+
4
+
5
+ def code_review(code,c_prompt=None):
6
+
7
+ if c_prompt is not None and len(c_prompt) > 30:
8
+ prompt=custom_prompt(code,c_prompt)
9
+ else:
10
+ prompt=default_gemini_prompt(code)
11
+ model=GeminiModel()
12
+ res= model.execute(prompt)
13
+
14
+ return res.text
prompts.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ TASK="As an experienced software developer responsible for code review and feedback, review the following python code by considering the best practices given in the guidelines below and provide your Observations and Suggestions."
2
+ INSTRUCTIONS="""Take a deep breath before providing the feedback and thoroughly understand the code logic.
3
+ Avoid verbosity and repetition in your feedback, focussing on essential points and offering valuable suggestions.
4
+ Add your own observations and suggestions as needed, and only include comments that are neccessary for clarity and improvement.
5
+ If you have specific code suggestions, ensure they are correct and aligned with the best practices.
6
+ """
7
+ GUIDELINES = f""" Use Intention-Revealing Names.
8
+ Pick one word per concept
9
+ Use Solution/Problem Domain Names
10
+ Classes should be small
11
+ Functions should be small
12
+ Do one Thing
13
+ Declare constants as applicable
14
+ Don't Repeat Yourself (Avoid Duplication)
15
+ Explain yourself in code
16
+ Make sure the code formatting is applied
17
+ Use Exceptions rather than Return codes
18
+ Don't return Null
19
+ Favor the use of standard exceptions
20
+ Don't ignore exceptions
21
+ Check parameters for validity
22
+ Return empty arrays or collections, not nulls
23
+ Minimize the accessibility of classes and members
24
+ In public classes, use accessor methods, not public fields
25
+ Minimize the scope of local variables
26
+ Refer to objects by their interfaces
27
+ Adhere to generally accepted naming conventions
28
+ Avoid finalizers
29
+ Synchronize access to shared mutable data
30
+ Valid unit test cases exist
31
+ Objects accessed by multiple threads are accessed only through a lock, or synchronized methods.
32
+ Race conditions have been handled
33
+ Locks are acquired and released in the right order to prevent deadlocks, even in error handling code.
34
+ All methods are commented in clear language
35
+ """
36
+
37
+ STEPS="""Follow the steps in the same order:
38
+ 1. Understand the code logic and structure.
39
+ 2. Notedown the observations based on the guodelines given above.
40
+ 3. Give suggestions for improving the code.
41
+ 4. Give an example of Final improved code.
42
+ """
43
+
44
+ def default_gemini_prompt(code, guidelines=None):
45
+ if guidelines is None:
46
+ guidelines=GUIDELINES
47
+ prompt=f"""<Task>: {TASK}
48
+ <Instructions>: {INSTRUCTIONS}
49
+
50
+ <Guidelines>: {guidelines}
51
+
52
+ <Steps to follow>: {STEPS}
53
+
54
+ <code>: {code}
55
+ """
56
+
57
+ return prompt
58
+
59
+
60
+ def prompt2(code):
61
+ prompt=f"""
62
+ <task>: As an experienced software developer responsible for code review and feedback, review the following code by considering the best practices given in the guidelines below and provide your Observations and Suggestions
63
+ Code: {code}
64
+
65
+ The following custom guidelines should be considered during the review:
66
+
67
+ {GUIDELINES}
68
+
69
+ Starting from the beginning of the code, analyze each line following a chain of thoughts. Consider the purpose of the code, its readability, efficiency, and adherence to the listed custom guidelines.
70
+
71
+ For each line, provide feedback that includes:
72
+
73
+ * Whether the code achieves its intended purpose.
74
+ * If there are any readability issues or areas for improvement.
75
+ * Suggestions for optimization or alternative approaches (if applicable).
76
+ * If there are any violations of the custom guidelines.
77
+
78
+ Break down your review into a step-by-step process, explaining your reasoning at each point. Maintain a clear and concise tone throughout the review.
79
+ Add your own observations and suggestions as needed, and only include comments that are neccessary for clarity and improvement.
80
+ If you have specific code suggestions, ensure they are correct and aligned with the best practices
81
+
82
+ **Overall Recommendation:**
83
+
84
+ Based on your analysis, provide an overall recommendation for the code. This could be approval with minor suggestions, approval with moderate changes, or rejection with a clear explanation for required improvements.
85
+ """
86
+
87
+ return prompt
88
+
89
+ def custom_prompt(c_prompt,code):
90
+ return f'{c_prompt}\n<Code>: {code}'
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain