Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from docx import Document
|
| 3 |
+
import io
|
| 4 |
+
from functions import get_report_fields, get_report_sections # Import the necessary functions
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Initialize session state for tracking progress
|
| 8 |
+
if 'session_tracker' not in st.session_state:
|
| 9 |
+
st.session_state.session_tracker = []
|
| 10 |
+
|
| 11 |
+
# Update session tracker
|
| 12 |
+
def update_session_tracker(section_name):
|
| 13 |
+
if section_name not in st.session_state.session_tracker:
|
| 14 |
+
st.session_state.session_tracker.append(section_name)
|
| 15 |
+
|
| 16 |
+
# Document-related utilities
|
| 17 |
+
def sanitize_filename(filename, max_length=50):
|
| 18 |
+
return re.sub(r'[<>:"/\\|?*]', '', filename[:max_length])
|
| 19 |
+
|
| 20 |
+
def create_document():
|
| 21 |
+
doc = Document()
|
| 22 |
+
doc.add_heading("Generated Report", 0)
|
| 23 |
+
return doc
|
| 24 |
+
|
| 25 |
+
def add_section_to_doc(doc, section_name, section_content):
|
| 26 |
+
doc.add_heading(section_name, level=1)
|
| 27 |
+
doc.add_paragraph(section_content)
|
| 28 |
+
return doc
|
| 29 |
+
|
| 30 |
+
def get_docx_bytes(doc):
|
| 31 |
+
doc_io = io.BytesIO()
|
| 32 |
+
doc.save(doc_io)
|
| 33 |
+
doc_io.seek(0)
|
| 34 |
+
return doc_io
|
| 35 |
+
|
| 36 |
+
# Generic function to dynamically call report generation functions
|
| 37 |
+
def call_dynamic_function(function_name, data):
|
| 38 |
+
# Use globals to call the function by name
|
| 39 |
+
function = globals()[function_name]
|
| 40 |
+
return function(data)
|
| 41 |
+
|
| 42 |
+
# Main info collection and report generation function
|
| 43 |
+
def collect_basic_info():
|
| 44 |
+
st.title("Academic Report Generator")
|
| 45 |
+
|
| 46 |
+
# Select report type
|
| 47 |
+
report_type = st.selectbox("Select report type", ["PhD Proposal", "Research Paper", "Thesis"])
|
| 48 |
+
|
| 49 |
+
# Fetch the dynamic fields and sections from functions.py
|
| 50 |
+
report_fields = get_report_fields(report_type)
|
| 51 |
+
report_sections = get_report_sections(report_type)
|
| 52 |
+
|
| 53 |
+
# Create data dictionary by dynamically generating input fields
|
| 54 |
+
data = {field_key: st.text_area(field_label) for field_key, field_label in report_fields}
|
| 55 |
+
|
| 56 |
+
# Select section to generate
|
| 57 |
+
section_to_generate = st.selectbox("Choose section to generate", [section[0] for section in report_sections])
|
| 58 |
+
|
| 59 |
+
if st.button('Generate Report Section'):
|
| 60 |
+
# Dynamically get the function name and call it
|
| 61 |
+
section_function_name = dict(report_sections)[section_to_generate]
|
| 62 |
+
update_session_tracker(section_to_generate)
|
| 63 |
+
|
| 64 |
+
# Generate content and display it
|
| 65 |
+
section_content = call_dynamic_function(section_function_name, data)
|
| 66 |
+
st.subheader(section_to_generate)
|
| 67 |
+
st.write(section_content)
|
| 68 |
+
|
| 69 |
+
# Create and download the document
|
| 70 |
+
doc = create_document()
|
| 71 |
+
doc = add_section_to_doc(doc, section_to_generate, section_content)
|
| 72 |
+
doc_bytes = get_docx_bytes(doc)
|
| 73 |
+
|
| 74 |
+
sanitized_topic = sanitize_filename(data["research_topic"])
|
| 75 |
+
filename = f"{report_type}_{sanitized_topic}.docx"
|
| 76 |
+
st.download_button(f"Download {section_to_generate} as DOCX", doc_bytes, filename)
|
| 77 |
+
|
| 78 |
+
# Run the main function
|
| 79 |
+
collect_basic_info()
|