Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import traceback
|
| 4 |
import subprocess
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Configure the Gemini API with advanced error handling
|
| 7 |
try:
|
|
@@ -12,10 +15,10 @@ except Exception as e:
|
|
| 12 |
|
| 13 |
# Create the model with system instructions and advanced configuration
|
| 14 |
generation_config = {
|
| 15 |
-
"temperature": 0.
|
| 16 |
-
"top_p": 0.
|
| 17 |
-
"top_k":
|
| 18 |
-
"max_output_tokens":
|
| 19 |
}
|
| 20 |
|
| 21 |
model = genai.GenerativeModel(
|
|
@@ -44,6 +47,60 @@ def execute_code(code):
|
|
| 44 |
except Exception as e:
|
| 45 |
return f"An error occurred while executing code: {e}"
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Streamlit UI setup
|
| 48 |
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="🚀", layout="wide")
|
| 49 |
|
|
@@ -184,6 +241,62 @@ if st.session_state.history:
|
|
| 184 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 185 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
st.markdown("""
|
| 188 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
| 189 |
Created with ❤️ by Your Sleek AI Code Assistant
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import traceback
|
| 4 |
import subprocess
|
| 5 |
+
import re
|
| 6 |
+
import ast
|
| 7 |
+
import astor
|
| 8 |
|
| 9 |
# Configure the Gemini API with advanced error handling
|
| 10 |
try:
|
|
|
|
| 15 |
|
| 16 |
# Create the model with system instructions and advanced configuration
|
| 17 |
generation_config = {
|
| 18 |
+
"temperature": 0.1, # Lower temperature for more deterministic responses
|
| 19 |
+
"top_p": 0.75,
|
| 20 |
+
"top_k": 20,
|
| 21 |
+
"max_output_tokens": 20480, # Increased max output tokens for longer responses
|
| 22 |
}
|
| 23 |
|
| 24 |
model = genai.GenerativeModel(
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
return f"An error occurred while executing code: {e}"
|
| 49 |
|
| 50 |
+
def optimize_code(code):
|
| 51 |
+
try:
|
| 52 |
+
optimization_prompt = f"Optimize the following Python code:\n```python\n{code}\n```"
|
| 53 |
+
optimized_code = generate_response(optimization_prompt)
|
| 54 |
+
return optimized_code
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"An error occurred while optimizing code: {e}")
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
def debug_code(code):
|
| 60 |
+
try:
|
| 61 |
+
debug_prompt = f"Debug the following Python code and suggest fixes:\n```python\n{code}\n```"
|
| 62 |
+
debug_suggestions = generate_response(debug_prompt)
|
| 63 |
+
return debug_suggestions
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(f"An error occurred while debugging code: {e}")
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
def suggest_improvements(code):
|
| 69 |
+
try:
|
| 70 |
+
improvement_prompt = f"Suggest improvements for the following Python code:\n```python\n{code}\n```"
|
| 71 |
+
improvement_suggestions = generate_response(improvement_prompt)
|
| 72 |
+
return improvement_suggestions
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.error(f"An error occurred while suggesting improvements: {e}")
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
def analyze_code(code):
|
| 78 |
+
try:
|
| 79 |
+
tree = ast.parse(code)
|
| 80 |
+
analyzer = CodeAnalyzer()
|
| 81 |
+
analyzer.visit(tree)
|
| 82 |
+
return analyzer.report()
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"An error occurred while analyzing code: {e}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
class CodeAnalyzer(ast.NodeVisitor):
|
| 88 |
+
def __init__(self):
|
| 89 |
+
self.issues = []
|
| 90 |
+
|
| 91 |
+
def visit_FunctionDef(self, node):
|
| 92 |
+
if len(node.body) == 0:
|
| 93 |
+
self.issues.append(f"Function '{node.name}' is empty.")
|
| 94 |
+
self.generic_visit(node)
|
| 95 |
+
|
| 96 |
+
def visit_For(self, node):
|
| 97 |
+
if isinstance(node.target, ast.Name) and node.target.id == '_':
|
| 98 |
+
self.issues.append(f"Possible unused variable in loop at line {node.lineno}.")
|
| 99 |
+
self.generic_visit(node)
|
| 100 |
+
|
| 101 |
+
def report(self):
|
| 102 |
+
return "\n".join(self.issues)
|
| 103 |
+
|
| 104 |
# Streamlit UI setup
|
| 105 |
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="🚀", layout="wide")
|
| 106 |
|
|
|
|
| 241 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 242 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 243 |
|
| 244 |
+
# Code Optimization
|
| 245 |
+
if st.session_state.history:
|
| 246 |
+
st.markdown("## Optimize Code")
|
| 247 |
+
code_to_optimize = st.selectbox("Select code to optimize", [entry['assistant'] for entry in st.session_state.history])
|
| 248 |
+
if st.button("Optimize Code"):
|
| 249 |
+
with st.spinner("Optimizing code..."):
|
| 250 |
+
optimized_code = optimize_code(code_to_optimize)
|
| 251 |
+
if optimized_code:
|
| 252 |
+
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 253 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 254 |
+
st.code(optimized_code)
|
| 255 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 256 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 257 |
+
|
| 258 |
+
# Code Debugging
|
| 259 |
+
if st.session_state.history:
|
| 260 |
+
st.markdown("## Debug Code")
|
| 261 |
+
code_to_debug = st.selectbox("Select code to debug", [entry['assistant'] for entry in st.session_state.history])
|
| 262 |
+
if st.button("Debug Code"):
|
| 263 |
+
with st.spinner("Debugging code..."):
|
| 264 |
+
debug_suggestions = debug_code(code_to_debug)
|
| 265 |
+
if debug_suggestions:
|
| 266 |
+
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 267 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 268 |
+
st.code(debug_suggestions)
|
| 269 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 270 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 271 |
+
|
| 272 |
+
# Code Improvement Suggestions
|
| 273 |
+
if st.session_state.history:
|
| 274 |
+
st.markdown("## Suggest Improvements")
|
| 275 |
+
code_to_improve = st.selectbox("Select code to improve", [entry['assistant'] for entry in st.session_state.history])
|
| 276 |
+
if st.button("Suggest Improvements"):
|
| 277 |
+
with st.spinner("Suggesting improvements..."):
|
| 278 |
+
improvement_suggestions = suggest_improvements(code_to_improve)
|
| 279 |
+
if improvement_suggestions:
|
| 280 |
+
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 281 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 282 |
+
st.code(improvement_suggestions)
|
| 283 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 284 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 285 |
+
|
| 286 |
+
# Code Analysis
|
| 287 |
+
if st.session_state.history:
|
| 288 |
+
st.markdown("## Analyze Code")
|
| 289 |
+
code_to_analyze = st.selectbox("Select code to analyze", [entry['assistant'] for entry in st.session_state.history])
|
| 290 |
+
if st.button("Analyze Code"):
|
| 291 |
+
with st.spinner("Analyzing code..."):
|
| 292 |
+
analysis_result = analyze_code(code_to_analyze)
|
| 293 |
+
if analysis_result:
|
| 294 |
+
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 295 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 296 |
+
st.code(analysis_result)
|
| 297 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 298 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 299 |
+
|
| 300 |
st.markdown("""
|
| 301 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
| 302 |
Created with ❤️ by Your Sleek AI Code Assistant
|