|
import streamlit as st |
|
from transformers import pipeline |
|
from reportlab.lib.pagesizes import letter |
|
from reportlab.pdfgen import canvas |
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
|
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak |
|
import os |
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
|
|
|
MODEL_NAME = "google/flan-t5-large" |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
try: |
|
return pipeline("text2text-generation", model=MODEL_NAME, token=HF_TOKEN) |
|
except Exception as e: |
|
st.error(f"β Error loading model: {str(e)}") |
|
return None |
|
|
|
|
|
generator = load_model() |
|
|
|
|
|
def generate_functional_requirements(topic): |
|
if generator is None: |
|
return "Error: Model failed to load." |
|
|
|
sections = { |
|
"Introduction": [ |
|
"Overview and Purpose", |
|
"Intended Users" |
|
], |
|
"Scope": [ |
|
"System Description", |
|
"Key Functionalities" |
|
], |
|
"Functional Specifications": [ |
|
"User Roles", |
|
"Core Features" |
|
], |
|
"Security & Compliance": [ |
|
"Regulatory Requirements", |
|
"Data Protection" |
|
], |
|
"Future Enhancements": [ |
|
"Potential Feature Expansions", |
|
"Roadmap & Next Steps" |
|
] |
|
} |
|
|
|
document = [] |
|
styles = getSampleStyleSheet() |
|
|
|
for section, subsections in sections.items(): |
|
document.append(Paragraph(f"<b>{section}</b>", styles['Title'])) |
|
document.append(Spacer(1, 10)) |
|
|
|
for subsection in subsections: |
|
prompt = f"Write a **detailed 300-word section** on '{subsection}' for the topic '{topic}' in banking. Provide structured paragraphs with examples." |
|
|
|
output = generator(prompt, max_length=1024, do_sample=True, temperature=0.7) |
|
|
|
if output and isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]: |
|
document.append(Paragraph(f"<b>{subsection}</b>", styles['Heading2'])) |
|
document.append(Spacer(1, 6)) |
|
document.append(Paragraph(output[0]["generated_text"], styles['Normal'])) |
|
document.append(Spacer(1, 10)) |
|
else: |
|
return "Error: Model failed to generate text." |
|
|
|
document.append(PageBreak()) |
|
|
|
return document |
|
|
|
|
|
def save_to_pdf(content, filename): |
|
if not content: |
|
st.error("β Error: No content available to write to the PDF.") |
|
return |
|
|
|
doc = SimpleDocTemplate(filename, pagesize=letter) |
|
doc.build(content) |
|
|
|
|
|
def main(): |
|
st.title("π AI-Powered Functional Requirement Generator for Banking") |
|
|
|
banking_topics = [ |
|
"Core Banking System", "Loan Management System", "Payment Processing Gateway", |
|
"Risk and Fraud Detection", "Regulatory Compliance Management", "Digital Banking APIs", |
|
"Customer Onboarding & KYC", "Treasury Management", "Wealth & Portfolio Management" |
|
] |
|
|
|
topic = st.selectbox("Select a Banking Functional Requirement Topic", banking_topics) |
|
|
|
if st.button("Generate Functional Requirement Document"): |
|
with st.spinner("Generating... This may take a while."): |
|
content = generate_functional_requirements(topic) |
|
|
|
if isinstance(content, str) and "Error" in content: |
|
st.error(content) |
|
else: |
|
filename = "functional_requirement.pdf" |
|
save_to_pdf(content, filename) |
|
|
|
st.success("β
Document Generated Successfully!") |
|
st.download_button("π₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf") |
|
os.remove(filename) |
|
|
|
if __name__ == "__main__": |
|
main() |