Update app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,10 @@ from fpdf import FPDF
|
|
| 22 |
from dotenv import load_dotenv
|
| 23 |
from retrying import retry
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Declare the exa search API
|
| 26 |
exa = Exa(api_key=os.getenv("EXA_API_KEY"))
|
| 27 |
|
|
@@ -66,7 +70,6 @@ def get_docx_bytes(doc):
|
|
| 66 |
|
| 67 |
# Generic function to dynamically call report generation functions
|
| 68 |
def call_dynamic_function(function_name, data):
|
| 69 |
-
# Use globals to call the function by name
|
| 70 |
function = globals()[function_name]
|
| 71 |
return function(data)
|
| 72 |
|
|
@@ -83,28 +86,50 @@ def collect_basic_info():
|
|
| 83 |
|
| 84 |
# Create data dictionary by dynamically generating input fields
|
| 85 |
data = {field_key: st.text_area(field_label) for field_key, field_label in report_fields}
|
| 86 |
-
|
| 87 |
-
#
|
| 88 |
section_to_generate = st.selectbox("Choose section to generate", [section[0] for section in report_sections])
|
| 89 |
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
# Run the main function
|
| 110 |
-
collect_basic_info()
|
|
|
|
| 22 |
from dotenv import load_dotenv
|
| 23 |
from retrying import retry
|
| 24 |
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
# Declare the exa search API
|
| 30 |
exa = Exa(api_key=os.getenv("EXA_API_KEY"))
|
| 31 |
|
|
|
|
| 70 |
|
| 71 |
# Generic function to dynamically call report generation functions
|
| 72 |
def call_dynamic_function(function_name, data):
|
|
|
|
| 73 |
function = globals()[function_name]
|
| 74 |
return function(data)
|
| 75 |
|
|
|
|
| 86 |
|
| 87 |
# Create data dictionary by dynamically generating input fields
|
| 88 |
data = {field_key: st.text_area(field_label) for field_key, field_label in report_fields}
|
| 89 |
+
|
| 90 |
+
# Add an option to generate all sections
|
| 91 |
section_to_generate = st.selectbox("Choose section to generate", [section[0] for section in report_sections])
|
| 92 |
|
| 93 |
+
if section_to_generate == "All Sections":
|
| 94 |
+
if st.button('Generate All Sections'):
|
| 95 |
+
# Generate all sections content at once
|
| 96 |
+
all_sections_content = generate_all_sections(data)
|
| 97 |
+
|
| 98 |
+
# Create a new document for all sections combined
|
| 99 |
+
combined_doc = create_document()
|
| 100 |
+
|
| 101 |
+
for section_name, section_content in all_sections_content.items():
|
| 102 |
+
st.subheader(section_name)
|
| 103 |
+
st.write(section_content)
|
| 104 |
+
|
| 105 |
+
# Add each section's content to the combined document
|
| 106 |
+
combined_doc = add_section_to_doc(combined_doc, section_name, section_content)
|
| 107 |
+
|
| 108 |
+
# Get bytes for the combined document and create a download button
|
| 109 |
+
combined_doc_bytes = get_docx_bytes(combined_doc)
|
| 110 |
+
sanitized_topic = sanitize_filename(data["research_topic"])
|
| 111 |
+
filename = f"{report_type}_{sanitized_topic}_all_sections.docx"
|
| 112 |
+
st.download_button(f"Download All Sections as DOCX", combined_doc_bytes, filename)
|
| 113 |
+
|
| 114 |
+
else:
|
| 115 |
+
if st.button('Generate Report Section'):
|
| 116 |
+
# Dynamically get the function name and call it for individual sections
|
| 117 |
+
section_function_name = dict(report_sections)[section_to_generate]
|
| 118 |
+
update_session_tracker(section_to_generate)
|
| 119 |
|
| 120 |
+
# Generate content and display it
|
| 121 |
+
section_content = call_dynamic_function(section_function_name, data)
|
| 122 |
+
st.subheader(section_to_generate)
|
| 123 |
+
st.write(section_content)
|
| 124 |
|
| 125 |
+
# Create and download the document for this specific section only
|
| 126 |
+
doc = create_document()
|
| 127 |
+
doc = add_section_to_doc(doc, section_to_generate, section_content)
|
| 128 |
+
doc_bytes = get_docx_bytes(doc)
|
| 129 |
|
| 130 |
+
sanitized_topic = sanitize_filename(data["research_topic"])
|
| 131 |
+
filename = f"{report_type}_{sanitized_topic}.docx"
|
| 132 |
+
st.download_button(f"Download {section_to_generate} as DOCX", doc_bytes, filename)
|
| 133 |
|
| 134 |
# Run the main function
|
| 135 |
+
collect_basic_info()
|