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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -38
app.py CHANGED
@@ -37,6 +37,7 @@ from transformers import pipeline
37
  from reportlab.lib.pagesizes import letter
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")
@@ -78,54 +79,43 @@ def generate_functional_requirements(topic):
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
96
 
97
  # πŸ“Œ Function to Save Generated Content as PDF
98
  def save_to_pdf(content, filename):
 
 
 
 
99
  c = canvas.Canvas(filename, pagesize=letter)
100
  c.setFont("Helvetica", 10)
101
-
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
 
@@ -148,15 +138,15 @@ def main():
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()
 
37
  from reportlab.lib.pagesizes import letter
38
  from reportlab.pdfgen import canvas
39
  import os
40
+ import textwrap
41
 
42
  # Load Hugging Face Token (Ensure it's set in Env Variables)
43
  HF_TOKEN = os.getenv("HF_TOKEN")
 
79
  for section in sections:
80
  prompt = f"Generate a **detailed and structured** section on '{section}' for **{topic}** in banking."
81
 
82
+ for _ in range(3): # Ensure enough content is generated
83
  output = generator(prompt, max_length=2048, do_sample=True, temperature=0.7)
84
 
85
+ if output and isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
86
+ document += f"\n\n### {section}\n\n" + output[0]["generated_text"] + "\n\n"
 
 
 
 
 
87
  else:
 
88
  return "Error: Model failed to generate text."
89
 
90
  return document
91
 
92
  # πŸ“Œ Function to Save Generated Content as PDF
93
  def save_to_pdf(content, filename):
94
+ if not content.strip():
95
+ st.error("❌ Error: No content available to write to the PDF.")
96
+ return
97
+
98
  c = canvas.Canvas(filename, pagesize=letter)
99
  c.setFont("Helvetica", 10)
 
 
 
 
 
 
100
 
101
+ # Handling text wrapping & new pages correctly
102
+ max_width = 80 # Approximate max characters per line
103
+ lines_per_page = 50 # Approximate lines per page
104
+ y_position = 750 # Start position for text
 
 
 
105
 
106
+ paragraphs = content.split("\n\n") # Preserve paragraph structure
107
+
108
+ for para in paragraphs:
109
+ wrapped_lines = textwrap.wrap(para, max_width)
110
+
111
+ for line in wrapped_lines:
112
+ if y_position < 50: # If near bottom, create a new page
113
+ c.showPage()
114
+ c.setFont("Helvetica", 10)
115
+ y_position = 750 # Reset text position
116
 
117
+ c.drawString(40, y_position, line)
118
+ y_position -= 14 # Move to next line
 
 
119
 
120
  c.save()
121
 
 
138
  if "Error" in content:
139
  st.error(content)
140
  else:
141
+ # Show document preview before saving to PDF (for debugging)
142
+ st.text_area("Generated Document Preview", content[:5000], height=400)
143
 
144
  filename = "functional_requirement.pdf"
145
  save_to_pdf(content, filename)
146
 
147
+ st.success("βœ… Document Generated Successfully!")
148
  st.download_button("πŸ“₯ Download PDF", data=open(filename, "rb"), file_name=filename, mime="application/pdf")
149
+ os.remove(filename) # Cleanup after download
150
 
151
  if __name__ == "__main__":
152
  main()