RAHULJUNEJA33 commited on
Commit
c6d0cfb
Β·
verified Β·
1 Parent(s): 007aac0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -61,7 +61,6 @@ def generate_functional_requirements(topic):
61
  if generator is None:
62
  return "Error: Model failed to load."
63
 
64
- # Structured Sections for Detailed Document
65
  sections = [
66
  "Introduction: Overview, Purpose, Intended Users",
67
  "Scope: System Description, Key Functionalities",
@@ -75,18 +74,22 @@ def generate_functional_requirements(topic):
75
  "Conclusion: Summary, Future Enhancements"
76
  ]
77
 
78
- document = "" # Initialize empty document
79
-
80
- # Generate Detailed Content for Each Section
81
  for section in sections:
82
  prompt = f"Generate a **detailed and structured** section on '{section}' for **{topic}** in banking."
83
 
84
- for _ in range(3): # Generate more content for each section
85
  output = generator(prompt, max_length=2048, do_sample=True, temperature=0.7)
86
-
87
- if output and "generated_text" in output[0]:
88
- document += f"### {section}\n\n" + output[0]['generated_text'] + "\n\n"
 
 
 
 
 
89
  else:
 
90
  return "Error: Model failed to generate text."
91
 
92
  return document
@@ -99,22 +102,31 @@ def save_to_pdf(content, filename):
99
  text = c.beginText(40, 750)
100
  text.setLeading(14)
101
 
 
 
 
102
  for line in content.split("\n"):
103
- if "### " in line: # Section Header - Start New Page
104
- c.showPage()
105
- text = c.beginText(40, 750)
106
- text.setLeading(14)
107
- c.setFont("Helvetica-Bold", 12)
 
108
 
109
  text.textLine(line)
 
110
 
111
- if text.getY() < 50: # Handle page overflow
112
  c.drawText(text)
113
  c.showPage()
114
  text = c.beginText(40, 750)
115
  text.setLeading(14)
116
-
117
- c.drawText(text)
 
 
 
 
118
  c.save()
119
 
120
  # πŸ“Œ Streamlit UI
@@ -132,16 +144,19 @@ def main():
132
  if st.button("Generate Functional Requirement Document"):
133
  with st.spinner("Generating... This may take a while."):
134
  content = generate_functional_requirements(topic)
135
-
136
  if "Error" in content:
137
  st.error(content)
138
  else:
 
 
 
139
  filename = "functional_requirement.pdf"
140
  save_to_pdf(content, filename)
141
 
142
  st.success("βœ… Document Generated!")
143
  st.download_button("πŸ“₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf")
144
- os.remove(filename) # Cleanup file after download
145
 
146
  if __name__ == "__main__":
147
  main()
 
61
  if generator is None:
62
  return "Error: Model failed to load."
63
 
 
64
  sections = [
65
  "Introduction: Overview, Purpose, Intended Users",
66
  "Scope: System Description, Key Functionalities",
 
74
  "Conclusion: Summary, Future Enhancements"
75
  ]
76
 
77
+ document = ""
 
 
78
  for section in sections:
79
  prompt = f"Generate a **detailed and structured** section on '{section}' for **{topic}** in banking."
80
 
81
+ for _ in range(3):
82
  output = generator(prompt, max_length=2048, do_sample=True, temperature=0.7)
83
+
84
+ if output:
85
+ st.write("Generated Text:", output) # Debugging: Check if text is generated
86
+ if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
87
+ document += f"### {section}\n\n" + output[0]["generated_text"] + "\n\n"
88
+ else:
89
+ st.error("Error: Model output format is incorrect.")
90
+ return "Error: Model failed to generate text."
91
  else:
92
+ st.error("Error: No output from the model.")
93
  return "Error: Model failed to generate text."
94
 
95
  return document
 
102
  text = c.beginText(40, 750)
103
  text.setLeading(14)
104
 
105
+ page_count = 1 # Track pages to avoid blank PDFs
106
+ content_written = False # Ensure at least some content is written
107
+
108
  for line in content.split("\n"):
109
+ if "### " in line:
110
+ if content_written:
111
+ c.showPage()
112
+ text = c.beginText(40, 750)
113
+ text.setLeading(14)
114
+ c.setFont("Helvetica-Bold", 12)
115
 
116
  text.textLine(line)
117
+ content_written = True
118
 
119
+ if text.getY() < 50:
120
  c.drawText(text)
121
  c.showPage()
122
  text = c.beginText(40, 750)
123
  text.setLeading(14)
124
+
125
+ if content_written:
126
+ c.drawText(text)
127
+ else:
128
+ st.error("Error: No content was written to the PDF.")
129
+
130
  c.save()
131
 
132
  # πŸ“Œ Streamlit UI
 
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
+ # Show document before saving to PDF (Debugging)
152
+ st.text_area("Generated Document Preview", content, height=400)
153
+
154
  filename = "functional_requirement.pdf"
155
  save_to_pdf(content, filename)
156
 
157
  st.success("βœ… Document Generated!")
158
  st.download_button("πŸ“₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf")
159
+ os.remove(filename)
160
 
161
  if __name__ == "__main__":
162
  main()