Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonshambo committed Jul 7, 2024
2 parents 239891a + 2e12c6d commit ae94212
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 35 deletions.
5 changes: 2 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .gemini_analyzer import CodeAnalyzer
__all__ = [
'CodeAnalyzer'
]

__all__ = ["CodeAnalyzer"]
7 changes: 3 additions & 4 deletions app/gemini_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def __init__(self) -> None:
ResponseData,
) # prevent circular import

self.language: Optional[str] = "python"
self.style_guide: Optional[str] = "google official"
self.language: str[Optional] = "python"
self.style_guide: str[Optional] = "google official"
self.gemini_model = genai.GenerativeModel(
AnalyzerConfigs.gemini_models[1],
generation_config={
Expand All @@ -35,8 +35,7 @@ def analyze_code(self) -> Union[list, None]:
This method analyzes the input code and generates a style guide using the Gemini model.
"""
try:
prompt = f"""
Analyze the following {self.language} code according to {self.style_guide} style guidelines:
prompt = f"""Analyze the following {self.language} code according to {self.style_guide} style guidelines:
```{self.language}
Expand Down
40 changes: 25 additions & 15 deletions app/gemini_config.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import os
import streamlit as st
from typing_extensions import TypedDict


class AnalyzerConfigs:
'''
"""
This class contains configuration settings for the CodeAnalyzer class.
'''
"""

gemini_models: list = [
# INFO : https://ai.google.dev/gemini-api/docs/json-mode?lang=python
"gemini-1.5-flash",
"gemini-1.5-pro",
"gemini-1.5-pro", # INFO : https://ai.google.dev/gemini-api/docs/json-mode?lang=python
]
famous_languages: list = ["Python", "Java", "JavaScript", "C++", "C#", "Ruby", "Go", "Swift", "Rust"]
genai_api_key: str = os.getenv("GENAI_API_KEY")
famous_languages: list = [
"Python",
"Java",
"JavaScript",
"C++",
"C#",
"Ruby",
"Go",
"Swift",
"Rust",
]
genai_api_key: str = st.secrets["GENAI_API_KEY"]
style_guides: dict = {
'google_style': 'https://google.github.io/styleguide/pyguide.html',
'pep8': 'https://pep8.org/',
"google style": "https://google.github.io/styleguide/pyguide.html",
"pep8": "https://pep8.org/",
}


class ResponseData(TypedDict):
'''
For schema based responses like `JSON` or `Dict` objects, used TypedDict to define the structure of the response.
'''
"""
For schema based responses like `JSON`
"""

line: int
message: str
severity: str
start_char: int
end_char: int # Add this line to include the end_char in the response
5 changes: 3 additions & 2 deletions app/gemini_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import json
from gemini_analyzer import CodeAnalyzer


def main():
code = sys.argv[1]
analyzer = CodeAnalyzer()
analyzer.setCode(code)
style_guide = analyzer.analyze_code()
if style_guide:
print(json.dumps({'style_guide': style_guide}))
print(json.dumps({"style_guide": style_guide}))
else:
print(json.dumps({'error': 'Analysis failed'}))
print(json.dumps({"error": "Analysis failed"}))


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion examples/class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print("Woof!")


my_dog = Dog("Buddy", "Golden Retriever")
my_dog.bark()
5 changes: 3 additions & 2 deletions examples/greet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
def greet(name):
"""Greets the person with the given name."""
"""Greets the person with the given name."""

print("Hello,", name)

print("Hello,", name)

greet("Alice")
8 changes: 5 additions & 3 deletions examples/sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
def calculate_sum( a , b): # Intentional spacing issues
def calculate_sum(a, b): # Intentional spacing issues
"""This function calculates the sum of two numbers."""
result=a+b
return result # Incorrect indentation
result = a + b


return result # Incorrect indentation

print(calculate_sum(10, 5))
7 changes: 4 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

app = Flask(__name__)

@app.route('/analyze', methods=['POST'])

@app.route("/analyze", methods=["POST"])
def analyze():
data = request.get_json()
code = data['code']
code = data["code"]

analyzer = CodeAnalyzer()
analyzer._input_code = code
Expand All @@ -17,7 +18,7 @@ def analyze():
if style_guide:
return jsonify({'style_guide': issues}), 200
else:
return jsonify({'error': 'Analysis failed'}), 500
return jsonify({"error": "Analysis failed"}), 500


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if st.button("Analyze"):
if code_input:
with st.spinner('Analyzing...'):
with st.spinner("Analyzing..."):
code_handler._input_code = code_input
response = code_handler.analyze_code()
data = json.loads(response)
Expand All @@ -25,4 +25,4 @@
st.json(response) # Display the Gemini-generated style guide
time.sleep(5)
else:
st.warning("Please paste your code to analyze.")
st.warning("Please paste your code to analyze.")

0 comments on commit ae94212

Please sign in to comment.