JSenkCC commited on
Commit
39d77c2
·
verified ·
1 Parent(s): fc2ab99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -47
app.py CHANGED
@@ -433,6 +433,7 @@ def process_gemini_output(output):
433
 
434
 
435
  def generate_documentation_page():
 
436
  st.sidebar.image("SimplifAI Logo Long.jpeg", use_container_width=True)
437
  st.sidebar.title(f"Project: {st.session_state.current_project}")
438
  if st.sidebar.button("Back to Project"):
@@ -445,81 +446,107 @@ def generate_documentation_page():
445
  st.subheader(f"Generate Documentation for {st.session_state.current_project}")
446
  st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
447
 
 
 
 
 
 
 
 
448
  # Prompt user for functionality description
449
  functionality = st.text_area(
450
  "Describe the functionality",
451
  placeholder="e.g., Explain the function of the file `main.py`",
452
  )
453
 
454
- # Reset generated files when a new analysis starts
455
  if st.button("Analyze"):
456
  if functionality.strip():
457
- st.session_state.generated_documentation = None
458
- st.session_state.generated_pdf = None
459
- st.session_state.generated_markdown = None
460
  st.write("Analyzing project files... Please wait.")
461
- # Simulate analysis
462
- import time
463
- time.sleep(3) # Replace this with actual analysis logic
464
 
