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

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +68 -0
main.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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!")