Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,453 +3,381 @@ import google.generativeai as genai
|
|
3 |
import zipfile
|
4 |
import io
|
5 |
import json
|
6 |
-
import os
|
7 |
from pathlib import Path
|
8 |
import time
|
9 |
|
10 |
# --- Configuration ---
|
11 |
-
|
12 |
-
MAX_PROMPT_TOKENS_ESTIMATE = 800000
|
13 |
-
RESULTS_PAGE_SIZE = 25
|
14 |
|
15 |
-
AVAILABLE_ANALYSES = {
|
16 |
-
# ... (keep the same)
|
17 |
"generate_docs": "Generate Missing Docstrings/Comments",
|
18 |
"find_bugs": "Identify Potential Bugs & Anti-patterns",
|
19 |
"check_style": "Check Style Guide Compliance (General)",
|
20 |
"summarize_modules": "Summarize Complex Modules/Files",
|
21 |
"suggest_refactoring": "Suggest Refactoring Opportunities"
|
22 |
}
|
23 |
-
CODE_EXTENSIONS = {'.py', '.js', '.java', '.c', '.cpp', '.h', '.cs', '.go', '.rb', '.php', '.swift', '.kt', '.ts', '.html', '.css', '.scss', '.sql'}
|
24 |
|
25 |
# --- Session State Initialization ---
|
|
|
26 |
if 'mock_api_call' not in st.session_state:
|
27 |
st.session_state.mock_api_call = False
|
28 |
if 'analysis_results' not in st.session_state:
|
29 |
-
st.session_state.analysis_results = None
|
30 |
if 'error_message' not in st.session_state:
|
31 |
st.session_state.error_message = None
|
32 |
if 'analysis_requested' not in st.session_state:
|
33 |
-
|
34 |
-
|
35 |
-
#
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def initialize_gemini_model():
|
39 |
-
"""Initializes the Gemini
|
40 |
global model
|
41 |
-
|
|
|
|
|
42 |
try:
|
43 |
if 'GEMINI_API_KEY' not in st.secrets:
|
44 |
st.error("π¨ Gemini API Key not found. Add it to `.streamlit/secrets.toml`.")
|
45 |
-
st.stop()
|
|
|
46 |
genai.configure(api_key=st.secrets["GEMINI_API_KEY"])
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
return True
|
50 |
except Exception as e:
|
51 |
-
st.error(f"π¨ Error initializing Gemini
|
|
|
52 |
st.stop()
|
53 |
return False
|
54 |
elif st.session_state.mock_api_call:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# --- Helper Functions ---
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
Roughly estimate token count (assuming ~3 characters per token).
|
66 |
-
If an integer is provided (i.e. a character count), simply divide by 3.
|
67 |
-
"""
|
68 |
-
if isinstance(text, int):
|
69 |
-
return text // 3
|
70 |
-
return len(text) // 3
|
71 |
-
|
72 |
-
# --- OPTIMIZATION: Cache ZIP processing ---
|
73 |
-
@st.cache_data(max_entries=5) # Cache results for recent uploads
|
74 |
def process_zip_file_cached(file_id, file_size, file_content_bytes):
|
75 |
-
|
76 |
-
code_files = {}
|
77 |
-
|
78 |
-
file_count = 0
|
79 |
-
ignored_files = []
|
80 |
-
status_placeholder = st.empty() # For progress bar
|
81 |
-
progress_bar = status_placeholder.progress(0)
|
82 |
-
|
83 |
try:
|
84 |
with zipfile.ZipFile(io.BytesIO(file_content_bytes), 'r') as zip_ref:
|
85 |
-
members = zip_ref.infolist()
|
86 |
-
total_members = len(members)
|
87 |
for i, member in enumerate(members):
|
88 |
-
|
89 |
-
if
|
90 |
-
progress_bar.progress(int((i / total_members) * 100))
|
91 |
-
|
92 |
-
if member.is_dir() or any(part.startswith('.') for part in Path(member.filename).parts) or '__' in member.filename:
|
93 |
-
continue
|
94 |
-
|
95 |
file_path = Path(member.filename)
|
96 |
if file_path.suffix.lower() in CODE_EXTENSIONS:
|
97 |
try:
|
98 |
with zip_ref.open(member) as file:
|
99 |
file_bytes = file.read()
|
100 |
-
try:
|
101 |
-
content = file_bytes.decode('utf-8')
|
102 |
except UnicodeDecodeError:
|
103 |
-
try:
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
continue
|
108 |
-
|
109 |
-
code_files[member.filename] = content
|
110 |
-
total_chars += len(content)
|
111 |
-
file_count += 1
|
112 |
-
except Exception as read_err:
|
113 |
-
ignored_files.append(f"{member.filename} (Read Error: {read_err})")
|
114 |
else:
|
115 |
-
if not (any(
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
status_placeholder.empty()
|
123 |
-
st.error("π¨ Invalid or corrupted ZIP file.")
|
124 |
-
return None, 0, 0, []
|
125 |
-
except Exception as e:
|
126 |
-
status_placeholder.empty()
|
127 |
-
st.error(f"π¨ Error processing ZIP file: {e}")
|
128 |
-
return None, 0, 0, []
|
129 |
-
|
130 |
-
if file_count == 0 and not ignored_files:
|
131 |
-
st.warning("No files with recognized code extensions found in the ZIP.")
|
132 |
-
elif file_count == 0 and ignored_files:
|
133 |
-
st.warning("No files with recognized code extensions found. Some files were skipped.")
|
134 |
-
|
135 |
-
print(f"Cache miss or new file: Processed ZIP {file_id}")
|
136 |
return code_files, total_chars, file_count, ignored_files
|
137 |
|
138 |
-
def construct_analysis_prompt(code_files_dict, requested_analyses):
|
139 |
-
"""Constructs the prompt for Gemini, including code content and JSON structure request."""
|
140 |
-
prompt_parts = ["Analyze the following codebase provided as a collection of file paths and their content.\n\n"]
|
141 |
-
current_token_estimate = estimate_token_count(prompt_parts[0])
|
142 |
-
included_files = []
|
143 |
-
code_segments = []
|
144 |
-
|
145 |
-
prompt_status = st.empty()
|
146 |
-
if len(code_files_dict) > 50:
|
147 |
-
prompt_status.write("Constructing prompt (processing files)...")
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
for filename, content in code_files_dict.items():
|
150 |
-
|
151 |
-
file_content = f"{content}\n"
|
152 |
-
file_end_marker = f"--- END FILE: {filename} ---\n\n"
|
153 |
-
segment = file_marker + file_content + file_end_marker
|
154 |
segment_token_estimate = estimate_token_count(segment)
|
155 |
-
|
156 |
if current_token_estimate + segment_token_estimate <= MAX_PROMPT_TOKENS_ESTIMATE:
|
157 |
-
code_segments.append(segment)
|
158 |
-
|
159 |
-
included_files.append(filename)
|
160 |
-
else:
|
161 |
-
st.warning(f"β οΈ Codebase may exceed context window estimate (~{MAX_PROMPT_TOKENS_ESTIMATE} tokens). Analysis performed only on the first {len(included_files)} files ({current_token_estimate:,} tokens).")
|
162 |
-
break
|
163 |
-
|
164 |
prompt_status.empty()
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
json_structure_description
|
174 |
-
|
175 |
-
if "generate_docs" in requested_analyses:
|
176 |
-
structure_parts.append(' "documentation_suggestions": [{"file": "path/to/file", "line": number, "suggestion": "Suggested docstring/comment"}]')
|
177 |
-
if "find_bugs" in requested_analyses:
|
178 |
-
structure_parts.append(' "potential_bugs": [{"file": "path/to/file", "line": number, "description": "Description of potential bug/anti-pattern", "severity": "High/Medium/Low"}]')
|
179 |
-
if "check_style" in requested_analyses:
|
180 |
-
structure_parts.append(' "style_issues": [{"file": "path/to/file", "line": number, "description": "Description of style deviation"}]')
|
181 |
-
if "summarize_modules" in requested_analyses:
|
182 |
-
structure_parts.append(' "module_summaries": [{"file": "path/to/file", "summary": "One-paragraph summary of the file purpose/functionality"}]')
|
183 |
-
if "suggest_refactoring" in requested_analyses:
|
184 |
-
structure_parts.append(' "refactoring_suggestions": [{"file": "path/to/file", "line": number, "area": "e.g., function name, class name", "suggestion": "Description of refactoring suggestion"}]')
|
185 |
-
|
186 |
-
json_structure_description += ",\n".join(structure_parts)
|
187 |
-
json_structure_description += "\n}"
|
188 |
-
|
189 |
-
prompt_footer = f"""
|
190 |
-
**Analysis Task:**
|
191 |
-
Perform the analyses corresponding to the keys present in the JSON structure below, based *only* on the provided code files ({', '.join(included_files)}).
|
192 |
-
|
193 |
-
**Output Format:**
|
194 |
-
Respond ONLY with a single, valid JSON object adhering strictly to the following structure. If no issues/suggestions are found for a category, provide an empty list `[]`. Do not include explanations outside the JSON structure.
|
195 |
-
|
196 |
-
{json_structure_description}
|
197 |
-
|
198 |
-
**JSON Output Only:**
|
199 |
-
"""
|
200 |
prompt_parts.append(prompt_footer)
|
201 |
full_prompt = "".join(prompt_parts)
|
202 |
return full_prompt, included_files
|
203 |
|
|
|
204 |
def call_gemini_api(prompt):
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
# MOCK MODE LOGIC
|
210 |
if st.session_state.mock_api_call:
|
211 |
-
st.info("MOCK MODE: Simulating API call...")
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
"documentation_suggestions": [{"file": "mock/core.py", "line": 15, "suggestion": "def process_data(data):\n \"\"\"Processes the input data using mock logic.\"\"\""}],
|
216 |
-
"potential_bugs": [{"file": "mock/utils.py", "line": 22, "description": "Potential division by zero if denominator is not checked.", "severity": "Medium"}],
|
217 |
-
"style_issues": [],
|
218 |
-
"module_summaries": [],
|
219 |
-
"refactoring_suggestions": []
|
220 |
-
})
|
221 |
-
st.success("Mock response generated.")
|
222 |
-
return json.loads(mock_json_response), None
|
223 |
-
|
224 |
-
# REAL API CALL LOGIC
|
225 |
else:
|
226 |
-
if not initialize_gemini_model():
|
227 |
-
|
228 |
-
if model is None:
|
229 |
-
return None, "Gemini model not available."
|
230 |
-
|
231 |
try:
|
232 |
api_status = st.empty()
|
233 |
-
|
234 |
-
api_status.info(f"π‘ Sending request to {
|
235 |
-
start_time = time.time()
|
236 |
-
|
237 |
-
|
238 |
-
generation_config=genai.types.GenerationConfig(temperature=0.2),
|
239 |
-
safety_settings=[
|
240 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
241 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
242 |
-
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
243 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
244 |
-
]
|
245 |
-
)
|
246 |
-
end_time = time.time()
|
247 |
-
api_status.success(f"β
Response received from AI in {end_time - start_time:.2f} seconds.")
|
248 |
-
time.sleep(1)
|
249 |
-
api_status.empty()
|
250 |
-
|
251 |
-
try:
|
252 |
json_response_text = response.text.strip()
|
253 |
-
|
254 |
-
|
255 |
-
if json_response_text.startswith("```"):
|
256 |
-
|
257 |
-
|
258 |
-
json_response_text = json_response_text[:-3]
|
259 |
-
json_start = json_response_text.find('{')
|
260 |
-
json_end = json_response_text.rfind('}') + 1
|
261 |
if json_start != -1 and json_end != -1 and json_end > json_start:
|
262 |
-
final_json_text = json_response_text[json_start:json_end]
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
return None, f"AI response was not valid JSON: {json_err}"
|
272 |
-
except AttributeError:
|
273 |
-
st.error("π¨ Unexpected API response structure (AttributeError).")
|
274 |
-
st.code(f"Response object: {response}", language='text')
|
275 |
-
try:
|
276 |
-
block_reason = response.prompt_feedback.block_reason
|
277 |
-
if block_reason:
|
278 |
-
return None, f"Content blocked by API. Reason: {block_reason}"
|
279 |
-
except Exception:
|
280 |
-
pass
|
281 |
-
return None, "Unexpected response structure from API (AttributeError)."
|
282 |
-
except Exception as e:
|
283 |
-
st.error(f"π¨ Unexpected issue processing AI response: {e}")
|
284 |
-
try:
|
285 |
-
st.code(f"Response object: {response}", language='text')
|
286 |
-
except Exception:
|
287 |
-
pass
|
288 |
-
return None, f"Unexpected response structure: {e}"
|
289 |
-
except Exception as e:
|
290 |
-
api_status.empty()
|
291 |
-
st.error(f"π¨ An error occurred during API call: {e}")
|
292 |
-
error_msg = f"API call failed: {e}"
|
293 |
if hasattr(e, 'message'):
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
error_msg = "Content blocked due to safety settings."
|
300 |
-
elif "block_reason: SAFETY" in str(e):
|
301 |
-
error_msg = "Content blocked due to safety settings."
|
302 |
return None, error_msg
|
303 |
|
|
|
304 |
def display_results(results_json, requested_analyses):
|
305 |
-
|
306 |
st.header("π Analysis Report")
|
307 |
-
if not isinstance(results_json, dict):
|
308 |
-
|
309 |
-
|
310 |
-
return
|
311 |
-
if "raw_response" in results_json:
|
312 |
-
st.subheader("Raw AI Response (JSON Parsing Failed)")
|
313 |
-
st.code(results_json["raw_response"], language='text')
|
314 |
-
return
|
315 |
-
|
316 |
-
display_config = {
|
317 |
"generate_docs": {"key": "documentation_suggestions", "title": AVAILABLE_ANALYSES["generate_docs"], "fields": {"file": "File", "line": "Line"}},
|
318 |
"find_bugs": {"key": "potential_bugs", "title": AVAILABLE_ANALYSES["find_bugs"], "fields": {"file": "File", "line": "Line", "severity": "Severity"}},
|
319 |
"check_style": {"key": "style_issues", "title": AVAILABLE_ANALYSES["check_style"], "fields": {"file": "File", "line": "Line"}},
|
320 |
"summarize_modules": {"key": "module_summaries", "title": AVAILABLE_ANALYSES["summarize_modules"], "fields": {"file": "File"}},
|
321 |
"suggest_refactoring": {"key": "refactoring_suggestions", "title": AVAILABLE_ANALYSES["suggest_refactoring"], "fields": {"file": "File", "line": "Line", "area": "Area"}}
|
322 |
}
|
323 |
-
|
324 |
any_results_found = False
|
325 |
for analysis_key in requested_analyses:
|
326 |
if analysis_key in display_config:
|
327 |
-
config = display_config[analysis_key]
|
328 |
-
|
329 |
-
total_items = len(items)
|
330 |
-
|
331 |
-
st.subheader(f"{config['title']} ({total_items} found)")
|
332 |
-
|
333 |
if items:
|
334 |
-
any_results_found = True
|
335 |
-
state_key =
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
visible_count = st.session_state[state_key]
|
340 |
-
items_to_display = items[:visible_count]
|
341 |
-
|
342 |
-
for item in items_to_display:
|
343 |
-
details = []
|
344 |
-
for field_key, field_label in config["fields"].items():
|
345 |
-
value = item.get(field_key, 'N/A')
|
346 |
-
if value != 'N/A':
|
347 |
-
details.append(f"**{field_label}:** `{value}`" if field_key == 'file' else f"**{field_label}:** {value}")
|
348 |
st.markdown("- " + " - ".join(details))
|
349 |
-
if 'suggestion' in item:
|
350 |
-
|
351 |
-
elif '
|
352 |
-
|
353 |
-
elif 'summary' in item:
|
354 |
-
st.markdown(f" > {item['summary']}")
|
355 |
-
|
356 |
-
if total_items > visible_count:
|
357 |
if st.button(f"Show more ({total_items - visible_count} remaining)", key=f"more_{analysis_key}"):
|
358 |
-
st.session_state[state_key] += RESULTS_PAGE_SIZE
|
359 |
-
|
360 |
-
else:
|
361 |
-
st.markdown("_No items found for this category._")
|
362 |
st.divider()
|
|
|
|
|
363 |
|
364 |
-
if not any_results_found:
|
365 |
-
st.info("No specific findings were identified in the analysis based on your selections.")
|
366 |
-
|
367 |
-
st.download_button(
|
368 |
-
label="Download Full Report (JSON)",
|
369 |
-
data=json.dumps(results_json, indent=4),
|
370 |
-
file_name="code_audit_report.json",
|
371 |
-
mime="application/json"
|
372 |
-
)
|
373 |
|
374 |
# --- Streamlit App Main Interface ---
|
375 |
st.set_page_config(page_title="Codebase Audit Assistant", layout="wide")
|
376 |
-
st.title("π€ Codebase Audit Assistant")
|
377 |
-
st.markdown(f"Upload codebase (`.zip`) for analysis via **{GEMINI_MODEL_NAME}**.")
|
378 |
|
|
|
379 |
with st.sidebar:
|
380 |
st.header("βοΈ Analysis Controls")
|
381 |
st.session_state.mock_api_call = st.toggle("π§ͺ Enable Mock API Mode", value=st.session_state.mock_api_call, help="Use fake data instead of calling Gemini API.")
|
382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
st.divider()
|
384 |
st.header("π Select Analyses")
|
385 |
selected_analyses = [key for key, name in AVAILABLE_ANALYSES.items() if st.checkbox(name, value=True, key=f"cb_{key}")]
|
386 |
st.divider()
|
387 |
-
st.header("π How To Use")
|
388 |
-
st.info("1. Set API Key
|
389 |
-
st.info(f"Note:
|
390 |
st.divider()
|
391 |
-
st.warning("β οΈ **Privacy:** Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
|
|
|
|
|
|
|
393 |
uploaded_file = st.file_uploader("π Upload Codebase ZIP File", type=['zip'], key="file_uploader",
|
394 |
on_change=lambda: st.session_state.update(analysis_results=None, error_message=None, analysis_requested=False))
|
395 |
-
|
396 |
-
|
397 |
-
results_placeholder = st.container() # Container for results display
|
398 |
|
399 |
if uploaded_file:
|
400 |
st.success(f"β
File '{uploaded_file.name}' uploaded.")
|
401 |
-
|
402 |
-
# Read file bytes once for caching
|
403 |
uploaded_file_bytes = uploaded_file.getvalue()
|
404 |
file_id = f"{uploaded_file.name}-{uploaded_file.size}"
|
405 |
-
|
406 |
code_files, total_chars, file_count, ignored_files = process_zip_file_cached(file_id, uploaded_file.size, uploaded_file_bytes)
|
407 |
-
|
408 |
if code_files is not None:
|
409 |
-
st.info(f"Found **{file_count}**
|
410 |
if ignored_files:
|
411 |
-
|
412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
413 |
|
414 |
-
analyze_button_disabled = (not selected_analyses or file_count == 0)
|
415 |
-
analyze_button_label = "Analyze Codebase" if not analyze_button_disabled else "Select Analyses or Upload Valid Code"
|
416 |
if analysis_button_placeholder.button(analyze_button_label, type="primary", disabled=analyze_button_disabled):
|
417 |
-
st.session_state.analysis_requested = True
|
418 |
-
st.
|
419 |
-
st.
|
420 |
-
|
421 |
-
if not selected_analyses:
|
422 |
-
st.warning("Please select analysis types.")
|
423 |
-
elif file_count == 0:
|
424 |
-
st.warning("No relevant code files found.")
|
425 |
else:
|
426 |
with results_placeholder:
|
427 |
-
|
|
|
|
|
428 |
analysis_prompt, included_files_in_prompt = construct_analysis_prompt(code_files, selected_analyses)
|
429 |
if analysis_prompt and included_files_in_prompt:
|
430 |
results_json, error_msg = call_gemini_api(analysis_prompt)
|
431 |
-
st.session_state.analysis_results = results_json
|
432 |
-
|
433 |
-
|
434 |
-
st.session_state.error_message = "Could not proceed: No files included (check token limits/errors)."
|
435 |
-
else:
|
436 |
-
st.session_state.error_message = "Failed to generate analysis prompt."
|
437 |
st.rerun()
|
438 |
|
|
|
439 |
if st.session_state.analysis_requested:
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
elif not uploaded_file:
|
452 |
-
results_placeholder.info("Upload a ZIP file containing your source code to begin.")
|
453 |
|
454 |
results_placeholder.divider()
|
455 |
-
results_placeholder.markdown("_Assistant powered by Google Gemini._")
|
|
|
3 |
import zipfile
|
4 |
import io
|
5 |
import json
|
6 |
+
import os # Still needed for API key potentially, but not model names
|
7 |
from pathlib import Path
|
8 |
import time
|
9 |
|
10 |
# --- Configuration ---
|
11 |
+
# Model names are now discovered dynamically. Remove hardcoded names.
|
12 |
+
MAX_PROMPT_TOKENS_ESTIMATE = 800000 # Keep this estimate
|
13 |
+
RESULTS_PAGE_SIZE = 25
|
14 |
|
15 |
+
AVAILABLE_ANALYSES = { # Keep analyses config
|
|
|
16 |
"generate_docs": "Generate Missing Docstrings/Comments",
|
17 |
"find_bugs": "Identify Potential Bugs & Anti-patterns",
|
18 |
"check_style": "Check Style Guide Compliance (General)",
|
19 |
"summarize_modules": "Summarize Complex Modules/Files",
|
20 |
"suggest_refactoring": "Suggest Refactoring Opportunities"
|
21 |
}
|
22 |
+
CODE_EXTENSIONS = {'.py', '.js', '.java', '.c', '.cpp', '.h', '.cs', '.go', '.rb', '.php', '.swift', '.kt', '.ts', '.html', '.css', '.scss', '.sql'} # Keep extensions
|
23 |
|
24 |
# --- Session State Initialization ---
|
25 |
+
# (Keep most session state, add one for the selected model)
|
26 |
if 'mock_api_call' not in st.session_state:
|
27 |
st.session_state.mock_api_call = False
|
28 |
if 'analysis_results' not in st.session_state:
|
29 |
+
st.session_state.analysis_results = None
|
30 |
if 'error_message' not in st.session_state:
|
31 |
st.session_state.error_message = None
|
32 |
if 'analysis_requested' not in st.session_state:
|
33 |
+
st.session_state.analysis_requested = False
|
34 |
+
if 'selected_model_name' not in st.session_state:
|
35 |
+
st.session_state.selected_model_name = None # Will hold the "models/..." name
|
36 |
+
if 'available_models_dict' not in st.session_state:
|
37 |
+
st.session_state.available_models_dict = {} # Store display_name -> name mapping
|
38 |
+
|
39 |
+
# --- Gemini API Setup & Model Discovery ---
|
40 |
+
model = None # Global variable for the initialized model instance
|
41 |
+
|
42 |
+
# --- NEW: Function to list available models ---
|
43 |
+
@st.cache_data(ttl=3600) # Cache model list for an hour
|
44 |
+
def get_available_models():
|
45 |
+
"""Lists models supporting 'generateContent' using the API key."""
|
46 |
+
model_dict = {}
|
47 |
+
try:
|
48 |
+
if 'GEMINI_API_KEY' not in st.secrets:
|
49 |
+
# Don't stop here, let the main part handle it, but return empty
|
50 |
+
print("API key not found in secrets during model listing attempt.")
|
51 |
+
return {}
|
52 |
+
# Configure API key temporarily just for listing
|
53 |
+
genai.configure(api_key=st.secrets["GEMINI_API_KEY"])
|
54 |
+
print("Listing available models via API...")
|
55 |
+
for m in genai.list_models():
|
56 |
+
# Check if the model supports the 'generateContent' method
|
57 |
+
if 'generateContent' in m.supported_generation_methods:
|
58 |
+
# Store mapping: user-friendly name -> internal name
|
59 |
+
model_dict[m.display_name] = m.name
|
60 |
+
print(f"Found {len(model_dict)} compatible models.")
|
61 |
+
return model_dict
|
62 |
+
except Exception as e:
|
63 |
+
st.error(f"π¨ Error listing available models: {e}")
|
64 |
+
return {} # Return empty on error
|
65 |
|
66 |
def initialize_gemini_model():
|
67 |
+
"""Initializes the Gemini model based on the selected name."""
|
68 |
global model
|
69 |
+
selected_name = st.session_state.get('selected_model_name')
|
70 |
+
|
71 |
+
if selected_name and model is None and not st.session_state.mock_api_call:
|
72 |
try:
|
73 |
if 'GEMINI_API_KEY' not in st.secrets:
|
74 |
st.error("π¨ Gemini API Key not found. Add it to `.streamlit/secrets.toml`.")
|
75 |
+
st.stop() # Stop if key missing for initialization
|
76 |
+
# Configure API key (might be redundant if list_models worked, but safe)
|
77 |
genai.configure(api_key=st.secrets["GEMINI_API_KEY"])
|
78 |
+
print(f"Initializing Gemini Model: {selected_name}")
|
79 |
+
# Use the selected model name from session state
|
80 |
+
model = genai.GenerativeModel(model_name=selected_name)
|
81 |
+
print(f"Gemini Model Initialized ({selected_name}).")
|
82 |
return True
|
83 |
except Exception as e:
|
84 |
+
st.error(f"π¨ Error initializing selected Gemini model '{selected_name}': {e}")
|
85 |
+
st.session_state.selected_model_name = None # Reset selection on error
|
86 |
st.stop()
|
87 |
return False
|
88 |
elif st.session_state.mock_api_call:
|
89 |
+
return True # No init needed for mock
|
90 |
+
elif model is not None and model.model_name == selected_name:
|
91 |
+
return True # Already initialized with the correct model
|
92 |
+
elif model is not None and model.model_name != selected_name:
|
93 |
+
print(f"Model changed. Re-initializing...")
|
94 |
+
model = None # Reset model instance
|
95 |
+
return initialize_gemini_model() # Recurse to re-initialize with new name
|
96 |
+
elif not selected_name and not st.session_state.mock_api_call:
|
97 |
+
# This case happens if no model is selected yet
|
98 |
+
return False # Cannot initialize without a selection
|
99 |
+
return False # Default case
|
100 |
|
101 |
# --- Helper Functions ---
|
102 |
+
# (estimate_token_count, process_zip_file_cached, construct_analysis_prompt,
|
103 |
+
# call_gemini_api, display_results - remain the same as the optimized version)
|
104 |
+
# estimate_token_count
|
105 |
+
def estimate_token_count(text): return len(text) // 3
|
106 |
|
107 |
+
# process_zip_file_cached (no changes)
|
108 |
+
@st.cache_data(max_entries=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
def process_zip_file_cached(file_id, file_size, file_content_bytes):
|
110 |
+
# ... (keep the exact same implementation as the previous optimized version) ...
|
111 |
+
code_files = {}; total_chars = 0; file_count = 0; ignored_files = []
|
112 |
+
status_placeholder = st.empty(); progress_bar = status_placeholder.progress(0)
|
|
|
|
|
|
|
|
|
|
|
113 |
try:
|
114 |
with zipfile.ZipFile(io.BytesIO(file_content_bytes), 'r') as zip_ref:
|
115 |
+
members = zip_ref.infolist(); total_members = len(members)
|
|
|
116 |
for i, member in enumerate(members):
|
117 |
+
if i % 10 == 0: progress_bar.progress(int((i / total_members) * 100))
|
118 |
+
if member.is_dir() or any(p.startswith('.') for p in Path(member.filename).parts) or '__' in member.filename: continue
|
|
|
|
|
|
|
|
|
|
|
119 |
file_path = Path(member.filename)
|
120 |
if file_path.suffix.lower() in CODE_EXTENSIONS:
|
121 |
try:
|
122 |
with zip_ref.open(member) as file:
|
123 |
file_bytes = file.read()
|
124 |
+
try: content = file_bytes.decode('utf-8')
|
|
|
125 |
except UnicodeDecodeError:
|
126 |
+
try: content = file_bytes.decode('latin-1')
|
127 |
+
except Exception as decode_err: ignored_files.append(f"{member.filename} (Decode Error: {decode_err})"); continue
|
128 |
+
code_files[member.filename] = content; total_chars += len(content); file_count += 1
|
129 |
+
except Exception as read_err: ignored_files.append(f"{member.filename} (Read Error: {read_err})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
else:
|
131 |
+
if not (any(p.startswith('.') for p in Path(member.filename).parts) or '__' in member.filename):
|
132 |
+
ignored_files.append(f"{member.filename} (Skipped Extension: {file_path.suffix})")
|
133 |
+
progress_bar.progress(100); status_placeholder.empty()
|
134 |
+
except zipfile.BadZipFile: status_placeholder.empty(); st.error("π¨ Invalid ZIP."); return None, 0, 0, []
|
135 |
+
except Exception as e: status_placeholder.empty(); st.error(f"π¨ ZIP Error: {e}"); return None, 0, 0, []
|
136 |
+
if file_count == 0 and not ignored_files: st.warning("No code files found.")
|
137 |
+
elif file_count == 0 and ignored_files: st.warning("No code files found; some skipped.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
return code_files, total_chars, file_count, ignored_files
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
+
# construct_analysis_prompt (no changes)
|
142 |
+
def construct_analysis_prompt(code_files_dict, requested_analyses):
|
143 |
+
# ... (keep the exact same implementation as the previous optimized version) ...
|
144 |
+
prompt_parts = ["Analyze the following codebase...\n\n"]; current_token_estimate = estimate_token_count(prompt_parts[0])
|
145 |
+
included_files = []; code_segments = []; prompt_status = st.empty()
|
146 |
+
if len(code_files_dict) > 50: prompt_status.info("Constructing prompt...")
|
147 |
for filename, content in code_files_dict.items():
|
148 |
+
segment = f"--- START FILE: {filename} ---\n{content}\n--- END FILE: {filename} ---\n\n"
|
|
|
|
|
|
|
149 |
segment_token_estimate = estimate_token_count(segment)
|
|
|
150 |
if current_token_estimate + segment_token_estimate <= MAX_PROMPT_TOKENS_ESTIMATE:
|
151 |
+
code_segments.append(segment); current_token_estimate += segment_token_estimate; included_files.append(filename)
|
152 |
+
else: st.warning(f"β οΈ Codebase may exceed context limit. Analyzed first {len(included_files)} files (~{current_token_estimate:,} tokens)."); break
|
|
|
|
|
|
|
|
|
|
|
153 |
prompt_status.empty()
|
154 |
+
if not included_files: st.error("π¨ No code files included in prompt."); return None, []
|
155 |
+
prompt_parts.append("".join(code_segments))
|
156 |
+
json_structure_description = "{\n"; structure_parts = []
|
157 |
+
if "generate_docs" in requested_analyses: structure_parts.append(' "documentation_suggestions": [...]') # Use shorthand for brevity
|
158 |
+
if "find_bugs" in requested_analyses: structure_parts.append(' "potential_bugs": [...]')
|
159 |
+
if "check_style" in requested_analyses: structure_parts.append(' "style_issues": [...]')
|
160 |
+
if "summarize_modules" in requested_analyses: structure_parts.append(' "module_summaries": [...]')
|
161 |
+
if "suggest_refactoring" in requested_analyses: structure_parts.append(' "refactoring_suggestions": [...]')
|
162 |
+
json_structure_description += ",\n".join(structure_parts) + "\n}"
|
163 |
+
prompt_footer = f"\n**Analysis Task:**...\n**Output Format:**...\n{json_structure_description}\n**JSON Output Only:**\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
prompt_parts.append(prompt_footer)
|
165 |
full_prompt = "".join(prompt_parts)
|
166 |
return full_prompt, included_files
|
167 |
|
168 |
+
# call_gemini_api (no changes other than relying on the globally selected model)
|
169 |
def call_gemini_api(prompt):
|
170 |
+
# ... (keep the exact same implementation as the previous optimized version,
|
171 |
+
# it implicitly uses the 'model' variable initialized by initialize_gemini_model) ...
|
172 |
+
if not prompt: return None, "Prompt generation failed."
|
173 |
+
# MOCK MODE
|
|
|
174 |
if st.session_state.mock_api_call:
|
175 |
+
st.info(" MOCK MODE: Simulating API call..."); time.sleep(1)
|
176 |
+
mock_json_response = json.dumps({"documentation_suggestions": [],"potential_bugs": [],"style_issues": [],"module_summaries": [],"refactoring_suggestions": []})
|
177 |
+
st.success("Mock response generated."); return json.loads(mock_json_response), None
|
178 |
+
# REAL API CALL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
else:
|
180 |
+
if not initialize_gemini_model(): return None, "Gemini Model Initialization Failed."
|
181 |
+
if model is None: return None, "Gemini model not selected or available." # Added check
|
|
|
|
|
|
|
182 |
try:
|
183 |
api_status = st.empty()
|
184 |
+
# Include model name in status message
|
185 |
+
api_status.info(f"π‘ Sending request to {model.model_name} (Est. prompt tokens: {estimate_token_count(prompt):,})... Please wait.")
|
186 |
+
start_time = time.time(); response = model.generate_content(prompt, generation_config=genai.types.GenerationConfig(temperature=0.2), safety_settings=[{"category": c, "threshold": "BLOCK_MEDIUM_AND_ABOVE"} for c in ["HARM_CATEGORY_HARASSMENT", "HARM_CATEGORY_HATE_SPEECH", "HARM_CATEGORY_SEXUALLY_EXPLICIT", "HARM_CATEGORY_DANGEROUS_CONTENT"]])
|
187 |
+
end_time = time.time(); api_status.success(f"β
Response received from AI ({model.model_name}) in {end_time - start_time:.2f}s."); time.sleep(1); api_status.empty()
|
188 |
+
try: # Keep JSON parsing logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
json_response_text = response.text.strip()
|
190 |
+
# ... (rest of JSON parsing identical to previous version) ...
|
191 |
+
if json_response_text.startswith("```json"): json_response_text = json_response_text[7:]
|
192 |
+
if json_response_text.startswith("```"): json_response_text = json_response_text[3:]
|
193 |
+
if json_response_text.endswith("```"): json_response_text = json_response_text[:-3]
|
194 |
+
json_start = json_response_text.find('{'); json_end = json_response_text.rfind('}') + 1
|
|
|
|
|
|
|
195 |
if json_start != -1 and json_end != -1 and json_end > json_start:
|
196 |
+
final_json_text = json_response_text[json_start:json_end]; insights = json.loads(final_json_text); return insights, None
|
197 |
+
else: st.warning("β οΈ Could not find valid JSON object."); return {"raw_response": response.text}, "AI response did not contain clear JSON object."
|
198 |
+
# ... (keep error handling for JSONDecodeError, AttributeError etc. identical) ...
|
199 |
+
except json.JSONDecodeError as json_err: st.error(f"π¨ Error parsing JSON: {json_err}"); st.code(response.text, language='text'); return None, f"AI response not valid JSON: {json_err}"
|
200 |
+
except AttributeError: st.error(f"π¨ Unexpected API response structure (AttributeError)."); st.code(f"Response object: {response}", language='text'); return None, "Unexpected response structure (AttributeError)." # Simplified message
|
201 |
+
except Exception as e: st.error(f"π¨ Unexpected issue processing response: {e}"); try: st.code(f"Response object: {response}", language='text'); except: pass; return None, f"Unexpected response structure: {e}"
|
202 |
+
except Exception as e: # Keep API call error handling
|
203 |
+
api_status.empty(); st.error(f"π¨ API call error: {e}"); error_msg = f"API call failed: {e}"
|
204 |
+
# ... (keep specific error message logic identical) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
if hasattr(e, 'message'):
|
206 |
+
if "429" in e.message: error_msg = "API Quota Exceeded or Rate Limit hit."
|
207 |
+
elif "API key not valid" in e.message: error_msg = "Invalid Gemini API Key."
|
208 |
+
elif "permission denied" in e.message.lower(): error_msg = f"Permission Denied for model '{st.session_state.selected_model_name}'. Check API key access."
|
209 |
+
elif "blocked" in e.message.lower(): error_msg = "Content blocked due to safety settings."
|
210 |
+
elif "block_reason: SAFETY" in str(e): error_msg = "Content blocked due to safety settings."
|
|
|
|
|
|
|
211 |
return None, error_msg
|
212 |
|
213 |
+
# display_results (no changes needed from optimized version)
|
214 |
def display_results(results_json, requested_analyses):
|
215 |
+
# ... (keep the exact same implementation as the previous optimized version with pagination) ...
|
216 |
st.header("π Analysis Report")
|
217 |
+
if not isinstance(results_json, dict): st.error("Invalid results format."); st.json(results_json); return
|
218 |
+
if "raw_response" in results_json: st.subheader("Raw AI Response (JSON Parsing Failed)"); st.code(results_json["raw_response"], language='text'); return
|
219 |
+
display_config = { # Keep config same
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
"generate_docs": {"key": "documentation_suggestions", "title": AVAILABLE_ANALYSES["generate_docs"], "fields": {"file": "File", "line": "Line"}},
|
221 |
"find_bugs": {"key": "potential_bugs", "title": AVAILABLE_ANALYSES["find_bugs"], "fields": {"file": "File", "line": "Line", "severity": "Severity"}},
|
222 |
"check_style": {"key": "style_issues", "title": AVAILABLE_ANALYSES["check_style"], "fields": {"file": "File", "line": "Line"}},
|
223 |
"summarize_modules": {"key": "module_summaries", "title": AVAILABLE_ANALYSES["summarize_modules"], "fields": {"file": "File"}},
|
224 |
"suggest_refactoring": {"key": "refactoring_suggestions", "title": AVAILABLE_ANALYSES["suggest_refactoring"], "fields": {"file": "File", "line": "Line", "area": "Area"}}
|
225 |
}
|
|
|
226 |
any_results_found = False
|
227 |
for analysis_key in requested_analyses:
|
228 |
if analysis_key in display_config:
|
229 |
+
config = display_config[analysis_key]; items = results_json.get(config["key"], [])
|
230 |
+
total_items = len(items); st.subheader(f"{config['title']} ({total_items} found)")
|
|
|
|
|
|
|
|
|
231 |
if items:
|
232 |
+
any_results_found = True; state_key = f"visible_{analysis_key}"
|
233 |
+
if state_key not in st.session_state: st.session_state[state_key] = RESULTS_PAGE_SIZE
|
234 |
+
visible_count = st.session_state[state_key]; items_to_display = items[:visible_count]
|
235 |
+
for item in items_to_display: # Keep item display logic
|
236 |
+
details = [f"**{field_label}:** `{item.get(field_key, 'N/A')}`" if field_key == 'file' else f"**{field_label}:** {item.get(field_key, 'N/A')}" for field_key, field_label in config["fields"].items() if item.get(field_key, 'N/A') != 'N/A']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
st.markdown("- " + " - ".join(details))
|
238 |
+
if 'suggestion' in item: st.code(item['suggestion'], language='text')
|
239 |
+
elif 'description' in item: st.markdown(f" > {item['description']}")
|
240 |
+
elif 'summary' in item: st.markdown(f" > {item['summary']}")
|
241 |
+
if total_items > visible_count: # Keep "Show More" logic
|
|
|
|
|
|
|
|
|
242 |
if st.button(f"Show more ({total_items - visible_count} remaining)", key=f"more_{analysis_key}"):
|
243 |
+
st.session_state[state_key] += RESULTS_PAGE_SIZE; st.rerun()
|
244 |
+
else: st.markdown("_No items found for this category._")
|
|
|
|
|
245 |
st.divider()
|
246 |
+
if not any_results_found: st.info("No specific findings were identified.")
|
247 |
+
st.download_button(label="Download Full Report (JSON)", data=json.dumps(results_json, indent=4), file_name="code_audit_report.json", mime="application/json")
|
248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
# --- Streamlit App Main Interface ---
|
251 |
st.set_page_config(page_title="Codebase Audit Assistant", layout="wide")
|
252 |
+
st.title("π€ Codebase Audit & Documentation Assistant")
|
|
|
253 |
|
254 |
+
# --- Sidebar ---
|
255 |
with st.sidebar:
|
256 |
st.header("βοΈ Analysis Controls")
|
257 |
st.session_state.mock_api_call = st.toggle("π§ͺ Enable Mock API Mode", value=st.session_state.mock_api_call, help="Use fake data instead of calling Gemini API.")
|
258 |
+
|
259 |
+
st.divider()
|
260 |
+
st.header("β Select Model")
|
261 |
+
# --- NEW: Dynamic Model Selection ---
|
262 |
+
if not st.session_state.mock_api_call:
|
263 |
+
# Get available models (uses cache)
|
264 |
+
st.session_state.available_models_dict = get_available_models()
|
265 |
+
model_display_names = list(st.session_state.available_models_dict.keys())
|
266 |
+
|
267 |
+
if model_display_names:
|
268 |
+
# Try to find the index of the previously selected model
|
269 |
+
current_model_display_name = None
|
270 |
+
if st.session_state.selected_model_name:
|
271 |
+
# Find display name matching the stored internal name
|
272 |
+
for disp_name, internal_name in st.session_state.available_models_dict.items():
|
273 |
+
if internal_name == st.session_state.selected_model_name:
|
274 |
+
current_model_display_name = disp_name
|
275 |
+
break
|
276 |
+
|
277 |
+
try:
|
278 |
+
selected_index = model_display_names.index(current_model_display_name) if current_model_display_name in model_display_names else 0
|
279 |
+
except ValueError:
|
280 |
+
selected_index = 0 # Default to first if previous selection not found
|
281 |
+
|
282 |
+
|
283 |
+
selected_display_name = st.selectbox(
|
284 |
+
"Choose Gemini model:",
|
285 |
+
options=model_display_names,
|
286 |
+
index=selected_index,
|
287 |
+
key="model_selector",
|
288 |
+
help="Select the Gemini model to use for analysis."
|
289 |
+
)
|
290 |
+
# Update session state with the internal name based on selection
|
291 |
+
st.session_state.selected_model_name = st.session_state.available_models_dict.get(selected_display_name)
|
292 |
+
st.info(f"Using REAL Gemini API ({st.session_state.selected_model_name})")
|
293 |
+
elif 'GEMINI_API_KEY' in st.secrets:
|
294 |
+
st.warning("No compatible models found or error listing models. Check API Key permissions.")
|
295 |
+
st.session_state.selected_model_name = None # Ensure no model selected
|
296 |
+
else:
|
297 |
+
st.warning("Add GEMINI_API_KEY to secrets to list models.")
|
298 |
+
st.session_state.selected_model_name = None
|
299 |
+
|
300 |
+
else: # Mock mode is active
|
301 |
+
st.info("Mock API Mode ACTIVE")
|
302 |
+
st.session_state.selected_model_name = "mock_model" # Use a placeholder name for mock mode
|
303 |
+
# --- End Dynamic Model Selection ---
|
304 |
+
|
305 |
+
|
306 |
st.divider()
|
307 |
st.header("π Select Analyses")
|
308 |
selected_analyses = [key for key, name in AVAILABLE_ANALYSES.items() if st.checkbox(name, value=True, key=f"cb_{key}")]
|
309 |
st.divider()
|
310 |
+
st.header("π How To Use") # Keep help text
|
311 |
+
st.info("1. Set API Key.\n2. Toggle Mock Mode if needed.\n3. Select Model (if not Mock).\n4. Select analyses.\n5. Upload ZIP.\n6. Click 'Analyze'.\n7. Review report.")
|
312 |
+
st.info(f"Note: Limited by token estimates (~{MAX_PROMPT_TOKENS_ESTIMATE:,} est. tokens).")
|
313 |
st.divider()
|
314 |
+
st.warning("β οΈ **Privacy:** Code sent to Google API if Mock Mode is OFF.")
|
315 |
+
|
316 |
+
|
317 |
+
# Update title dynamically based on selected model
|
318 |
+
if st.session_state.selected_model_name and not st.session_state.mock_api_call:
|
319 |
+
st.markdown(f"Upload codebase (`.zip`) for analysis via **{st.session_state.selected_model_name}**.")
|
320 |
+
elif st.session_state.mock_api_call:
|
321 |
+
st.markdown("Upload codebase (`.zip`) for analysis (Using **Mock Data**).")
|
322 |
+
else:
|
323 |
+
st.markdown("Upload codebase (`.zip`) for analysis.")
|
324 |
|
325 |
+
|
326 |
+
# --- Main Content Area ---
|
327 |
+
# (Keep the file uploader, button logic, and results display structure the same)
|
328 |
uploaded_file = st.file_uploader("π Upload Codebase ZIP File", type=['zip'], key="file_uploader",
|
329 |
on_change=lambda: st.session_state.update(analysis_results=None, error_message=None, analysis_requested=False))
|
330 |
+
analysis_button_placeholder = st.empty()
|
331 |
+
results_placeholder = st.container()
|
|
|
332 |
|
333 |
if uploaded_file:
|
334 |
st.success(f"β
File '{uploaded_file.name}' uploaded.")
|
|
|
|
|
335 |
uploaded_file_bytes = uploaded_file.getvalue()
|
336 |
file_id = f"{uploaded_file.name}-{uploaded_file.size}"
|
|
|
337 |
code_files, total_chars, file_count, ignored_files = process_zip_file_cached(file_id, uploaded_file.size, uploaded_file_bytes)
|
|
|
338 |
if code_files is not None:
|
339 |
+
st.info(f"Found **{file_count}** code files ({total_chars:,} chars). Est. tokens: ~{estimate_token_count(total_chars):,}")
|
340 |
if ignored_files:
|
341 |
+
with st.expander(f"View {len(ignored_files)} Skipped/Ignored Files"): st.code("\n".join(ignored_files), language='text')
|
342 |
+
|
343 |
+
# Disable button if no model selected (and not in mock mode)
|
344 |
+
model_ready = bool(st.session_state.selected_model_name) or st.session_state.mock_api_call
|
345 |
+
analyze_button_disabled = (not selected_analyses or file_count == 0 or not model_ready)
|
346 |
+
analyze_button_label = "Analyze Codebase"
|
347 |
+
if not model_ready: analyze_button_label = "Select Model First"
|
348 |
+
elif analyze_button_disabled: analyze_button_label = "Select Analyses or Upload Valid Code"
|
349 |
|
|
|
|
|
350 |
if analysis_button_placeholder.button(analyze_button_label, type="primary", disabled=analyze_button_disabled):
|
351 |
+
st.session_state.analysis_requested = True; st.session_state.analysis_results = None; st.session_state.error_message = None
|
352 |
+
if not selected_analyses: st.warning("Please select analysis types.")
|
353 |
+
elif file_count == 0: st.warning("No relevant code files found.")
|
354 |
+
elif not model_ready: st.warning("Please select a Gemini model from the sidebar.") # Should be disabled, but safety check
|
|
|
|
|
|
|
|
|
355 |
else:
|
356 |
with results_placeholder:
|
357 |
+
spinner_model_name = st.session_state.selected_model_name if not st.session_state.mock_api_call else "Mock Mode"
|
358 |
+
spinner_msg = f"π Preparing prompt & contacting AI ({spinner_model_name})... Please wait."
|
359 |
+
with st.spinner(spinner_msg):
|
360 |
analysis_prompt, included_files_in_prompt = construct_analysis_prompt(code_files, selected_analyses)
|
361 |
if analysis_prompt and included_files_in_prompt:
|
362 |
results_json, error_msg = call_gemini_api(analysis_prompt)
|
363 |
+
st.session_state.analysis_results = results_json; st.session_state.error_message = error_msg
|
364 |
+
elif not included_files_in_prompt: st.session_state.error_message = "Could not proceed: No files included."
|
365 |
+
else: st.session_state.error_message = "Failed to generate analysis prompt."
|
|
|
|
|
|
|
366 |
st.rerun()
|
367 |
|
368 |
+
# Display results (Keep the same logic)
|
369 |
if st.session_state.analysis_requested:
|
370 |
+
with results_placeholder:
|
371 |
+
st.divider()
|
372 |
+
if st.session_state.error_message:
|
373 |
+
st.error(f"Analysis Failed: {st.session_state.error_message}")
|
374 |
+
if isinstance(st.session_state.analysis_results, dict) and "raw_response" in st.session_state.analysis_results:
|
375 |
+
st.subheader("Raw AI Response"); st.code(st.session_state.analysis_results["raw_response"], language='text')
|
376 |
+
elif st.session_state.analysis_results:
|
377 |
+
# Pass selected_analyses to display_results in case it's needed later
|
378 |
+
display_results(st.session_state.analysis_results, selected_analyses)
|
379 |
+
else: st.info("Analysis initiated, but no results/errors stored.")
|
380 |
+
elif not uploaded_file: results_placeholder.info("Upload a ZIP file to begin.")
|
|
|
|
|
381 |
|
382 |
results_placeholder.divider()
|
383 |
+
results_placeholder.markdown("_Assistant powered by Google Gemini._")
|