RAHULJUNEJA33 commited on
Commit
5ea0d14
Β·
verified Β·
1 Parent(s): ed13e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -61
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 = "mistralai/Mistral-7B-Instruct-v0.1" # 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,120 +38,96 @@ from reportlab.lib.pagesizes import letter
38
  from reportlab.pdfgen import canvas
39
  import os
40
 
41
- # Load Hugging Face Token from Environment (Set this in Hugging Face Spaces Secrets)
42
  HF_TOKEN = os.getenv("HF_TOKEN")
43
 
44
- # πŸ† Recommended Model: Mistral-7B (Lightweight & Open-Source)
45
- MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1"
46
 
47
- # πŸ“Œ Load Model Efficiently (Caching to avoid reloading)
48
  @st.cache_resource
49
  def load_model():
50
  try:
51
- return pipeline("text-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
- generator = load_model() # Load once and reuse
 
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
- The document should follow this structured format:
67
 
68
- 1️⃣ **Introduction**
69
- - Overview
70
- - Purpose
71
- - Intended Users
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(4): # Generate 4 chunks of text
98
  output = generator(prompt, max_length=1024, do_sample=True, temperature=0.7)
99
- if output and len(output) > 0 and "generated_text" in output[0]:
 
100
  document += output[0]['generated_text'] + "\n\n"
101
  else:
102
  return "Error: Model failed to generate text."
103
-
104
- return document # Return final document text
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) # Set starting position for text
112
- text.setLeading(14) # Line spacing
113
 
114
  for line in content.split("\n"):
115
- text.textLine(line) # Add text line by line
116
- if text.getY() < 50: # Start a new page when space runs out
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
- "Loan Management System",
133
- "Payment Processing Gateway",
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) # Generate content
147
 
148
  if "Error" in content:
149
  st.error(content)
150
  else:
151
  filename = "functional_requirement.pdf"
152
- save_to_pdf(content, filename) # Save to PDF
153
- st.success("βœ… Functional Requirement Document Generated!")
154
- st.download_button(label="πŸ“₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf")
 
 
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()