Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,14 +55,18 @@ def initialize_gemini_model():
|
|
55 |
# Running in Mock Mode. Skipping Gemini initialization.
|
56 |
return True # Allow proceeding in mock mode
|
57 |
elif model is not None:
|
58 |
-
# Gemini Model already initialized.
|
59 |
return True
|
60 |
return False
|
61 |
|
62 |
# --- Helper Functions ---
|
63 |
|
64 |
def estimate_token_count(text):
|
65 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
66 |
return len(text) // 3
|
67 |
|
68 |
# --- OPTIMIZATION: Cache ZIP processing ---
|
@@ -81,7 +85,7 @@ def process_zip_file_cached(file_id, file_size, file_content_bytes):
|
|
81 |
members = zip_ref.infolist()
|
82 |
total_members = len(members)
|
83 |
for i, member in enumerate(members):
|
84 |
-
# Update progress bar
|
85 |
if i % 10 == 0:
|
86 |
progress_bar.progress(int((i / total_members) * 100))
|
87 |
|
@@ -128,7 +132,7 @@ def process_zip_file_cached(file_id, file_size, file_content_bytes):
|
|
128 |
elif file_count == 0 and ignored_files:
|
129 |
st.warning("No files with recognized code extensions found. Some files were skipped.")
|
130 |
|
131 |
-
print(f"Cache miss or new file: Processed ZIP {file_id}")
|
132 |
return code_files, total_chars, file_count, ignored_files
|
133 |
|
134 |
def construct_analysis_prompt(code_files_dict, requested_analyses):
|
@@ -136,11 +140,8 @@ def construct_analysis_prompt(code_files_dict, requested_analyses):
|
|
136 |
prompt_parts = ["Analyze the following codebase provided as a collection of file paths and their content.\n\n"]
|
137 |
current_token_estimate = estimate_token_count(prompt_parts[0])
|
138 |
included_files = []
|
139 |
-
|
140 |
-
# Use join for potentially faster concatenation
|
141 |
code_segments = []
|
142 |
|
143 |
-
# Provide feedback for large codebases
|
144 |
prompt_status = st.empty()
|
145 |
if len(code_files_dict) > 50:
|
146 |
prompt_status.write("Constructing prompt (processing files)...")
|
@@ -160,7 +161,7 @@ def construct_analysis_prompt(code_files_dict, requested_analyses):
|
|
160 |
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).")
|
161 |
break
|
162 |
|
163 |
-
prompt_status.empty()
|
164 |
|
165 |
if not included_files:
|
166 |
st.error("🚨 No code files could be included within the estimated token limit.")
|
@@ -169,7 +170,6 @@ def construct_analysis_prompt(code_files_dict, requested_analyses):
|
|
169 |
concatenated_code = "".join(code_segments)
|
170 |
prompt_parts.append(concatenated_code)
|
171 |
|
172 |
-
# Generate the expected JSON structure description based on selected analyses
|
173 |
json_structure_description = "{\n"
|
174 |
structure_parts = []
|
175 |
if "generate_docs" in requested_analyses:
|
@@ -209,9 +209,8 @@ def call_gemini_api(prompt):
|
|
209 |
# MOCK MODE LOGIC
|
210 |
if st.session_state.mock_api_call:
|
211 |
st.info("MOCK MODE: Simulating API call...")
|
212 |
-
st.write("...")
|
213 |
-
time.sleep(1)
|
214 |
-
|
215 |
mock_json_response = json.dumps({
|
216 |
"documentation_suggestions": [{"file": "mock/core.py", "line": 15, "suggestion": "def process_data(data):\n \"\"\"Processes the input data using mock logic.\"\"\""}],
|
217 |
"potential_bugs": [{"file": "mock/utils.py", "line": 22, "description": "Potential division by zero if denominator is not checked.", "severity": "Medium"}],
|
@@ -400,6 +399,7 @@ results_placeholder = st.container() # Container for results display
|
|
400 |
if uploaded_file:
|
401 |
st.success(f"✅ File '{uploaded_file.name}' uploaded.")
|
402 |
|
|
|
403 |
uploaded_file_bytes = uploaded_file.getvalue()
|
404 |
file_id = f"{uploaded_file.name}-{uploaded_file.size}"
|
405 |
|
@@ -448,7 +448,6 @@ if st.session_state.analysis_requested:
|
|
448 |
display_results(st.session_state.analysis_results, selected_analyses)
|
449 |
else:
|
450 |
st.info("Analysis initiated, but no results or errors were stored. Please try again.")
|
451 |
-
|
452 |
elif not uploaded_file:
|
453 |
results_placeholder.info("Upload a ZIP file containing your source code to begin.")
|
454 |
|
|
|
55 |
# Running in Mock Mode. Skipping Gemini initialization.
|
56 |
return True # Allow proceeding in mock mode
|
57 |
elif model is not None:
|
|
|
58 |
return True
|
59 |
return False
|
60 |
|
61 |
# --- Helper Functions ---
|
62 |
|
63 |
def estimate_token_count(text):
|
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 ---
|
|
|
85 |
members = zip_ref.infolist()
|
86 |
total_members = len(members)
|
87 |
for i, member in enumerate(members):
|
88 |
+
# Update progress bar every 10 files to reduce overhead
|
89 |
if i % 10 == 0:
|
90 |
progress_bar.progress(int((i / total_members) * 100))
|
91 |
|
|
|
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):
|
|
|
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)...")
|
|
|
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 |
if not included_files:
|
167 |
st.error("🚨 No code files could be included within the estimated token limit.")
|
|
|
170 |
concatenated_code = "".join(code_segments)
|
171 |
prompt_parts.append(concatenated_code)
|
172 |
|
|
|
173 |
json_structure_description = "{\n"
|
174 |
structure_parts = []
|
175 |
if "generate_docs" in requested_analyses:
|
|
|
209 |
# MOCK MODE LOGIC
|
210 |
if st.session_state.mock_api_call:
|
211 |
st.info("MOCK MODE: Simulating API call...")
|
212 |
+
st.write("...")
|
213 |
+
time.sleep(1)
|
|
|
214 |
mock_json_response = json.dumps({
|
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"}],
|
|
|
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 |
|
|
|
448 |
display_results(st.session_state.analysis_results, selected_analyses)
|
449 |
else:
|
450 |
st.info("Analysis initiated, but no results or errors were stored. Please try again.")
|
|
|
451 |
elif not uploaded_file:
|
452 |
results_placeholder.info("Upload a ZIP file containing your source code to begin.")
|
453 |
|