Update app.py
Browse files
app.py
CHANGED
@@ -246,12 +246,24 @@ def generate_prompt(file_contents, functionality_description):
|
|
246 |
for file_path, content in file_contents.items():
|
247 |
prompt += f"File: {os.path.basename(file_path)}\n{content}\n\n"
|
248 |
|
249 |
-
prompt += "
|
250 |
prompt += "1. Which file the function is found in.\n"
|
251 |
prompt += "2. The function name.\n"
|
252 |
prompt += "3. Dependencies on other functions or modules.\n"
|
253 |
|
254 |
-
prompt += "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
return prompt
|
257 |
|
@@ -300,42 +312,112 @@ def clean_output(output):
|
|
300 |
]
|
301 |
return "\n".join(filtered_lines)
|
302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
def validate_and_generate_documentation(api_url, headers, gemini_output, file_contents, functionality_description):
|
304 |
"""
|
305 |
-
Uses the Hugging Face Inference API to
|
306 |
-
Ensures that the output
|
307 |
"""
|
308 |
-
# Generate the prompt for the Qwen model
|
309 |
prompt = f"""
|
310 |
User-specified functionality: '{functionality_description}'
|
311 |
Functions identified by Gemini:
|
312 |
{gemini_output}
|
313 |
|
314 |
Task:
|
315 |
-
1.
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
"""
|
340 |
|
341 |
# Send the prompt to the Hugging Face API
|
@@ -352,45 +434,48 @@ def validate_and_generate_documentation(api_url, headers, gemini_output, file_co
|
|
352 |
else:
|
353 |
raise ValueError("Unexpected response format from Hugging Face API.")
|
354 |
|
355 |
-
# Clean the output to exclude any
|
356 |
-
|
357 |
-
return cleaned_output
|
358 |
else:
|
359 |
raise ValueError(f"Error: {response.status_code}, {response.text}")
|
360 |
|
361 |
|
362 |
|
363 |
|
|
|
364 |
def generate_documentation_page():
|
365 |
st.subheader(f"Generate Documentation for {st.session_state.current_project}")
|
366 |
-
st.write("Enter the functionality or parts of the project for which you'd like to
|
367 |
|
368 |
# Prompt user for functionality description
|
369 |
-
functionality = st.text_area(
|
|
|
|
|
|
|
370 |
|
371 |
# Button to start analyzing functionality
|
372 |
if st.button("Analyze"):
|
373 |
if functionality.strip():
|
374 |
st.write("Analyzing project files... Please wait.")
|
375 |
-
|
376 |
# Get the path of the current project
|
377 |
user_folder = os.path.join("user_projects", st.session_state.username)
|
378 |
project_folder = os.path.join(user_folder, st.session_state.current_project)
|
379 |
-
|
380 |
if os.path.exists(project_folder):
|
381 |
try:
|
382 |
# Call Gemini to identify required functions
|
383 |
gemini_result = identify_required_functions(project_folder, functionality)
|
384 |
-
|
385 |
# Read project files
|
386 |
file_paths = read_project_files(project_folder)
|
387 |
file_contents = read_files(file_paths)
|
388 |
-
|
389 |
-
# Call the Hugging Face API
|
390 |
final_documentation = validate_and_generate_documentation(
|
391 |
API_URL, headers, gemini_result, file_contents, functionality
|
392 |
)
|
393 |
-
|
394 |
# Display the final documentation
|
395 |
st.success("Documentation generated successfully!")
|
396 |
st.text_area("Generated Documentation", final_documentation, height=600)
|
|
|
246 |
for file_path, content in file_contents.items():
|
247 |
prompt += f"File: {os.path.basename(file_path)}\n{content}\n\n"
|
248 |
|
249 |
+
prompt += "For each relevant function, provide:\n"
|
250 |
prompt += "1. Which file the function is found in.\n"
|
251 |
prompt += "2. The function name.\n"
|
252 |
prompt += "3. Dependencies on other functions or modules.\n"
|
253 |
|
254 |
+
prompt += """
|
255 |
+
Return your output in the following format providing no commentary:
|
256 |
+
|
257 |
+
Project Summary:
|
258 |
+
<A summary of the project>
|
259 |
+
Functionality:
|
260 |
+
<a more precise version of the user defined funcionality>
|
261 |
+
Functions:
|
262 |
+
<for each file containing relevant functions>
|
263 |
+
<file name of the file that contains relevant functions>:
|
264 |
+
-<relevant function header from the current file>:
|
265 |
+
-Function Dependencies:<function dependencies seperated by commas>
|
266 |
+
"""
|
267 |
|
268 |
return prompt
|
269 |
|
|
|
312 |
]
|
313 |
return "\n".join(filtered_lines)
|
314 |
|
315 |
+
def extract_project_summary_and_functionality(gemini_output):
|
316 |
+
"""
|
317 |
+
Extracts the Project Summary and Enhanced Functionality from the Gemini output.
|
318 |
+
|
319 |
+
Args:
|
320 |
+
gemini_output (str): The output returned by Gemini.
|
321 |
+
|
322 |
+
Returns:
|
323 |
+
dict: A dictionary with keys 'project_summary' and 'enhanced_functionality'.
|
324 |
+
"""
|
325 |
+
lines = gemini_output.splitlines()
|
326 |
+
project_summary = []
|
327 |
+
enhanced_functionality = []
|
328 |
+
|
329 |
+
# Extract relevant sections
|
330 |
+
current_section = None
|
331 |
+
for line in lines:
|
332 |
+
line = line.strip()
|
333 |
+
if line.startswith("Project Summary:"):
|
334 |
+
current_section = "project_summary"
|
335 |
+
elif line.startswith("Functionality:"):
|
336 |
+
current_section = "enhanced_functionality"
|
337 |
+
elif line.startswith("Functions:"):
|
338 |
+
break
|
339 |
+
elif current_section == "project_summary" and line:
|
340 |
+
project_summary.append(line)
|
341 |
+
elif current_section == "enhanced_functionality" and line:
|
342 |
+
enhanced_functionality.append(line)
|
343 |
+
|
344 |
+
return {
|
345 |
+
"project_summary": " ".join(project_summary).replace("Project Summary:", "").strip(),
|
346 |
+
"enhanced_functionality": " ".join(enhanced_functionality).replace("Functionality:", "").strip(),
|
347 |
+
}
|
348 |
+
|
349 |
+
def extract_functions(gemini_output):
|
350 |
+
"""
|
351 |
+
Extracts the functions and their dependencies from the Gemini output.
|
352 |
+
|
353 |
+
Args:
|
354 |
+
gemini_output (str): The output returned by Gemini.
|
355 |
+
|
356 |
+
Returns:
|
357 |
+
dict: A dictionary where keys are file names and values are dictionaries of functions and their dependencies.
|
358 |
+
"""
|
359 |
+
lines = gemini_output.splitlines()
|
360 |
+
functions_section = False
|
361 |
+
functions = {}
|
362 |
+
current_file = None
|
363 |
+
|
364 |
+
for line in lines:
|
365 |
+
line = line.strip()
|
366 |
+
if line.startswith("Functions:"):
|
367 |
+
functions_section = True
|
368 |
+
continue
|
369 |
+
if functions_section:
|
370 |
+
# Identify file name
|
371 |
+
if line.endswith(":") and not line.startswith("-"):
|
372 |
+
current_file = line[:-1].strip() # Remove colon and strip
|
373 |
+
functions[current_file] = {}
|
374 |
+
# Identify function details
|
375 |
+
elif line.startswith("-") and current_file:
|
376 |
+
parts = line.split(":", maxsplit=1)
|
377 |
+
if len(parts) == 2:
|
378 |
+
function_header = parts[0].replace("-", "").strip()
|
379 |
+
dependencies = parts[1].replace("Function Dependencies:", "").strip()
|
380 |
+
functions[current_file][function_header] = dependencies.split(",") if dependencies else []
|
381 |
+
|
382 |
+
return functions
|
383 |
+
|
384 |
+
|
385 |
def validate_and_generate_documentation(api_url, headers, gemini_output, file_contents, functionality_description):
|
386 |
"""
|
387 |
+
Uses the Hugging Face Inference API to generate documentation based on Gemini output.
|
388 |
+
Ensures that the output matches the expected format without unnecessary details.
|
389 |
"""
|
390 |
+
# Generate the refined prompt for the Qwen model
|
391 |
prompt = f"""
|
392 |
User-specified functionality: '{functionality_description}'
|
393 |
Functions identified by Gemini:
|
394 |
{gemini_output}
|
395 |
|
396 |
Task:
|
397 |
+
1. Generate a summary of the project:
|
398 |
+
Project Summary:
|
399 |
+
<Include project description and library or module dependencies>
|
400 |
+
|
401 |
+
2. Refine the user-defined functionality:
|
402 |
+
Functionality Summary:
|
403 |
+
<Description of user-specified functionality>
|
404 |
+
|
405 |
+
3. Describe the flow of the functionality:
|
406 |
+
Functionality Flow:
|
407 |
+
<Explain the sequence of functions and data flow>
|
408 |
+
|
409 |
+
4. For all relevant functions, generate detailed documentation:
|
410 |
+
Function Documentation:
|
411 |
+
For each relevant function:
|
412 |
+
- Summary: <Description of the function's purpose>
|
413 |
+
- Inputs: <Details of inputs and their types>
|
414 |
+
- Outputs: <Details of outputs and their types>
|
415 |
+
- Dependencies: <Dependencies on other modules/functions>
|
416 |
+
- Data structures: <Details of data structures used>
|
417 |
+
- Algorithmic Details: <Description of the algorithm used in the function>
|
418 |
+
- Error Handling: <Description of how the function handles errors>
|
419 |
+
- Assumptions: <Any assumptions the function makes>
|
420 |
+
- Example Usage: <Example demonstrating how to use the function>
|
421 |
"""
|
422 |
|
423 |
# Send the prompt to the Hugging Face API
|
|
|
434 |
else:
|
435 |
raise ValueError("Unexpected response format from Hugging Face API.")
|
436 |
|
437 |
+
# Clean the output to exclude any unnecessary details
|
438 |
+
return output
|
|
|
439 |
else:
|
440 |
raise ValueError(f"Error: {response.status_code}, {response.text}")
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
+
|
446 |
def generate_documentation_page():
|
447 |
st.subheader(f"Generate Documentation for {st.session_state.current_project}")
|
448 |
+
st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
|
449 |
|
450 |
# Prompt user for functionality description
|
451 |
+
functionality = st.text_area(
|
452 |
+
"Describe the functionality",
|
453 |
+
placeholder="e.g., Explain the function of the file `main.py`",
|
454 |
+
)
|
455 |
|
456 |
# Button to start analyzing functionality
|
457 |
if st.button("Analyze"):
|
458 |
if functionality.strip():
|
459 |
st.write("Analyzing project files... Please wait.")
|
460 |
+
|
461 |
# Get the path of the current project
|
462 |
user_folder = os.path.join("user_projects", st.session_state.username)
|
463 |
project_folder = os.path.join(user_folder, st.session_state.current_project)
|
464 |
+
|
465 |
if os.path.exists(project_folder):
|
466 |
try:
|
467 |
# Call Gemini to identify required functions
|
468 |
gemini_result = identify_required_functions(project_folder, functionality)
|
469 |
+
|
470 |
# Read project files
|
471 |
file_paths = read_project_files(project_folder)
|
472 |
file_contents = read_files(file_paths)
|
473 |
+
|
474 |
+
# Call the Hugging Face API to generate documentation
|
475 |
final_documentation = validate_and_generate_documentation(
|
476 |
API_URL, headers, gemini_result, file_contents, functionality
|
477 |
)
|
478 |
+
|
479 |
# Display the final documentation
|
480 |
st.success("Documentation generated successfully!")
|
481 |
st.text_area("Generated Documentation", final_documentation, height=600)
|