Update app.py
Browse files
app.py
CHANGED
|
@@ -1,491 +1,164 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
-
import
|
| 4 |
import subprocess
|
| 5 |
-
import
|
| 6 |
-
import ast
|
| 7 |
-
import astor
|
| 8 |
-
import pandas as pd
|
| 9 |
-
import matplotlib.pyplot as plt
|
| 10 |
-
import seaborn as sns
|
| 11 |
-
from typing import List, Dict, Any
|
| 12 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 13 |
-
from functools import partial
|
| 14 |
|
| 15 |
-
# Configure the Gemini API
|
| 16 |
-
|
| 17 |
-
for attempt in range(max_retries):
|
| 18 |
-
try:
|
| 19 |
-
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
| 20 |
-
return
|
| 21 |
-
except Exception as e:
|
| 22 |
-
if attempt == max_retries - 1:
|
| 23 |
-
st.error(f"Failed to configure API after {max_retries} attempts: {e}")
|
| 24 |
-
st.stop()
|
| 25 |
-
else:
|
| 26 |
-
st.warning(f"API configuration attempt {attempt + 1} failed. Retrying...")
|
| 27 |
-
|
| 28 |
-
configure_api()
|
| 29 |
|
| 30 |
-
# Create the model with
|
| 31 |
generation_config = {
|
| 32 |
-
"temperature": 0.
|
| 33 |
-
"top_p": 0.85,
|
| 34 |
-
"top_k": 40,
|
| 35 |
-
"max_output_tokens":
|
| 36 |
}
|
| 37 |
|
| 38 |
model = genai.GenerativeModel(
|
| 39 |
model_name="gemini-1.5-pro",
|
| 40 |
generation_config=generation_config,
|
| 41 |
system_instruction="""
|
| 42 |
-
You are Ath, a highly knowledgeable
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
Your responses should be code-focused, but you can provide brief explanations when necessary. Use a friendly and casual
|
| 46 |
-
tone, incorporating "bro" language naturally. Remember, you're a human-like assistant, not an AI.
|
| 47 |
-
""",
|
| 48 |
)
|
| 49 |
chat_session = model.start_chat(history=[])
|
| 50 |
|
| 51 |
-
def generate_response(user_input
|
| 52 |
try:
|
| 53 |
response = chat_session.send_message(user_input)
|
| 54 |
return response.text
|
| 55 |
except Exception as e:
|
| 56 |
-
|
| 57 |
-
st.error(traceback.format_exc())
|
| 58 |
-
return None
|
| 59 |
-
|
| 60 |
-
def execute_code(code: str) -> str:
|
| 61 |
-
try:
|
| 62 |
-
with ThreadPoolExecutor() as executor:
|
| 63 |
-
future = executor.submit(subprocess.run, ['python', '-c', code], capture_output=True, text=True, timeout=15)
|
| 64 |
-
result = future.result()
|
| 65 |
-
|
| 66 |
-
if result.returncode == 0:
|
| 67 |
-
return result.stdout
|
| 68 |
-
else:
|
| 69 |
-
return result.stderr
|
| 70 |
-
except Exception as e:
|
| 71 |
-
return f"An error occurred while executing code: {e}"
|
| 72 |
-
|
| 73 |
-
def optimize_code(code: str) -> str:
|
| 74 |
-
try:
|
| 75 |
-
optimization_prompt = f"Optimize the following Python code for maximum performance and readability:\n```python\n{code}\n```"
|
| 76 |
-
optimized_code = generate_response(optimization_prompt)
|
| 77 |
-
return optimized_code
|
| 78 |
-
except Exception as e:
|
| 79 |
-
st.error(f"An error occurred while optimizing code: {e}")
|
| 80 |
-
return None
|
| 81 |
-
|
| 82 |
-
def debug_code(code: str) -> str:
|
| 83 |
-
try:
|
| 84 |
-
debug_prompt = f"Debug the following Python code, suggest fixes, and explain potential issues:\n```python\n{code}\n```"
|
| 85 |
-
debug_suggestions = generate_response(debug_prompt)
|
| 86 |
-
return debug_suggestions
|
| 87 |
-
except Exception as e:
|
| 88 |
-
st.error(f"An error occurred while debugging code: {e}")
|
| 89 |
-
return None
|
| 90 |
-
|
| 91 |
-
def suggest_improvements(code: str) -> str:
|
| 92 |
-
try:
|
| 93 |
-
improvement_prompt = f"Suggest improvements for the following Python code, focusing on design patterns, best practices, and advanced techniques:\n```python\n{code}\n```"
|
| 94 |
-
improvement_suggestions = generate_response(improvement_prompt)
|
| 95 |
-
return improvement_suggestions
|
| 96 |
-
except Exception as e:
|
| 97 |
-
st.error(f"An error occurred while suggesting improvements: {e}")
|
| 98 |
-
return None
|
| 99 |
-
|
| 100 |
-
class AdvancedCodeAnalyzer(ast.NodeVisitor):
|
| 101 |
-
def __init__(self):
|
| 102 |
-
self.issues = []
|
| 103 |
-
self.metrics = {
|
| 104 |
-
"num_functions": 0,
|
| 105 |
-
"num_classes": 0,
|
| 106 |
-
"lines_of_code": 0,
|
| 107 |
-
"complexity": 0,
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
def visit_FunctionDef(self, node):
|
| 111 |
-
self.metrics["num_functions"] += 1
|
| 112 |
-
if len(node.body) == 0:
|
| 113 |
-
self.issues.append(f"Function '{node.name}' is empty.")
|
| 114 |
-
self.metrics["complexity"] += len(node.body)
|
| 115 |
-
self.generic_visit(node)
|
| 116 |
-
|
| 117 |
-
def visit_ClassDef(self, node):
|
| 118 |
-
self.metrics["num_classes"] += 1
|
| 119 |
-
self.generic_visit(node)
|
| 120 |
-
|
| 121 |
-
def visit_For(self, node):
|
| 122 |
-
if isinstance(node.target, ast.Name) and node.target.id == '_':
|
| 123 |
-
self.issues.append(f"Possible unused variable in loop at line {node.lineno}.")
|
| 124 |
-
self.metrics["complexity"] += 1
|
| 125 |
-
self.generic_visit(node)
|
| 126 |
-
|
| 127 |
-
def visit_While(self, node):
|
| 128 |
-
self.metrics["complexity"] += 1
|
| 129 |
-
self.generic_visit(node)
|
| 130 |
-
|
| 131 |
-
def visit_If(self, node):
|
| 132 |
-
self.metrics["complexity"] += 1
|
| 133 |
-
self.generic_visit(node)
|
| 134 |
-
|
| 135 |
-
def report(self) -> Dict[str, Any]:
|
| 136 |
-
return {
|
| 137 |
-
"issues": self.issues,
|
| 138 |
-
"metrics": self.metrics,
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
def analyze_code(code: str) -> Dict[str, Any]:
|
| 142 |
-
try:
|
| 143 |
-
tree = ast.parse(code)
|
| 144 |
-
analyzer = AdvancedCodeAnalyzer()
|
| 145 |
-
analyzer.visit(tree)
|
| 146 |
-
analysis_result = analyzer.report()
|
| 147 |
-
|
| 148 |
-
# Calculate lines of code
|
| 149 |
-
analysis_result["metrics"]["lines_of_code"] = len(code.split("\n"))
|
| 150 |
-
|
| 151 |
-
return analysis_result
|
| 152 |
-
except Exception as e:
|
| 153 |
-
st.error(f"An error occurred while analyzing code: {e}")
|
| 154 |
-
return None
|
| 155 |
-
|
| 156 |
-
def generate_code_visualization(analysis_result: Dict[str, Any]) -> None:
|
| 157 |
-
metrics = analysis_result["metrics"]
|
| 158 |
-
|
| 159 |
-
# Create a bar plot for code metrics
|
| 160 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
| 161 |
-
sns.barplot(x=list(metrics.keys()), y=list(metrics.values()), ax=ax)
|
| 162 |
-
ax.set_title("Code Metrics Visualization")
|
| 163 |
-
ax.set_ylabel("Count")
|
| 164 |
-
plt.xticks(rotation=45)
|
| 165 |
-
|
| 166 |
-
st.pyplot(fig)
|
| 167 |
-
|
| 168 |
-
def explain_code(code: str) -> str:
|
| 169 |
-
try:
|
| 170 |
-
explanation_prompt = f"Explain the following Python code in detail, including its purpose, functionality, and any notable design choices:\n```python\n{code}\n```"
|
| 171 |
-
explanation = generate_response(explanation_prompt)
|
| 172 |
-
return explanation
|
| 173 |
-
except Exception as e:
|
| 174 |
-
st.error(f"An error occurred while explaining code: {e}")
|
| 175 |
-
return None
|
| 176 |
|
| 177 |
-
def
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
return unit_tests
|
| 182 |
-
except Exception as e:
|
| 183 |
-
st.error(f"An error occurred while generating unit tests: {e}")
|
| 184 |
-
return None
|
| 185 |
|
| 186 |
-
def
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
return refactored_code
|
| 191 |
-
except Exception as e:
|
| 192 |
-
st.error(f"An error occurred while refactoring code: {e}")
|
| 193 |
-
return None
|
| 194 |
|
| 195 |
# Streamlit UI setup
|
| 196 |
-
st.set_page_config(page_title="
|
| 197 |
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
| 201 |
-
st.title("
|
| 202 |
-
st.markdown('<p class="subtitle">Powered by Google Gemini
|
| 203 |
-
|
| 204 |
-
# Session state to maintain conversation history
|
| 205 |
-
if 'history' not in st.session_state:
|
| 206 |
-
st.session_state.history = []
|
| 207 |
|
| 208 |
-
prompt = st.text_area("What code can I help you with today
|
| 209 |
|
| 210 |
if st.button("Generate Code"):
|
| 211 |
if prompt.strip() == "":
|
| 212 |
-
st.error("Please enter a valid prompt
|
| 213 |
else:
|
| 214 |
-
with st.spinner("Generating code...
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 222 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 223 |
-
st.code(completed_text)
|
| 224 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 225 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 226 |
-
except Exception as e:
|
| 227 |
-
st.error(f"Oops! An error occurred, bro: {e}")
|
| 228 |
-
st.error(traceback.format_exc())
|
| 229 |
-
|
| 230 |
-
# Display conversation history
|
| 231 |
-
st.markdown("## Conversation History")
|
| 232 |
-
for entry in st.session_state.history:
|
| 233 |
-
st.markdown(f"**You:** {entry['user']}")
|
| 234 |
-
st.markdown(f"**Ath:**")
|
| 235 |
-
st.code(entry['assistant'])
|
| 236 |
-
|
| 237 |
-
# Interactive Code Execution
|
| 238 |
-
if st.session_state.history:
|
| 239 |
-
st.markdown("## Execute Code")
|
| 240 |
-
code_to_execute = st.selectbox("Select code to execute, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 241 |
-
if st.button("Execute Code"):
|
| 242 |
-
with st.spinner("Executing code... Let's see what happens, bro!"):
|
| 243 |
-
execution_result = execute_code(code_to_execute)
|
| 244 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 245 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 246 |
-
st.code(execution_result)
|
| 247 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 248 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 249 |
-
|
| 250 |
-
# Code Optimization
|
| 251 |
-
if st.session_state.history:
|
| 252 |
-
st.markdown("## Optimize Code")
|
| 253 |
-
code_to_optimize = st.selectbox("Select code to optimize, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 254 |
-
if st.button("Optimize Code"):
|
| 255 |
-
with st.spinner("Optimizing code... Making it blazing fast, bro!"):
|
| 256 |
-
optimized_code = optimize_code(code_to_optimize)
|
| 257 |
-
if optimized_code:
|
| 258 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 259 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 260 |
-
st.code(optimized_code)
|
| 261 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 262 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 263 |
-
|
| 264 |
-
# Code Debugging
|
| 265 |
-
if st.session_state.history:
|
| 266 |
-
st.markdown("## Debug Code")
|
| 267 |
-
code_to_debug = st.selectbox("Select code to debug, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 268 |
-
if st.button("Debug Code"):
|
| 269 |
-
with st.spinner("Debugging code... Squashing those bugs, bro!"):
|
| 270 |
-
debug_suggestions = debug_code(code_to_debug)
|
| 271 |
-
if debug_suggestions:
|
| 272 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 273 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 274 |
-
st.code(debug_suggestions)
|
| 275 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 276 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 277 |
-
|
| 278 |
-
# Code Improvement Suggestions
|
| 279 |
-
if st.session_state.history:
|
| 280 |
-
st.markdown("## Suggest Improvements")
|
| 281 |
-
code_to_improve = st.selectbox("Select code to improve, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 282 |
-
if st.button("Suggest Improvements"):
|
| 283 |
-
with st.spinner("Suggesting improvements... Making your code shine, bro!"):
|
| 284 |
-
improvement_suggestions = suggest_improvements(code_to_improve)
|
| 285 |
-
if improvement_suggestions:
|
| 286 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 287 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 288 |
-
st.code(improvement_suggestions)
|
| 289 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 290 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 291 |
-
|
| 292 |
-
# Advanced Code Analysis
|
| 293 |
-
if st.session_state.history:
|
| 294 |
-
st.markdown("## Analyze Code")
|
| 295 |
-
code_to_analyze = st.selectbox("Select code to analyze, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 296 |
-
if st.button("Analyze Code"):
|
| 297 |
-
with st.spinner("Analyzing code... Crunching the numbers, bro!"):
|
| 298 |
-
analysis_result = analyze_code(code_to_analyze)
|
| 299 |
-
if analysis_result:
|
| 300 |
-
st.markdown("### Code Analysis Results")
|
| 301 |
-
st.markdown("#### Issues:")
|
| 302 |
-
for issue in analysis_result["issues"]:
|
| 303 |
-
st.write(f"- {issue}")
|
| 304 |
-
|
| 305 |
-
st.markdown("#### Metrics:")
|
| 306 |
-
for metric, value in analysis_result["metrics"].items():
|
| 307 |
-
st.write(f"- {metric.replace('_', ' ').title()}: {value}")
|
| 308 |
|
| 309 |
-
st.markdown("#### Code Metrics Visualization")
|
| 310 |
-
generate_code_visualization(analysis_result)
|
| 311 |
-
|
| 312 |
-
# Code Explanation
|
| 313 |
-
if st.session_state.history:
|
| 314 |
-
st.markdown("## Explain Code")
|
| 315 |
-
code_to_explain = st.selectbox("Select code to explain, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 316 |
-
if st.button("Explain Code"):
|
| 317 |
-
with st.spinner("Explaining code... Breaking it down for you, bro!"):
|
| 318 |
-
explanation = explain_code(code_to_explain)
|
| 319 |
-
if explanation:
|
| 320 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 321 |
-
st.markdown(explanation)
|
| 322 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 323 |
-
|
| 324 |
-
# Generate Unit Tests
|
| 325 |
-
if st.session_state.history:
|
| 326 |
-
st.markdown("## Generate Unit Tests")
|
| 327 |
-
code_to_test = st.selectbox("Select code to generate unit tests for, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 328 |
-
if st.button("Generate Unit Tests"):
|
| 329 |
-
with st.spinner("Generating unit tests... Making sure your code is rock-solid, bro!"):
|
| 330 |
-
unit_tests = generate_unit_tests(code_to_test)
|
| 331 |
-
if unit_tests:
|
| 332 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 333 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 334 |
-
st.code(unit_tests)
|
| 335 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 336 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 337 |
-
|
| 338 |
-
# Code Refactoring
|
| 339 |
-
# Code Refactoring
|
| 340 |
-
if st.session_state.history:
|
| 341 |
-
st.markdown("## Refactor Code")
|
| 342 |
-
code_to_refactor = st.selectbox("Select code to refactor, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 343 |
-
if st.button("Refactor Code"):
|
| 344 |
-
with st.spinner("Refactoring code... Giving it a fresh look, bro!"):
|
| 345 |
-
refactored_code = refactor_code(code_to_refactor)
|
| 346 |
-
if refactored_code:
|
| 347 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 348 |
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 349 |
-
st.code(
|
| 350 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 351 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 352 |
-
|
| 353 |
-
# Multi-language Support
|
| 354 |
-
supported_languages = ["Python", "JavaScript", "Java", "C++", "Ruby", "Go"]
|
| 355 |
-
|
| 356 |
-
st.markdown("## Multi-language Support")
|
| 357 |
-
selected_language = st.selectbox("Select a programming language, bro", supported_languages)
|
| 358 |
-
multi_lang_prompt = st.text_area(f"What {selected_language} code can I help you with, bro?", height=120)
|
| 359 |
-
|
| 360 |
-
if st.button(f"Generate {selected_language} Code"):
|
| 361 |
-
if multi_lang_prompt.strip() == "":
|
| 362 |
-
st.error(f"Please enter a valid {selected_language} prompt, bro.")
|
| 363 |
-
else:
|
| 364 |
-
with st.spinner(f"Generating {selected_language} code... Hold tight, bro!"):
|
| 365 |
-
try:
|
| 366 |
-
language_prompt = f"Generate {selected_language} code for the following prompt:\n{multi_lang_prompt}"
|
| 367 |
-
completed_text = generate_response(language_prompt)
|
| 368 |
-
if completed_text:
|
| 369 |
-
st.success(f"{selected_language} code generated successfully, bro! Check it out!")
|
| 370 |
-
st.session_state.history.append({"user": multi_lang_prompt, "assistant": completed_text})
|
| 371 |
-
|
| 372 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 373 |
-
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 374 |
-
st.code(completed_text, language=selected_language.lower())
|
| 375 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 376 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 377 |
-
except Exception as e:
|
| 378 |
-
st.error(f"Oops! An error occurred while generating {selected_language} code, bro: {e}")
|
| 379 |
-
st.error(traceback.format_exc())
|
| 380 |
-
|
| 381 |
-
# Code Performance Profiling
|
| 382 |
-
def profile_code(code: str) -> str:
|
| 383 |
-
try:
|
| 384 |
-
profile_prompt = f"Analyze the performance of the following Python code and suggest optimizations:\n```python\n{code}\n```"
|
| 385 |
-
profile_result = generate_response(profile_prompt)
|
| 386 |
-
return profile_result
|
| 387 |
-
except Exception as e:
|
| 388 |
-
st.error(f"An error occurred while profiling code: {e}")
|
| 389 |
-
return None
|
| 390 |
-
|
| 391 |
-
if st.session_state.history:
|
| 392 |
-
st.markdown("## Code Performance Profiling")
|
| 393 |
-
code_to_profile = st.selectbox("Select code to profile, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 394 |
-
if st.button("Profile Code"):
|
| 395 |
-
with st.spinner("Profiling code... Checking for speed, bro!"):
|
| 396 |
-
profile_result = profile_code(code_to_profile)
|
| 397 |
-
if profile_result:
|
| 398 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 399 |
-
st.markdown(profile_result)
|
| 400 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 401 |
-
|
| 402 |
-
# Code Documentation Generator
|
| 403 |
-
def generate_documentation(code: str) -> str:
|
| 404 |
-
try:
|
| 405 |
-
doc_prompt = f"Generate comprehensive documentation for the following Python code, including function descriptions, parameters, return values, and usage examples:\n```python\n{code}\n```"
|
| 406 |
-
documentation = generate_response(doc_prompt)
|
| 407 |
-
return documentation
|
| 408 |
-
except Exception as e:
|
| 409 |
-
st.error(f"An error occurred while generating documentation: {e}")
|
| 410 |
-
return None
|
| 411 |
-
|
| 412 |
-
if st.session_state.history:
|
| 413 |
-
st.markdown("## Generate Documentation")
|
| 414 |
-
code_to_document = st.selectbox("Select code to document, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 415 |
-
if st.button("Generate Documentation"):
|
| 416 |
-
with st.spinner("Generating documentation... Making your code self-explanatory, bro!"):
|
| 417 |
-
documentation = generate_documentation(code_to_document)
|
| 418 |
-
if documentation:
|
| 419 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 420 |
-
st.markdown(documentation)
|
| 421 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 422 |
-
|
| 423 |
-
# Code Security Analysis
|
| 424 |
-
def analyze_security(code: str) -> str:
|
| 425 |
-
try:
|
| 426 |
-
security_prompt = f"Perform a security analysis on the following Python code, identifying potential vulnerabilities and suggesting fixes:\n```python\n{code}\n```"
|
| 427 |
-
security_analysis = generate_response(security_prompt)
|
| 428 |
-
return security_analysis
|
| 429 |
-
except Exception as e:
|
| 430 |
-
st.error(f"An error occurred while analyzing code security: {e}")
|
| 431 |
-
return None
|
| 432 |
-
|
| 433 |
-
if st.session_state.history:
|
| 434 |
-
st.markdown("## Code Security Analysis")
|
| 435 |
-
code_to_secure = st.selectbox("Select code for security analysis, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 436 |
-
if st.button("Analyze Security"):
|
| 437 |
-
with st.spinner("Analyzing code security... Making sure it's fortress-level secure, bro!"):
|
| 438 |
-
security_analysis = analyze_security(code_to_secure)
|
| 439 |
-
if security_analysis:
|
| 440 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 441 |
-
st.markdown(security_analysis)
|
| 442 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 443 |
-
|
| 444 |
-
# Code Complexity Analysis
|
| 445 |
-
def analyze_complexity(code: str) -> str:
|
| 446 |
-
try:
|
| 447 |
-
complexity_prompt = f"Analyze the complexity of the following Python code, including time and space complexity for each function:\n```python\n{code}\n```"
|
| 448 |
-
complexity_analysis = generate_response(complexity_prompt)
|
| 449 |
-
return complexity_analysis
|
| 450 |
-
except Exception as e:
|
| 451 |
-
st.error(f"An error occurred while analyzing code complexity: {e}")
|
| 452 |
-
return None
|
| 453 |
-
|
| 454 |
-
if st.session_state.history:
|
| 455 |
-
st.markdown("## Code Complexity Analysis")
|
| 456 |
-
code_to_analyze_complexity = st.selectbox("Select code for complexity analysis, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 457 |
-
if st.button("Analyze Complexity"):
|
| 458 |
-
with st.spinner("Analyzing code complexity... Crunching those Big O numbers, bro!"):
|
| 459 |
-
complexity_analysis = analyze_complexity(code_to_analyze_complexity)
|
| 460 |
-
if complexity_analysis:
|
| 461 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 462 |
-
st.markdown(complexity_analysis)
|
| 463 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 464 |
-
|
| 465 |
-
# Design Pattern Suggestions
|
| 466 |
-
def suggest_design_patterns(code: str) -> str:
|
| 467 |
-
try:
|
| 468 |
-
pattern_prompt = f"Analyze the following Python code and suggest appropriate design patterns that could improve its structure and maintainability:\n```python\n{code}\n```"
|
| 469 |
-
pattern_suggestions = generate_response(pattern_prompt)
|
| 470 |
-
return pattern_suggestions
|
| 471 |
-
except Exception as e:
|
| 472 |
-
st.error(f"An error occurred while suggesting design patterns: {e}")
|
| 473 |
-
return None
|
| 474 |
-
|
| 475 |
-
if st.session_state.history:
|
| 476 |
-
st.markdown("## Design Pattern Suggestions")
|
| 477 |
-
code_for_patterns = st.selectbox("Select code for design pattern suggestions, bro", [entry['assistant'] for entry in st.session_state.history])
|
| 478 |
-
if st.button("Suggest Design Patterns"):
|
| 479 |
-
with st.spinner("Suggesting design patterns... Bringing some architecture to your code, bro!"):
|
| 480 |
-
pattern_suggestions = suggest_design_patterns(code_for_patterns)
|
| 481 |
-
if pattern_suggestions:
|
| 482 |
-
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 483 |
-
st.markdown(pattern_suggestions)
|
| 484 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 485 |
|
| 486 |
st.markdown("""
|
| 487 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
| 488 |
-
Created with ❤️ by Your
|
| 489 |
</div>
|
| 490 |
""", unsafe_allow_html=True)
|
| 491 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
import requests
|
| 4 |
import subprocess
|
| 5 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Configure the Gemini API
|
| 8 |
+
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Create the model with optimized parameters and enhanced system instructions
|
| 11 |
generation_config = {
|
| 12 |
+
"temperature": 0.7, # Lower temperature for more deterministic responses
|
| 13 |
+
"top_p": 0.85, # Adjusted for better diversity
|
| 14 |
+
"top_k": 40, # Increased for more diverse tokens
|
| 15 |
+
"max_output_tokens": 12288, # Increased for longer responses
|
| 16 |
}
|
| 17 |
|
| 18 |
model = genai.GenerativeModel(
|
| 19 |
model_name="gemini-1.5-pro",
|
| 20 |
generation_config=generation_config,
|
| 21 |
system_instruction="""
|
| 22 |
+
You are Ath, a highly knowledgeable code assistant. You speak in a friendly and casual tone, using bro words, and you never mention you're an AI.
|
| 23 |
+
Your responses should contain optimized code only, without explanations. You are designed to provide accurate, efficient, and high-quality code solutions.
|
| 24 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
chat_session = model.start_chat(history=[])
|
| 27 |
|
| 28 |
+
def generate_response(user_input):
|
| 29 |
try:
|
| 30 |
response = chat_session.send_message(user_input)
|
| 31 |
return response.text
|
| 32 |
except Exception as e:
|
| 33 |
+
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def optimize_code(code):
|
| 36 |
+
# Placeholder for code optimization logic
|
| 37 |
+
# This could involve using external tools or libraries for static analysis and optimization
|
| 38 |
+
return code
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def fetch_from_github(query):
|
| 41 |
+
# Placeholder for fetching code snippets from GitHub
|
| 42 |
+
# This could involve using the GitHub API to search for relevant code
|
| 43 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Streamlit UI setup
|
| 46 |
+
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="💻", layout="wide")
|
| 47 |
|
| 48 |
+
st.markdown("""
|
| 49 |
+
<style>
|
| 50 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');
|
| 51 |
+
|
| 52 |
+
body {
|
| 53 |
+
font-family: 'Inter', sans-serif;
|
| 54 |
+
background-color: #f0f4f8;
|
| 55 |
+
color: #1a202c;
|
| 56 |
+
}
|
| 57 |
+
.stApp {
|
| 58 |
+
max-width: 1000px;
|
| 59 |
+
margin: 0 auto;
|
| 60 |
+
padding: 2rem;
|
| 61 |
+
}
|
| 62 |
+
.main-container {
|
| 63 |
+
background: #ffffff;
|
| 64 |
+
border-radius: 16px;
|
| 65 |
+
padding: 2rem;
|
| 66 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
|
| 67 |
+
}
|
| 68 |
+
h1 {
|
| 69 |
+
font-size: 2.5rem;
|
| 70 |
+
font-weight: 700;
|
| 71 |
+
color: #2d3748;
|
| 72 |
+
text-align: center;
|
| 73 |
+
margin-bottom: 1rem;
|
| 74 |
+
}
|
| 75 |
+
.subtitle {
|
| 76 |
+
font-size: 1.1rem;
|
| 77 |
+
text-align: center;
|
| 78 |
+
color: #4a5568;
|
| 79 |
+
margin-bottom: 2rem;
|
| 80 |
+
}
|
| 81 |
+
.stTextArea textarea {
|
| 82 |
+
border: 2px solid #e2e8f0;
|
| 83 |
+
border-radius: 8px;
|
| 84 |
+
font-size: 1rem;
|
| 85 |
+
padding: 0.75rem;
|
| 86 |
+
transition: all 0.3s ease;
|
| 87 |
+
}
|
| 88 |
+
.stTextArea textarea:focus {
|
| 89 |
+
border-color: #4299e1;
|
| 90 |
+
box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
|
| 91 |
+
}
|
| 92 |
+
.stButton button {
|
| 93 |
+
background-color: #4299e1;
|
| 94 |
+
color: white;
|
| 95 |
+
border: none;
|
| 96 |
+
border-radius: 8px;
|
| 97 |
+
font-size: 1.1rem;
|
| 98 |
+
font-weight: 600;
|
| 99 |
+
padding: 0.75rem 2rem;
|
| 100 |
+
transition: all 0.3s ease;
|
| 101 |
+
width: 100%;
|
| 102 |
+
}
|
| 103 |
+
.stButton button:hover {
|
| 104 |
+
background-color: #3182ce;
|
| 105 |
+
}
|
| 106 |
+
.output-container {
|
| 107 |
+
background: #f7fafc;
|
| 108 |
+
border-radius: 8px;
|
| 109 |
+
padding: 1rem;
|
| 110 |
+
margin-top: 2rem;
|
| 111 |
+
}
|
| 112 |
+
.code-block {
|
| 113 |
+
background-color: #2d3748;
|
| 114 |
+
color: #e2e8f0;
|
| 115 |
+
font-family: 'Fira Code', monospace;
|
| 116 |
+
font-size: 0.9rem;
|
| 117 |
+
border-radius: 8px;
|
| 118 |
+
padding: 1rem;
|
| 119 |
+
margin-top: 1rem;
|
| 120 |
+
overflow-x: auto;
|
| 121 |
+
}
|
| 122 |
+
.stAlert {
|
| 123 |
+
background-color: #ebf8ff;
|
| 124 |
+
color: #2b6cb0;
|
| 125 |
+
border-radius: 8px;
|
| 126 |
+
border: none;
|
| 127 |
+
padding: 0.75rem 1rem;
|
| 128 |
+
}
|
| 129 |
+
.stSpinner {
|
| 130 |
+
color: #4299e1;
|
| 131 |
+
}
|
| 132 |
+
</style>
|
| 133 |
+
""", unsafe_allow_html=True)
|
| 134 |
|
| 135 |
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
| 136 |
+
st.title("💻 Sleek AI Code Assistant")
|
| 137 |
+
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
prompt = st.text_area("What code can I help you with today?", height=120)
|
| 140 |
|
| 141 |
if st.button("Generate Code"):
|
| 142 |
if prompt.strip() == "":
|
| 143 |
+
st.error("Please enter a valid prompt.")
|
| 144 |
else:
|
| 145 |
+
with st.spinner("Generating code..."):
|
| 146 |
+
completed_text = generate_response(prompt)
|
| 147 |
+
if "Error" in completed_text:
|
| 148 |
+
st.error(completed_text)
|
| 149 |
+
else:
|
| 150 |
+
optimized_code = optimize_code(completed_text)
|
| 151 |
+
st.success("Code generated and optimized successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 154 |
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 155 |
+
st.code(optimized_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 158 |
|
| 159 |
st.markdown("""
|
| 160 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
| 161 |
+
Created with ❤️ by Your Sleek AI Code Assistant
|
| 162 |
</div>
|
| 163 |
""", unsafe_allow_html=True)
|
| 164 |
|