Phaneendrabayi commited on
Commit
5df7911
Β·
verified Β·
1 Parent(s): b63e180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -62
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import streamlit as st
2
  import google.generativeai as genai
3
- import hashlib
4
- import re
5
 
6
  # Configure API Key
7
- api_key = "AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo"
8
  genai.configure(api_key=api_key)
9
 
10
  # System Instruction for AI Code Review
@@ -21,39 +20,6 @@ model = genai.GenerativeModel(
21
  system_instruction=system_prompt
22
  )
23
 
24
- # Cache storage for feedback history
25
- if "history" not in st.session_state:
26
- st.session_state.history = []
27
-
28
- @st.cache_data # Using cache_data for better handling of user-specific data
29
- def get_feedback_from_ai(user_code):
30
- """Function to generate AI code review feedback."""
31
- try:
32
- # Validate Python syntax
33
- compile(user_code, '<string>', 'exec')
34
-
35
- # Generate AI response
36
- response = model.generate_content(user_code)
37
- feedback = response.text.strip() if hasattr(response, "text") else "❌ Error: Unexpected response format."
38
-
39
- return feedback
40
- except SyntaxError as e:
41
- st.warning(f"⚠️ Invalid Python code: {e}")
42
- return None
43
- except Exception as e:
44
- st.error("🚨 An error occurred during code review.")
45
- st.exception(e)
46
- return None
47
-
48
- def add_to_history(user_code, feedback):
49
- """Add code and its feedback to the session history."""
50
- history_entry = {
51
- "code": user_code,
52
- "feedback": feedback,
53
- "hash": hashlib.md5(user_code.encode()).hexdigest()
54
- }
55
- st.session_state.history.append(history_entry)
56
-
57
  # Streamlit UI
58
  st.title("πŸ“’ AI Code Reviewer")
59
  st.write("πŸ” Enter your Python code below and receive AI-powered feedback.")
@@ -65,35 +31,28 @@ user_code = st.text_area(
65
  placeholder="Write or paste your Python code here..."
66
  )
67
 
68
- # Display past feedback history
69
- if st.session_state.history:
70
- with st.expander("πŸ•’ Review History", expanded=True):
71
- for idx, entry in enumerate(reversed(st.session_state.history)):
72
- with st.expander(f"πŸ”™ Review {idx + 1}"):
73
- st.code(entry["code"], language="python")
74
- st.write(entry["feedback"])
75
-
76
  # Button to trigger code review
77
  if st.button("πŸš€ Generate Review") and user_code:
78
  if not user_code.strip():
79
  st.warning("⚠️ Please enter some Python code to review.")
80
  else:
81
  with st.spinner("πŸ€– Analyzing your code..."):
82
- feedback = get_feedback_from_ai(user_code)
83
-
84
- if feedback:
85
- # Store review in history
86
- add_to_history(user_code, feedback)
87
-
88
- # Extract Python code blocks and explanations using regex
89
- code_blocks = re.split(r'```python|```', feedback, flags=re.IGNORECASE)
90
- with st.expander("πŸ’‘ AI Review Feedback", expanded=True):
91
- for block in code_blocks:
92
- cleaned_block = block.strip()
93
- if cleaned_block:
94
- if re.search(r'\b(def |import |=|return |class |for |while |if |elif |else )', cleaned_block):
95
- st.code(cleaned_block, language="python")
96
- else:
97
- st.write(cleaned_block)
98
- else:
99
- st.error("❌ Code review failed. Please check your input or try again.")
 
 
1
  import streamlit as st
2
  import google.generativeai as genai
3
+ from IPython import display
 
4
 
5
  # Configure API Key
6
+ api_key = st.secrets["GOOGLE_API_KEY"]
7
  genai.configure(api_key=api_key)
8
 
9
  # System Instruction for AI Code Review
 
20
  system_instruction=system_prompt
21
  )
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Streamlit UI
24
  st.title("πŸ“’ AI Code Reviewer")
25
  st.write("πŸ” Enter your Python code below and receive AI-powered feedback.")
 
31
  placeholder="Write or paste your Python code here..."
32
  )
33
 
 
 
 
 
 
 
 
 
34
  # Button to trigger code review
35
  if st.button("πŸš€ Generate Review") and user_code:
36
  if not user_code.strip():
37
  st.warning("⚠️ Please enter some Python code to review.")
38
  else:
39
  with st.spinner("πŸ€– Analyzing your code..."):
40
+ try:
41
+ # Validate Python syntax
42
+ compile(user_code, '<string>', 'exec')
43
+
44
+ # Generate AI response
45
+ response = model.generate_content(user_code)
46
+ feedback = response.text.strip() if hasattr(response, "text") else "❌ Error: Unexpected response format."
47
+
48
+ # Display feedback
49
+ if feedback:
50
+ st.subheader("πŸ’‘ AI Review Feedback:")
51
+ st.write(feedback)
52
+ else:
53
+ st.error("❌ Code review failed. Please check your input or try again.")
54
+ except SyntaxError as e:
55
+ st.warning(f"⚠️ Invalid Python code: {e}")
56
+ except Exception as e:
57
+ st.error("🚨 An error occurred during code review.")
58
+ st.exception(e)