DasariHarshitha commited on
Commit
bf6404a
Β·
verified Β·
1 Parent(s): d4c15b8

Update app.py

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