Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,45 @@ import os
|
|
| 2 |
import openai
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from docx import Document
|
| 5 |
-
from docx.shared import Inches
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
import streamlit as st
|
| 8 |
-
from openai import OpenAI
|
| 9 |
|
| 10 |
# Initialize OpenAI client
|
| 11 |
client = OpenAI(api_key=st.secrets.get("OPENAI_API_KEY"))
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def generate_text(prompt, role="user"):
|
| 20 |
-
"""Generate text using the OpenAI API (v1.0+ compatible)"""
|
| 21 |
try:
|
| 22 |
response = client.chat.completions.create(
|
| 23 |
-
model="gpt-4-1106-preview",
|
| 24 |
messages=[
|
| 25 |
{"role": "system", "content": "You are an expert banking analyst."},
|
| 26 |
{"role": role, "content": prompt}
|
|
@@ -29,134 +49,157 @@ def generate_text(prompt, role="user"):
|
|
| 29 |
return response.choices[0].message.content
|
| 30 |
except Exception as e:
|
| 31 |
st.error(f"Error generating text: {e}")
|
| 32 |
-
return "(*
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
doc = Document()
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
doc.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
doc.add_heading(
|
| 45 |
-
|
| 46 |
-
"1.
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
doc.
|
| 72 |
-
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import openai
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from docx import Document
|
| 5 |
+
from docx.shared import Inches, Pt
|
| 6 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
| 7 |
from datetime import datetime
|
| 8 |
import streamlit as st
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
# Initialize OpenAI client
|
| 12 |
client = OpenAI(api_key=st.secrets.get("OPENAI_API_KEY"))
|
| 13 |
|
| 14 |
+
# Custom CSS for styling
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.stTextInput>div>div>input, .stTextArea>div>div>textarea {
|
| 18 |
+
background-color: #f0f2f6;
|
| 19 |
+
border-radius: 5px;
|
| 20 |
+
}
|
| 21 |
+
.stButton>button {
|
| 22 |
+
background-color: #4CAF50;
|
| 23 |
+
color: white;
|
| 24 |
+
padding: 10px 24px;
|
| 25 |
+
border-radius: 5px;
|
| 26 |
+
border: none;
|
| 27 |
+
}
|
| 28 |
+
.stButton>button:hover {
|
| 29 |
+
background-color: #45a049;
|
| 30 |
+
}
|
| 31 |
+
</style>
|
| 32 |
+
""", unsafe_allow_html=True)
|
| 33 |
+
|
| 34 |
+
def init_session_state():
|
| 35 |
+
if 'current_step' not in st.session_state:
|
| 36 |
+
st.session_state.current_step = 1
|
| 37 |
+
if 'user_inputs' not in st.session_state:
|
| 38 |
+
st.session_state.user_inputs = {}
|
| 39 |
|
| 40 |
def generate_text(prompt, role="user"):
|
|
|
|
| 41 |
try:
|
| 42 |
response = client.chat.completions.create(
|
| 43 |
+
model="gpt-4-1106-preview",
|
| 44 |
messages=[
|
| 45 |
{"role": "system", "content": "You are an expert banking analyst."},
|
| 46 |
{"role": role, "content": prompt}
|
|
|
|
| 49 |
return response.choices[0].message.content
|
| 50 |
except Exception as e:
|
| 51 |
st.error(f"Error generating text: {e}")
|
| 52 |
+
return "(*Content generation failed. Please fill manually.*)"
|
| 53 |
+
|
| 54 |
+
def create_fsd_document(inputs):
|
| 55 |
+
doc = Document()
|
| 56 |
+
|
| 57 |
+
# Set default font
|
| 58 |
+
style = doc.styles['Normal']
|
| 59 |
+
font = style.font
|
| 60 |
+
font.name = 'Calibri'
|
| 61 |
+
font.size = Pt(11)
|
| 62 |
+
|
| 63 |
+
# Title Page
|
| 64 |
+
doc.add_heading('Functional Specification Document (FSD)', 0)
|
| 65 |
+
doc.add_paragraph("Project Name: " + inputs.get('project_name', 'Banking System'))
|
| 66 |
+
doc.add_paragraph(f"Version: {inputs.get('version', '1.0')}")
|
| 67 |
+
doc.add_paragraph(f"Prepared By: {inputs.get('author', 'Your Name')}")
|
| 68 |
+
doc.add_paragraph(f"Date: {datetime.now().strftime('%d/%m/%Y')}")
|
| 69 |
+
doc.add_page_break()
|
| 70 |
+
|
| 71 |
+
# Table of Contents
|
| 72 |
+
doc.add_heading("Table of Contents", 1)
|
| 73 |
+
toc = [
|
| 74 |
+
"1. Introduction",
|
| 75 |
+
"2. System Overview",
|
| 76 |
+
"3. Functional Requirements",
|
| 77 |
+
"4. Non-Functional Requirements",
|
| 78 |
+
"5. Security & Compliance",
|
| 79 |
+
"6. System Architecture",
|
| 80 |
+
"7. Process Flows",
|
| 81 |
+
"8. Data Requirements",
|
| 82 |
+
"9. User Interface",
|
| 83 |
+
"10. Reporting",
|
| 84 |
+
"11. Error Handling",
|
| 85 |
+
"12. Assumptions",
|
| 86 |
+
"13. Dependencies",
|
| 87 |
+
"14. Glossary"
|
| 88 |
+
]
|
| 89 |
+
for item in toc:
|
| 90 |
+
doc.add_paragraph(item, style='ListBullet')
|
| 91 |
+
doc.add_page_break()
|
| 92 |
+
|
| 93 |
+
# Detailed Sections
|
| 94 |
+
sections = {
|
| 95 |
+
"1. Introduction": f"Write a comprehensive introduction for {inputs['project_name']} covering: {inputs['brief_description']}",
|
| 96 |
+
"2. System Overview": f"Create detailed system overview for {inputs['project_name']} including: {inputs['system_components']}",
|
| 97 |
+
"3. Functional Requirements": f"Expand these functional requirements into detailed specifications: {inputs['functional_reqs']}",
|
| 98 |
+
"4. Non-Functional Requirements": f"Create non-functional requirements for: {inputs['non_functional_reqs']}",
|
| 99 |
+
"5. Security & Compliance": f"Detail security measures and compliance requirements for: {inputs['security_needs']}",
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
for header, prompt in sections.items():
|
| 103 |
+
doc.add_heading(header, level=1)
|
| 104 |
+
content = generate_text(prompt)
|
| 105 |
+
for paragraph in content.split('\n'):
|
| 106 |
+
if paragraph.strip():
|
| 107 |
+
p = doc.add_paragraph(paragraph)
|
| 108 |
+
p.paragraph_format.space_after = Pt(6)
|
| 109 |
+
|
| 110 |
+
# Add detailed subsections
|
| 111 |
+
if header == "3. Functional Requirements":
|
| 112 |
+
doc.add_heading("3.1 Detailed Use Cases", level=2)
|
| 113 |
+
use_cases = generate_text(f"Create detailed use cases for: {inputs['functional_reqs']}")
|
| 114 |
+
doc.add_paragraph(use_cases)
|
| 115 |
+
|
| 116 |
+
doc.add_page_break()
|
| 117 |
+
|
| 118 |
+
# Add diagrams
|
| 119 |
+
doc.add_heading("6. System Architecture", 1)
|
| 120 |
+
fig = create_architecture_diagram()
|
| 121 |
+
diagram_path = "system_architecture.png"
|
| 122 |
+
fig.savefig(diagram_path)
|
| 123 |
+
doc.add_picture(diagram_path, width=Inches(6))
|
| 124 |
+
|
| 125 |
+
return doc
|
| 126 |
+
|
| 127 |
+
def create_architecture_diagram():
|
| 128 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 129 |
+
components = [
|
| 130 |
+
("User Interface", 0.5, 0.9),
|
| 131 |
+
("Application Layer", 0.3, 0.7),
|
| 132 |
+
("Business Logic", 0.7, 0.7),
|
| 133 |
+
("Database", 0.5, 0.5),
|
| 134 |
+
("External APIs", 0.5, 0.3)
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
for text, x, y in components:
|
| 138 |
+
ax.text(x, y, text,
|
| 139 |
+
ha='center', va='center',
|
| 140 |
+
bbox=dict(facecolor='lightblue', edgecolor='black', boxstyle='round,pad=0.5'),
|
| 141 |
+
fontsize=10)
|
| 142 |
+
|
| 143 |
+
ax.set_xlim(0, 1)
|
| 144 |
+
ax.set_ylim(0, 1)
|
| 145 |
+
ax.axis('off')
|
| 146 |
+
return fig
|
| 147 |
+
|
| 148 |
+
def main():
|
| 149 |
+
init_session_state()
|
| 150 |
+
st.title("FSD Document Generator")
|
| 151 |
+
st.subheader("Create Custom Functional Specification Documents")
|
| 152 |
+
|
| 153 |
+
if st.session_state.current_step == 1:
|
| 154 |
+
with st.form("basic_info"):
|
| 155 |
+
st.header("Step 1: Basic Information")
|
| 156 |
+
st.session_state.user_inputs['project_name'] = st.text_input("Project Name")
|
| 157 |
+
st.session_state.user_inputs['brief_description'] = st.text_area("Brief Description", height=150)
|
| 158 |
+
if st.form_submit_button("Next →"):
|
| 159 |
+
st.session_state.current_step = 2
|
| 160 |
+
st.rerun()
|
| 161 |
+
|
| 162 |
+
elif st.session_state.current_step == 2:
|
| 163 |
+
with st.form("detailed_info"):
|
| 164 |
+
st.header("Step 2: Detailed Requirements")
|
| 165 |
+
st.session_state.user_inputs['functional_reqs'] = st.text_area(
|
| 166 |
+
"Functional Requirements (comma-separated)",
|
| 167 |
+
height=100,
|
| 168 |
+
value="User authentication, Transaction monitoring, Report generation"
|
| 169 |
+
)
|
| 170 |
+
st.session_state.user_inputs['non_functional_reqs'] = st.text_input(
|
| 171 |
+
"Non-Functional Requirements",
|
| 172 |
+
value="High availability, GDPR compliance"
|
| 173 |
+
)
|
| 174 |
+
st.session_state.user_inputs['security_needs'] = st.text_input(
|
| 175 |
+
"Security Requirements",
|
| 176 |
+
value="Data encryption, Role-based access control"
|
| 177 |
+
)
|
| 178 |
+
if st.form_submit_button("Generate FSD"):
|
| 179 |
+
st.session_state.current_step = 3
|
| 180 |
+
st.rerun()
|
| 181 |
+
|
| 182 |
+
elif st.session_state.current_step == 3:
|
| 183 |
+
st.header("Step 3: Generated Document")
|
| 184 |
+
with st.spinner("Generating 10+ page FSD document..."):
|
| 185 |
+
doc = create_fsd_document(st.session_state.user_inputs)
|
| 186 |
+
output_folder = "generated_docs"
|
| 187 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 188 |
+
doc_path = os.path.join(output_folder, "FSD_Document.docx")
|
| 189 |
+
doc.save(doc_path)
|
| 190 |
+
|
| 191 |
+
with open(doc_path, "rb") as file:
|
| 192 |
+
st.download_button(
|
| 193 |
+
label="Download FSD Document",
|
| 194 |
+
data=file,
|
| 195 |
+
file_name="FSD_Document.docx",
|
| 196 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
if st.button("Start New Document"):
|
| 200 |
+
st.session_state.current_step = 1
|
| 201 |
+
st.session_state.user_inputs = {}
|
| 202 |
+
st.rerun()
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
main()
|