Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import sqlite3
|
3 |
import hashlib
|
4 |
import os
|
|
|
5 |
import zipfile
|
6 |
from git import Repo
|
7 |
|
@@ -198,6 +199,66 @@ def workspace_page():
|
|
198 |
except Exception as e:
|
199 |
st.error(f"Failed to clone repository: {e}")
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
def generate_documentation_page():
|
202 |
st.subheader(f"Generate Documentation for {st.session_state.current_project}")
|
203 |
st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
|
@@ -208,8 +269,19 @@ def generate_documentation_page():
|
|
208 |
# Button to start generating documentation
|
209 |
if st.button("Generate"):
|
210 |
if functionality.strip():
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
else:
|
214 |
st.error("Please enter the functionality to generate documentation.")
|
215 |
|
@@ -219,6 +291,9 @@ def generate_documentation_page():
|
|
219 |
st.rerun()
|
220 |
|
221 |
|
|
|
|
|
|
|
222 |
def view_documentation_page():
|
223 |
st.subheader(f"View Documentation for {st.session_state.current_project}")
|
224 |
st.write("This page will display the generated documentation for the selected project.")
|
|
|
2 |
import sqlite3
|
3 |
import hashlib
|
4 |
import os
|
5 |
+
import google.generativeai as genai
|
6 |
import zipfile
|
7 |
from git import Repo
|
8 |
|
|
|
199 |
except Exception as e:
|
200 |
st.error(f"Failed to clone repository: {e}")
|
201 |
|
202 |
+
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
203 |
+
|
204 |
+
|
205 |
+
# Configure Gemini API
|
206 |
+
api_key = os.getenv("GEMINI")
|
207 |
+
genai.configure(api_key)
|
208 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
209 |
+
|
210 |
+
def read_project_files(project_path):
|
211 |
+
"""Reads all files in the project directory and its subdirectories."""
|
212 |
+
file_paths = []
|
213 |
+
for root, _, files in os.walk(project_path):
|
214 |
+
for file in files:
|
215 |
+
file_paths.append(os.path.join(root, file))
|
216 |
+
return file_paths
|
217 |
+
|
218 |
+
def read_files(file_paths):
|
219 |
+
"""Reads content from a list of file paths."""
|
220 |
+
file_contents = {}
|
221 |
+
for file_path in file_paths:
|
222 |
+
if os.path.exists(file_path):
|
223 |
+
with open(file_path, 'r') as file:
|
224 |
+
file_contents[file_path] = file.read()
|
225 |
+
else:
|
226 |
+
print(f"File not found: {file_path}")
|
227 |
+
return file_contents
|
228 |
+
|
229 |
+
def generate_prompt(file_contents, functionality_description):
|
230 |
+
"""Generates a prompt for Gemini to analyze the files."""
|
231 |
+
prompt = "Analyze the following code files to identify all functions required to implement the functionality: "
|
232 |
+
prompt += f"'{functionality_description}'.\n\n"
|
233 |
+
|
234 |
+
for file_path, content in file_contents.items():
|
235 |
+
prompt += f"File: {os.path.basename(file_path)}\n{content}\n\n"
|
236 |
+
|
237 |
+
prompt += "For each relevant function, provide:\n"
|
238 |
+
prompt += "1. The function name.\n"
|
239 |
+
prompt += "2. A brief description of its purpose.\n"
|
240 |
+
prompt += "3. Its inputs and outputs.\n"
|
241 |
+
prompt += "4. Dependencies on other functions or modules.\n"
|
242 |
+
|
243 |
+
return prompt
|
244 |
+
|
245 |
+
def identify_required_functions(project_path, functionality_description):
|
246 |
+
"""Identifies required functions for a specified functionality."""
|
247 |
+
# Gather all file paths in the project directory
|
248 |
+
file_paths = read_project_files(project_path)
|
249 |
+
|
250 |
+
# Read file contents
|
251 |
+
file_contents = read_files(file_paths)
|
252 |
+
|
253 |
+
# Generate a refined prompt for Gemini
|
254 |
+
prompt = generate_prompt(file_contents, functionality_description)
|
255 |
+
|
256 |
+
# Call the Gemini model
|
257 |
+
response = model.generate_content(prompt)
|
258 |
+
|
259 |
+
# Process and return the response
|
260 |
+
return response.text
|
261 |
+
|
262 |
def generate_documentation_page():
|
263 |
st.subheader(f"Generate Documentation for {st.session_state.current_project}")
|
264 |
st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
|
|
|
269 |
# Button to start generating documentation
|
270 |
if st.button("Generate"):
|
271 |
if functionality.strip():
|
272 |
+
st.write("Analyzing project files... Please wait.")
|
273 |
+
|
274 |
+
# Get the path of the current project
|
275 |
+
user_folder = os.path.join("user_projects", st.session_state.username)
|
276 |
+
project_folder = os.path.join(user_folder, st.session_state.current_project)
|
277 |
+
|
278 |
+
# Call the function to identify required functions
|
279 |
+
try:
|
280 |
+
result = identify_required_functions(project_folder, functionality)
|
281 |
+
st.success("Documentation generated successfully!")
|
282 |
+
st.text_area("Generated Documentation", result, height=400)
|
283 |
+
except Exception as e:
|
284 |
+
st.error(f"An error occurred: {e}")
|
285 |
else:
|
286 |
st.error("Please enter the functionality to generate documentation.")
|
287 |
|
|
|
291 |
st.rerun()
|
292 |
|
293 |
|
294 |
+
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
295 |
+
|
296 |
+
|
297 |
def view_documentation_page():
|
298 |
st.subheader(f"View Documentation for {st.session_state.current_project}")
|
299 |
st.write("This page will display the generated documentation for the selected project.")
|