kodamkarthik281 commited on
Commit
2774169
·
verified ·
1 Parent(s): ce2c8b7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +79 -68
main.py CHANGED
@@ -1,68 +1,79 @@
1
- import streamlit as st
2
- import google.generativeai as genai
3
-
4
- st.title("AI Code Reviewer ")
5
-
6
- try:
7
- with open("API_key.txt", "r") as f:
8
- api_key = f.read().strip()
9
- genai.configure(api_key=api_key)
10
- model = genai.GenerativeModel(model_name="gemini-2.0-flash-001")
11
-
12
- except Exception as e:
13
- st.error("API Key Error! Please check your API key file.")
14
- st.stop()
15
-
16
- # system prompt
17
- sys_prompt ='''
18
- You are an AI Code Reviewer. Your task is to analyze submitted code in any programming language and provide a structured review.
19
-
20
- If the code contains errors (syntax, logical, or runtime):
21
- 1. Identify and explain the errors concisely.
22
- 2. Provide a corrected version in a copyable format.
23
- 3. Ensure the logic is correct; if flawed, correct it with an explanation.
24
-
25
- If the code is correct but inefficient:
26
- - Suggest performance optimizations (e.g., time and space complexity improvements).
27
-
28
- If the code is correct and efficient:
29
- - Briefly explain what the code does in simple terms.
30
-
31
- Additional Considerations:
32
- - Ensure readability and best coding practices (e.g., meaningful variable names, comments, modularity).
33
- - If the user submits a non-programming question, respond humorously as a dedicated code reviewer.
34
-
35
- Keep responses concise, structured, and helpful.
36
-
37
- '''
38
-
39
- if "history" not in st.session_state:
40
- st.session_state.history = []
41
-
42
- user_query = st.text_area("Submit your code for review:", height=150, key="user_input",placeholder= 'code here')
43
-
44
- if st.button("Review"):
45
- with st.spinner("Reviewing your code... "):
46
- try:
47
- response = model.generate_content(f"{sys_prompt}\n\nUser: {user_query}")
48
- ai_response = response.text.strip() if response.text else " Error: No response received."
49
-
50
- st.session_state.history.append({"query": user_query, "response": ai_response})
51
-
52
- st.subheader("Review Result:")
53
- st.write(ai_response)
54
-
55
- except Exception as e:
56
- st.error(f"An error occurred: {e}")
57
-
58
- st.sidebar.title("Review History")
59
- if st.session_state.history:
60
- queries = [f"Query {i+1}" for i in range(len(st.session_state.history))]
61
- selected_query = st.sidebar.selectbox("Select a past query:", queries, index=len(queries)-1)
62
-
63
- if selected_query:
64
- idx = queries.index(selected_query)
65
- st.sidebar.subheader("Selected Review Result:")
66
- st.sidebar.write(st.session_state.history[idx]["response"])
67
- else:
68
- st.sidebar.write("No queries yet. Submit your code above!")
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+
4
+ st.title("🧠 AI Code Reviewer")
5
+
6
+ # Prompt user for API key (stored in session)
7
+ if "api_key" not in st.session_state:
8
+ st.session_state.api_key = ""
9
+
10
+ # Show input field if API key is not already entered
11
+ if not st.session_state.api_key:
12
+ st.session_state.api_key = st.text_input("Enter your Google Generative AI API Key", type="password")
13
+ if not st.session_state.api_key:
14
+ st.warning("Please enter your API key to continue.")
15
+ st.stop()
16
+
17
+ # Configure generative model
18
+ try:
19
+ genai.configure(api_key=st.session_state.api_key)
20
+ model = genai.GenerativeModel(model_name="gemini-1.5-flash")
21
+ except Exception as e:
22
+ st.error("Invalid API key or configuration error.")
23
+ st.stop()
24
+
25
+ # System prompt for AI behavior
26
+ sys_prompt = '''
27
+ You are an AI Code Reviewer. Your task is to analyze submitted code in any programming language and provide a structured review.
28
+
29
+ If the code contains errors (syntax, logical, or runtime):
30
+ 1. Identify and explain the errors concisely.
31
+ 2. Provide a corrected version in a copyable format.
32
+ 3. Ensure the logic is correct; if flawed, correct it with an explanation.
33
+
34
+ If the code is correct but inefficient:
35
+ - Suggest performance optimizations (e.g., time and space complexity improvements).
36
+
37
+ If the code is correct and efficient:
38
+ - Briefly explain what the code does in simple terms.
39
+
40
+ Additional Considerations:
41
+ - Ensure readability and best coding practices (e.g., meaningful variable names, comments, modularity).
42
+ - If the user submits a non-programming question, respond humorously as a dedicated code reviewer.
43
+
44
+ Keep responses concise, structured, and helpful.
45
+ '''
46
+
47
+ # Review history storage
48
+ if "history" not in st.session_state:
49
+ st.session_state.history = []
50
+
51
+ # Code input
52
+ user_query = st.text_area("Submit your code for review:", height=150, key="user_input", placeholder="Paste your code here...")
53
+
54
+ if st.button("Review"):
55
+ with st.spinner("Reviewing your code..."):
56
+ try:
57
+ response = model.generate_content(f"{sys_prompt}\n\nUser: {user_query}")
58
+ ai_response = response.text.strip() if response.text else "Error: No response received."
59
+
60
+ st.session_state.history.append({"query": user_query, "response": ai_response})
61
+
62
+ st.subheader("Review Result:")
63
+ st.write(ai_response)
64
+
65
+ except Exception as e:
66
+ st.error(f"An error occurred: {e}")
67
+
68
+ # Sidebar - Review History
69
+ st.sidebar.title("Review History")
70
+ if st.session_state.history:
71
+ queries = [f"Query {i+1}" for i in range(len(st.session_state.history))]
72
+ selected_query = st.sidebar.selectbox("Select a past query:", queries, index=len(queries)-1)
73
+
74
+ if selected_query:
75
+ idx = queries.index(selected_query)
76
+ st.sidebar.subheader("Selected Review Result:")
77
+ st.sidebar.write(st.session_state.history[idx]["response"])
78
+ else:
79
+ st.sidebar.write("No queries yet. Submit your code above!")