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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -67
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 = "EleutherAI/gpt-neo-2.7B" # 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
@@ -39,82 +39,94 @@ 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") # Token from your Hugging Face account for accessing models.
43
 
44
- # Model Selection: Use GPT-Neo-2.7B for generating functional requirements
45
- MODEL_NAME = "EleutherAI/gpt-neo-2.7B" # Open-source GPT-Neo-2.7B model for longer text generation
46
 
47
- # Load Model - Using Streamlit's caching to improve performance and load model only once
48
  @st.cache_resource
49
  def load_model():
50
  try:
51
- # Initialize the Hugging Face pipeline with the selected model for text generation.
52
  return pipeline("text-generation", model=MODEL_NAME, token=HF_TOKEN)
53
  except Exception as e:
54
- st.error(f"❌ Error loading model: {str(e)}") # Error handling for failed model loading
55
- return None # Return None if model loading fails
56
 
57
- # Initialize the model generator
58
- generator = load_model()
59
 
60
- # Function to generate functional requirement document based on the selected topic
61
  def generate_functional_requirements(topic):
62
  if generator is None:
63
- return "Error: Model not loaded properly." # Return error if the model is not loaded
64
 
65
- # Define the structured prompt for generating functional requirement document
66
  prompt = f"""
67
- Generate a comprehensive functional requirement document for a {topic} in the banking sector.
68
- The document should be structured as follows:
69
 
70
- 1. **Introduction**: Provide an overview of the {topic} and its importance in the banking sector.
71
- 2. **Functional Requirements**: List and describe the key functional requirements that the system should fulfill.
72
- 3. **Non-Functional Requirements**: Discuss the performance, security, scalability, and other non-functional aspects.
73
- 4. **Use Cases**: Provide detailed use cases or scenarios that illustrate how the system will function.
74
- 5. **Data Flow Diagrams (DFD)**: Provide explanations of the system's data flows and processes (optional).
75
- 6. **User Interface (UI) Requirements**: Describe the UI/UX expectations for the system.
76
- 7. **System Architecture**: Explain the high-level architecture of the system and integration with other systems.
77
- 8. **Regulatory Compliance**: Detail the compliance requirements for the system (e.g., GDPR, PCI-DSS).
78
- 9. **Conclusion**: Summarize the document with key takeaways.
79
-
80
- The document should be detailed and cover all sections thoroughly, producing a functional document that is suitable for a technical audience in a banking institution.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  """
82
-
83
- # Generate content using the text generation model
84
- output = generator(prompt, max_length=3000, do_sample=True, temperature=0.7) # Generate long-form content
85
- generated_text = output[0]['generated_text']
86
-
87
- if not generated_text.strip():
88
- return "No content generated. Please try again." # Handle the case where the model generates an empty response
89
-
90
- return generated_text # Return the generated content
91
 
92
- # Function to save generated content as a PDF
 
 
 
 
 
 
 
 
 
 
 
93
  def save_to_pdf(content, filename):
94
- c = canvas.Canvas(filename, pagesize=letter) # Initialize canvas to create a PDF
95
- c.setFont("Helvetica", 10) # Set the font for the PDF
96
 
97
- text = c.beginText(40, 750) # Set the starting position for the text on the PDF
98
- text.setLeading(14) # Set line height for text
99
 
100
- # Ensure content is not too large to fit in one page, splitting into lines
101
- lines = content.split("\n")
102
- for line in lines:
103
- text.textLine(line) # Add the line to the PDF
104
- if text.getY() < 50: # Check if the text has gone below the bottom margin
105
- c.drawText(text) # Draw the current text
106
- c.showPage() # Create a new page in the PDF
107
- text = c.beginText(40, 750) # Reset the text position for the new page
108
- text.setLeading(14) # Reset the line height
109
-
110
- c.drawText(text) # Draw any remaining text
111
- c.save() # Save the PDF file
112
-
113
- # Streamlit UI - User Interface for interacting with the app
114
  def main():
115
- st.title("πŸ“„ AI-Powered Functional Requirement Generator for Banking") # Title for the app
116
 
117
- # Define a list of banking topics that users can select from
118
  banking_topics = [
119
  "Core Banking System",
120
  "Loan Management System",
@@ -127,21 +139,19 @@ def main():
127
  "Wealth & Portfolio Management"
128
  ]
129
 
130
- # Dropdown menu to select a topic
131
  topic = st.selectbox("Select a Banking Functional Requirement Topic", banking_topics)
132
 
133
- # Button to trigger the document generation
134
  if st.button("Generate Functional Requirement Document"):
135
- with st.spinner("Generating... This may take a while."): # Show a loading spinner while generating
136
- content = generate_functional_requirements(topic) # Generate the content based on the selected topic
 
137
  if "Error" in content:
138
- st.error(content) # Show an error message if the content generation fails
139
  else:
140
- st.write(content[:2000]) # Display first 2000 characters for debugging
141
- filename = "functional_requirement.pdf" # Set the filename for the generated PDF
142
- save_to_pdf(content, filename) # Save the content as a PDF file
143
- st.success("βœ… Functional Requirement Document Generated!") # Show success message
144
- st.download_button(label="πŸ“₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf") # Provide a download link for the PDF
145
 
146
  if __name__ == "__main__":
147
- main() # Run the main function to start the app
 
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
 
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",
 
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()