465
- # Example analysis result
466
- st.session_state.generated_documentation = f"Documentation for functionality: {functionality}"
467
- st.success("Analysis complete! Documentation is ready.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
  else:
469
  st.error("Please enter the functionality to analyze.")
470
 
471
- # Display the documentation if available
472
- if st.session_state.get("generated_documentation"):
473
- st.text_area(
474
- "Generated Documentation",
475
- st.session_state.generated_documentation,
476
- height=400,
477
- )
478
 
479
  # Define paths for PDF and Markdown files
480
  user_folder = os.path.join("user_projects", st.session_state.username)
481
- os.makedirs(user_folder, exist_ok=True)
482
 
483
  pdf_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.pdf")
484
  markdown_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.md")
485
 
486
- # Handle PDF generation and download
487
- if st.session_state.get("generated_pdf"):
488
- with open(pdf_path, "rb") as pdf_file:
489
- st.download_button(
490
- label="Download PDF",
491
- data=pdf_file.read(),
492
- file_name=f"{st.session_state.current_project}_Documentation.pdf",
493
- mime="application/pdf",
494
- )
495
- else:
496
  if st.button("Generate PDF"):
497
  try:
498
- generate_pdf(st.session_state.generated_documentation, pdf_path)
499
- st.session_state.generated_pdf = True
500
- st.info("PDF Available in the 'Saved Documentation' Page")
501
- st.experimental_rerun() # Trigger rerun to show the download button
502
  except Exception as e:
503
  st.error(f"Failed to generate PDF: {e}")
504
-
505
- # Handle Markdown generation and download
506
- if st.session_state.get("generated_markdown"):
507
- with open(markdown_path, "rb") as markdown_file:
508
- st.download_button(
509
- label="Download Markdown",
510
- data=markdown_file.read(),
511
- file_name=f"{st.session_state.current_project}_Documentation.md",
512
- mime="text/markdown",
513
- )
514
  else:
515
- if st.button("Generate Markdown"):
 
 
 
 
 
 
 
 
 
 
 
 
 
516
  try:
517
- generate_markdown_file(st.session_state.generated_documentation, markdown_path)
518
- st.session_state.generated_markdown = True
519
- st.info("Markdown Available in the 'Saved Documentation' Page")
520
- st.experimental_rerun() # Trigger rerun to show the download button
521
  except Exception as e:
522
  st.error(f"Failed to generate Markdown file: {e}")
 
 
 
 
 
 
 
 
 
 
 
523
 
524
 
525
 
 
433
 
434
 
435
  def generate_documentation_page():
436
+ # Sidebar with "Log Out" and "Back to Project" buttons
437
  st.sidebar.image("SimplifAI Logo Long.jpeg", use_container_width=True)
438
  st.sidebar.title(f"Project: {st.session_state.current_project}")
439
  if st.sidebar.button("Back to Project"):
 
446
  st.subheader(f"Generate Documentation for {st.session_state.current_project}")
447
  st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
448
 
449
+ # Clear previously generated documentation files when starting a new analysis
450
+ if st.session_state.get("new_analysis_started", False):
451
+ st.session_state.pop("pdf_generated", None)
452
+ st.session_state.pop("markdown_generated", None)
453
+ st.session_state.pop("generated_documentation", None)
454
+ st.session_state.new_analysis_started = False
455
+
456
  # Prompt user for functionality description
457
  functionality = st.text_area(
458
  "Describe the functionality",
459
  placeholder="e.g., Explain the function of the file `main.py`",
460
  )
461
 
462
+ # Button to start analyzing functionality
463
  if st.button("Analyze"):
464
  if functionality.strip():
465
+ st.session_state.new_analysis_started = True
 
 
466
  st.write("Analyzing project files... Please wait.")
 
 
 
467
 
468
+ # Get the path of the current project
469
+ user_folder = os.path.join("user_projects", st.session_state.username)
470
+ project_folder = os.path.join(user_folder, st.session_state.current_project)
471
+
472
+ if os.path.exists(project_folder):
473
+ try:
474
+ # Gather all file paths in the project directory
475
+ file_paths = read_project_files(project_folder)
476
+
477
+ # Read file contents
478
+ file_contents = read_files(file_paths)
479
+
480
+ # Generate documentation using Gemini
481
+ documentation = generate_detailed_documentation(file_contents, functionality)
482
+
483
+ # Save the documentation in session state for exporting and viewing
484
+ st.session_state.generated_documentation = documentation
485
+
486
+ # Display the final documentation
487
+ st.success("Documentation generated successfully!")
488
+ st.text_area("Generated Documentation", documentation, height=600)
489
+ except Exception as e:
490
+ st.error(f"An error occurred: {e}")
491
+ else:
492
+ st.error("Project folder not found. Ensure the GitHub repository was cloned successfully.")
493
  else:
494
  st.error("Please enter the functionality to analyze.")
495
 
496
+ # Add export/download buttons if documentation is available
497
+ if "generated_documentation" in st.session_state and st.session_state.generated_documentation:
498
+ documentation = st.session_state.generated_documentation
 
 
 
 
499
 
500
  # Define paths for PDF and Markdown files
501
  user_folder = os.path.join("user_projects", st.session_state.username)
502
+ os.makedirs(user_folder, exist_ok=True) # Ensure the user folder exists
503
 
504
  pdf_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.pdf")
505
  markdown_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.md")
506
 
507
+ # PDF generation and download logic
508
+ if not st.session_state.get("pdf_generated", False):
 
 
 
 
 
 
 
 
509
  if st.button("Generate PDF"):
510
  try:
511
+ generate_pdf(documentation, pdf_path)
512
+ st.session_state.pdf_generated = True
513
+ st.success("PDF Available in the 'Saved Documentation' Page")
514
+ st.rerun()
515
  except Exception as e:
516
  st.error(f"Failed to generate PDF: {e}")
 
 
 
 
 
 
 
 
 
 
517
  else:
518
+ try:
519
+ with open(pdf_path, "rb") as pdf_file:
520
+ st.download_button(
521
+ label="Download PDF",
522
+ data=pdf_file.read(),
523
+ file_name=f"{st.session_state.current_project}_Documentation.pdf",
524
+ mime="application/pdf",
525
+ )
526
+ except FileNotFoundError:
527
+ st.error("PDF file not found. Try generating it again.")
528
+
529
+ # Markdown generation and download logic
530
+ if not st.session_state.get("markdown_generated", False):
531
+ if st.button("Generate Markdown File"):
532
  try:
533
+ generate_markdown_file(documentation, markdown_path)
534
+ st.session_state.markdown_generated = True
535
+ st.success("Markdown Available in the 'Saved Documentation' Page")
536
+ st.rerun()
537
  except Exception as e:
538
  st.error(f"Failed to generate Markdown file: {e}")
539
+ else:
540
+ try:
541
+ with open(markdown_path, "rb") as markdown_file:
542
+ st.download_button(
543
+ label="Download Markdown File",
544
+ data=markdown_file.read(),
545
+ file_name=f"{st.session_state.current_project}_Documentation.md",
546
+ mime="text/markdown",
547
+ )
548
+ except FileNotFoundError:
549
+ st.error("Markdown file not found. Try generating it again.")
550
 
551
 
552