Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from reportlab.lib.pagesizes import letter
|
| 4 |
+
from reportlab.pdfgen import canvas
|
| 5 |
+
from reportlab.lib import colors
|
| 6 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 7 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Load Hugging Face Token (Ensure it's set in Env Variables)
|
| 11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
# β
Optimized Model (Flan-T5 for Lower Memory Usage)
|
| 14 |
+
MODEL_NAME = "google/flan-t5-large"
|
| 15 |
+
|
| 16 |
+
# π Load Model Efficiently (Avoid Reloading)
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_model():
|
| 19 |
+
try:
|
| 20 |
+
return pipeline("text2text-generation", model=MODEL_NAME, token=HF_TOKEN)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"β Error loading model: {str(e)}")
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
# Load once and reuse
|
| 26 |
+
generator = load_model()
|
| 27 |
+
|
| 28 |
+
# π Function to Generate Functional Requirement Document
|
| 29 |
+
def generate_functional_requirements(topic):
|
| 30 |
+
if generator is None:
|
| 31 |
+
return "Error: Model failed to load."
|
| 32 |
+
|
| 33 |
+
sections = {
|
| 34 |
+
"Introduction": [
|
| 35 |
+
"Overview and Background",
|
| 36 |
+
"Purpose of the System",
|
| 37 |
+
"Intended Users and Audience"
|
| 38 |
+
],
|
| 39 |
+
"Scope": [
|
| 40 |
+
"General System Description",
|
| 41 |
+
"Key Functionalities & Modules",
|
| 42 |
+
"Out of Scope Features"
|
| 43 |
+
],
|
| 44 |
+
"Functional Specifications": [
|
| 45 |
+
"User Roles & Access Levels",
|
| 46 |
+
"Main Features and Use Cases",
|
| 47 |
+
"Transaction Handling & Workflows"
|
| 48 |
+
],
|
| 49 |
+
"Security & Compliance": [
|
| 50 |
+
"Regulatory Requirements",
|
| 51 |
+
"Data Protection & Encryption",
|
| 52 |
+
"Fraud Detection Mechanisms"
|
| 53 |
+
],
|
| 54 |
+
"User Interface": [
|
| 55 |
+
"Wireframes and UI/UX Considerations",
|
| 56 |
+
"Navigation & Accessibility Features"
|
| 57 |
+
],
|
| 58 |
+
"System Architecture": [
|
| 59 |
+
"High-Level Architecture",
|
| 60 |
+
"Technology Stack Used",
|
| 61 |
+
"API Integrations"
|
| 62 |
+
],
|
| 63 |
+
"Performance & Scalability": [
|
| 64 |
+
"Expected Load & System Throughput",
|
| 65 |
+
"Response Time Expectations",
|
| 66 |
+
"Scalability Strategies"
|
| 67 |
+
],
|
| 68 |
+
"Data Management": [
|
| 69 |
+
"Data Flow and Storage",
|
| 70 |
+
"Backup & Disaster Recovery Strategies"
|
| 71 |
+
],
|
| 72 |
+
"Future Enhancements": [
|
| 73 |
+
"Potential Feature Expansions",
|
| 74 |
+
"Long-Term Roadmap",
|
| 75 |
+
"Next Steps in Development"
|
| 76 |
+
]
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
document = [] # Store paragraphs in a structured way
|
| 80 |
+
styles = getSampleStyleSheet()
|
| 81 |
+
|
| 82 |
+
for section, subsections in sections.items():
|
| 83 |
+
document.append(Paragraph(f"<b>{section}</b>", styles['Title']))
|
| 84 |
+
document.append(Spacer(1, 10))
|
| 85 |
+
|
| 86 |
+
for subsection in subsections:
|
| 87 |
+
prompt = f"Write a **detailed 500-word section** on '{subsection}' for the topic '{topic}' in banking. Provide structured paragraphs with examples."
|
| 88 |
+
|
| 89 |
+
for _ in range(2): # Increase iterations per subsection
|
| 90 |
+
output = generator(prompt, max_length=2048, do_sample=True, temperature=0.7)
|
| 91 |
+
|
| 92 |
+
if output and isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
|
| 93 |
+
document.append(Paragraph(f"<b>{subsection}</b>", styles['Heading2']))
|
| 94 |
+
document.append(Spacer(1, 6))
|
| 95 |
+
document.append(Paragraph(output[0]["generated_text"], styles['Normal']))
|
| 96 |
+
document.append(Spacer(1, 10))
|
| 97 |
+
else:
|
| 98 |
+
return "Error: Model failed to generate text."
|
| 99 |
+
|
| 100 |
+
document.append(PageBreak()) # Add a page break after each major section
|
| 101 |
+
|
| 102 |
+
return document
|
| 103 |
+
|
| 104 |
+
# π Function to Save Generated Content as PDF
|
| 105 |
+
def save_to_pdf(content, filename):
|
| 106 |
+
if not content:
|
| 107 |
+
st.error("β Error: No content available to write to the PDF.")
|
| 108 |
+
return
|
| 109 |
+
|
| 110 |
+
doc = SimpleDocTemplate(filename, pagesize=letter)
|
| 111 |
+
doc.build(content)
|
| 112 |
+
|
| 113 |
+
# π Streamlit UI
|
| 114 |
+
def main():
|
| 115 |
+
st.title("π AI-Powered Functional Requirement Generator for Banking")
|
| 116 |
+
|
| 117 |
+
banking_topics = [
|
| 118 |
+
"Core Banking System", "Loan Management System", "Payment Processing Gateway",
|
| 119 |
+
"Risk and Fraud Detection", "Regulatory Compliance Management", "Digital Banking APIs",
|
| 120 |
+
"Customer Onboarding & KYC", "Treasury Management", "Wealth & Portfolio Management"
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
topic = st.selectbox("Select a Banking Functional Requirement Topic", banking_topics)
|
| 124 |
+
|
| 125 |
+
if st.button("Generate Functional Requirement Document"):
|
| 126 |
+
with st.spinner("Generating... This may take a while."):
|
| 127 |
+
content = generate_functional_requirements(topic)
|
| 128 |
+
|
| 129 |
+
if isinstance(content, str) and "Error" in content:
|
| 130 |
+
st.error(content)
|
| 131 |
+
else:
|
| 132 |
+
filename = "functional_requirement.pdf"
|
| 133 |
+
save_to_pdf(content, filename)
|
| 134 |
+
|
| 135 |
+
st.success("β
Document Generated Successfully!")
|
| 136 |
+
st.download_button("π₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf")
|
| 137 |
+
os.remove(filename) # Cleanup after download
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
main()
|