DasariHarshitha commited on
Commit
397f59d
Β·
verified Β·
1 Parent(s): 3995a7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -36
app.py CHANGED
@@ -1,28 +1,21 @@
1
  import streamlit as st
2
- import openai
3
  import google.generativeai as genai
4
 
5
  # --------------------------
6
  # 1. Page Configuration
7
  # --------------------------
8
- st.set_page_config(page_title="AI-Powered Code Reviewer", layout="wide")
9
- st.title("🐍 AI-Powered Python Code Reviewer")
10
 
11
  # --------------------------
12
- # 2. Sidebar API Selection
13
  # --------------------------
14
- st.sidebar.header("βš™οΈ Settings")
15
-
16
- api_choice = st.sidebar.radio("Choose API:", ["OpenAI", "Google Gemini"])
17
-
18
- if api_choice == "OpenAI":
19
- api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
20
- if api_key:
21
- openai.api_key = api_key
22
  else:
23
- api_key = st.sidebar.text_input("Enter Google API Key", type="password")
24
- if api_key:
25
- genai.configure(api_key=api_key)
26
 
27
  # --------------------------
28
  # 3. User Code Input
@@ -33,21 +26,20 @@ user_code = st.text_area("Paste Python code here:", height=300)
33
  # --------------------------
34
  # 4. Code Review Function
35
  # --------------------------
36
- def review_code_with_openai(code):
37
- response = openai.ChatCompletion.create(
38
- model="gpt-4o-mini", # You can change model (gpt-4o, gpt-4o-mini)
39
- messages=[
40
- {"role": "system", "content": "You are a Python code reviewer. Identify bugs and suggest fixes."},
41
- {"role": "user", "content": f"Review this Python code:\n{code}"}
42
- ],
43
- temperature=0.3,
44
- )
45
- return response["choices"][0]["message"]["content"]
46
-
47
  def review_code_with_gemini(code):
48
- model = genai.GenerativeModel("gemini-2.5-pro")
49
  response = model.generate_content(
50
- f"Review this Python code, detect bugs, and suggest fixes with corrected code:\n{code}"
 
 
 
 
 
 
 
 
 
 
51
  )
52
  return response.text
53
 
@@ -57,16 +49,11 @@ def review_code_with_gemini(code):
57
  if st.button("πŸ” Review Code"):
58
  if not user_code.strip():
59
  st.warning("⚠️ Please enter some Python code first.")
60
- elif not api_key:
61
- st.error("❌ Please enter your API key in the sidebar.")
62
  else:
63
- with st.spinner("Analyzing your code..."):
64
  try:
65
- if api_choice == "OpenAI":
66
- feedback = review_code_with_openai(user_code)
67
- else:
68
- feedback = review_code_with_gemini(user_code)
69
-
70
  st.subheader("πŸ“’ Review Feedback & Fixes")
71
  st.write(feedback)
72
 
 
1
  import streamlit as st
 
2
  import google.generativeai as genai
3
 
4
  # --------------------------
5
  # 1. Page Configuration
6
  # --------------------------
7
+ st.set_page_config(page_title="AI-Powered Code Reviewer (Gemini)", layout="wide")
8
+ st.title("🐍 AI-Powered Python Code Reviewer (Gemini)")
9
 
10
  # --------------------------
11
+ # 2. Configure Gemini API from Secrets
12
  # --------------------------
13
+ if "GEMINI_API_KEY" in st.secrets:
14
+ api_key = st.secrets["GEMINI_API_KEY"]
15
+ genai.configure(api_key=api_key)
 
 
 
 
 
16
  else:
17
+ st.error("❌ Gemini API key not found! Please add it in Hugging Face Secrets.")
18
+ st.stop()
 
19
 
20
  # --------------------------
21
  # 3. User Code Input
 
26
  # --------------------------
27
  # 4. Code Review Function
28
  # --------------------------
 
 
 
 
 
 
 
 
 
 
 
29
  def review_code_with_gemini(code):
30
+ model = genai.GenerativeModel("gemini-1.5-pro") # βœ… Gemini 1.5 Pro
31
  response = model.generate_content(
32
+ f"""
33
+ You are an expert Python code reviewer.
34
+ Analyze the following code for:
35
+ - Bugs
36
+ - Logical errors
37
+ - Improvements in readability and efficiency
38
+ - Provide corrected code if necessary
39
+
40
+ Code:
41
+ {code}
42
+ """
43
  )
44
  return response.text
45
 
 
49
  if st.button("πŸ” Review Code"):
50
  if not user_code.strip():
51
  st.warning("⚠️ Please enter some Python code first.")
 
 
52
  else:
53
+ with st.spinner("Analyzing your code with Gemini..."):
54
  try:
55
+ feedback = review_code_with_gemini(user_code)
56
+
 
 
 
57
  st.subheader("πŸ“’ Review Feedback & Fixes")
58
  st.write(feedback)
59