Update app.py
Browse files
app.py
CHANGED
@@ -27,7 +27,7 @@
|
|
27 |
# - **Hugging Face Model**: "sentence-transformers/all-MiniLM-L6-v2"
|
28 |
|
29 |
# Choose your model from the above options:
|
30 |
-
MODEL_NAME = "
|
31 |
|
32 |
# ---------------------------------------------------------------------------
|
33 |
# Code Below to Load, Generate, and Save Functional Requirement Documents
|
@@ -38,120 +38,96 @@ from reportlab.lib.pagesizes import letter
|
|
38 |
from reportlab.pdfgen import canvas
|
39 |
import os
|
40 |
|
41 |
-
# Load Hugging Face Token
|
42 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
43 |
|
44 |
-
#
|
45 |
-
MODEL_NAME = "
|
46 |
|
47 |
-
# π Load Model Efficiently (
|
48 |
@st.cache_resource
|
49 |
def load_model():
|
50 |
try:
|
51 |
-
return pipeline("
|
52 |
except Exception as e:
|
53 |
st.error(f"β Error loading model: {str(e)}")
|
54 |
return None
|
55 |
|
56 |
-
|
|
|
57 |
|
58 |
# π Function to Generate Functional Requirement Document
|
59 |
def generate_functional_requirements(topic):
|
60 |
if generator is None:
|
61 |
return "Error: Model failed to load."
|
62 |
|
63 |
-
# Structured Prompt for Better Document Output
|
64 |
prompt = f"""
|
65 |
-
Generate a detailed functional requirements document for {topic} in the banking sector.
|
66 |
-
|
67 |
|
68 |
-
1οΈβ£ **Introduction
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
2οΈβ£ **Scope**
|
74 |
-
- System Description
|
75 |
-
- Key Functionalities
|
76 |
-
|
77 |
-
3οΈβ£ **Functional Specifications**
|
78 |
-
- Core Features
|
79 |
-
- User Roles & Permissions
|
80 |
-
- Transaction Processing
|
81 |
-
|
82 |
-
4οΈβ£ **System Features**
|
83 |
-
- Security & Compliance
|
84 |
-
- Performance Metrics
|
85 |
-
|
86 |
-
5οΈβ£ **Regulatory & Compliance**
|
87 |
-
- Central Bank Regulations
|
88 |
-
- Data Protection & Privacy
|
89 |
-
|
90 |
-
6οΈβ£ **Conclusion**
|
91 |
-
- Summary
|
92 |
-
- Future Enhancements
|
93 |
"""
|
94 |
|
95 |
document = "" # Initialize empty document
|
96 |
-
|
97 |
-
for _ in range(
|
98 |
output = generator(prompt, max_length=1024, do_sample=True, temperature=0.7)
|
99 |
-
|
|
|
100 |
document += output[0]['generated_text'] + "\n\n"
|
101 |
else:
|
102 |
return "Error: Model failed to generate text."
|
103 |
-
|
104 |
-
return document
|
105 |
|
106 |
# π Function to Save Generated Content as PDF
|
107 |
def save_to_pdf(content, filename):
|
108 |
c = canvas.Canvas(filename, pagesize=letter)
|
109 |
c.setFont("Helvetica", 10)
|
110 |
|
111 |
-
text = c.beginText(40, 750)
|
112 |
-
text.setLeading(14)
|
113 |
|
114 |
for line in content.split("\n"):
|
115 |
-
text.textLine(line)
|
116 |
-
if text.getY() < 50: #
|
117 |
c.drawText(text)
|
118 |
c.showPage()
|
119 |
text = c.beginText(40, 750)
|
120 |
text.setLeading(14)
|
121 |
-
|
122 |
c.drawText(text)
|
123 |
c.save()
|
124 |
|
125 |
# π Streamlit UI
|
126 |
def main():
|
127 |
st.title("π AI-Powered Functional Requirement Generator for Banking")
|
128 |
-
|
129 |
-
# Dropdown menu for selecting banking topics
|
130 |
banking_topics = [
|
131 |
-
"Core Banking System",
|
132 |
-
"
|
133 |
-
"
|
134 |
-
"Risk and Fraud Detection",
|
135 |
-
"Regulatory Compliance Management",
|
136 |
-
"Digital Banking APIs",
|
137 |
-
"Customer Onboarding & KYC",
|
138 |
-
"Treasury Management",
|
139 |
-
"Wealth & Portfolio Management"
|
140 |
]
|
141 |
|
142 |
topic = st.selectbox("Select a Banking Functional Requirement Topic", banking_topics)
|
143 |
|
144 |
if st.button("Generate Functional Requirement Document"):
|
145 |
with st.spinner("Generating... This may take a while."):
|
146 |
-
content = generate_functional_requirements(topic)
|
147 |
|
148 |
if "Error" in content:
|
149 |
st.error(content)
|
150 |
else:
|
151 |
filename = "functional_requirement.pdf"
|
152 |
-
save_to_pdf(content, filename)
|
153 |
-
|
154 |
-
st.
|
|
|
|
|
155 |
|
156 |
if __name__ == "__main__":
|
157 |
-
main()
|
|
|
27 |
# - **Hugging Face Model**: "sentence-transformers/all-MiniLM-L6-v2"
|
28 |
|
29 |
# Choose your model from the above options:
|
30 |
+
MODEL_NAME = "google/flan-t5-base" # Change this to one of the other models based on your needs.
|
31 |
|
32 |
# ---------------------------------------------------------------------------
|
33 |
# Code Below to Load, Generate, and Save Functional Requirement Documents
|
|
|
38 |
from reportlab.pdfgen import canvas
|
39 |
import os
|
40 |
|
41 |
+
# Load Hugging Face Token (Ensure it's set in Env Variables)
|
42 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
43 |
|
44 |
+
# β
Optimized Model (Flan-T5 for Lower Memory Usage)
|
45 |
+
MODEL_NAME = "google/flan-t5-base"
|
46 |
|
47 |
+
# π Load Model Efficiently (Avoid Reloading)
|
48 |
@st.cache_resource
|
49 |
def load_model():
|
50 |
try:
|
51 |
+
return pipeline("text2text-generation", model=MODEL_NAME, token=HF_TOKEN)
|
52 |
except Exception as e:
|
53 |
st.error(f"β Error loading model: {str(e)}")
|
54 |
return None
|
55 |
|
56 |
+
# Load once and reuse
|
57 |
+
generator = load_model()
|
58 |
|
59 |
# π Function to Generate Functional Requirement Document
|
60 |
def generate_functional_requirements(topic):
|
61 |
if generator is None:
|
62 |
return "Error: Model failed to load."
|
63 |
|
|
|
64 |
prompt = f"""
|
65 |
+
Generate a **detailed functional requirements document** for **{topic}** in the banking sector.
|
66 |
+
Ensure it includes the following sections:
|
67 |
|
68 |
+
1οΈβ£ **Introduction**: Overview, Purpose, Intended Users
|
69 |
+
2οΈβ£ **Scope**: System Description, Key Functionalities
|
70 |
+
3οΈβ£ **Functional Specifications**: Features, User Roles, Transactions
|
71 |
+
4οΈβ£ **Security & Compliance**: Regulations, Data Protection
|
72 |
+
5οΈβ£ **Conclusion**: Summary, Future Enhancements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
"""
|
74 |
|
75 |
document = "" # Initialize empty document
|
76 |
+
|
77 |
+
for _ in range(3): # Generate in chunks (Avoids memory crash)
|
78 |
output = generator(prompt, max_length=1024, do_sample=True, temperature=0.7)
|
79 |
+
|
80 |
+
if output and "generated_text" in output[0]:
|
81 |
document += output[0]['generated_text'] + "\n\n"
|
82 |
else:
|
83 |
return "Error: Model failed to generate text."
|
84 |
+
|
85 |
+
return document
|
86 |
|
87 |
# π Function to Save Generated Content as PDF
|
88 |
def save_to_pdf(content, filename):
|
89 |
c = canvas.Canvas(filename, pagesize=letter)
|
90 |
c.setFont("Helvetica", 10)
|
91 |
|
92 |
+
text = c.beginText(40, 750)
|
93 |
+
text.setLeading(14)
|
94 |
|
95 |
for line in content.split("\n"):
|
96 |
+
text.textLine(line)
|
97 |
+
if text.getY() < 50: # Handle page overflow
|
98 |
c.drawText(text)
|
99 |
c.showPage()
|
100 |
text = c.beginText(40, 750)
|
101 |
text.setLeading(14)
|
102 |
+
|
103 |
c.drawText(text)
|
104 |
c.save()
|
105 |
|
106 |
# π Streamlit UI
|
107 |
def main():
|
108 |
st.title("π AI-Powered Functional Requirement Generator for Banking")
|
109 |
+
|
|
|
110 |
banking_topics = [
|
111 |
+
"Core Banking System", "Loan Management System", "Payment Processing Gateway",
|
112 |
+
"Risk and Fraud Detection", "Regulatory Compliance Management", "Digital Banking APIs",
|
113 |
+
"Customer Onboarding & KYC", "Treasury Management", "Wealth & Portfolio Management"
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
]
|
115 |
|
116 |
topic = st.selectbox("Select a Banking Functional Requirement Topic", banking_topics)
|
117 |
|
118 |
if st.button("Generate Functional Requirement Document"):
|
119 |
with st.spinner("Generating... This may take a while."):
|
120 |
+
content = generate_functional_requirements(topic)
|
121 |
|
122 |
if "Error" in content:
|
123 |
st.error(content)
|
124 |
else:
|
125 |
filename = "functional_requirement.pdf"
|
126 |
+
save_to_pdf(content, filename)
|
127 |
+
|
128 |
+
st.success("β
Document Generated!")
|
129 |
+
st.download_button("π₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf")
|
130 |
+
os.remove(filename) # Cleanup file after download
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
+
main()
|