Infinity-1995 commited on
Commit
9bd5bfd
·
verified ·
1 Parent(s): 757eb38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -4,7 +4,11 @@ from groq import Groq
4
  from reportlab.pdfgen import canvas
5
  from reportlab.lib.pagesizes import letter
6
  import tempfile
 
7
  from textwrap import wrap
 
 
 
8
 
9
  # Load API key securely from environment
10
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
@@ -63,42 +67,35 @@ def generate_pdf(itinerary_text):
63
  if not itinerary_text.strip():
64
  return None
65
 
66
- # Create a temporary PDF file
67
- temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
 
 
 
68
 
69
- # Prepare the document template with letter page size
70
- doc = SimpleDocTemplate(temp_pdf.name, pagesize=letter,
71
  rightMargin=40, leftMargin=40,
72
  topMargin=40, bottomMargin=40)
73
 
74
- # Get default stylesheet and customize the Normal style
75
  styles = getSampleStyleSheet()
76
  style = styles["Normal"]
77
- style.fontName = "Courier" # Use monospaced font like your original code
78
  style.fontSize = 10
79
- style.leading = 14 # line height
80
 
81
- # Convert markdown-style bold (**text**) to HTML <b>text</b>
82
  html_text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', itinerary_text)
83
-
84
- # Split the text into paragraphs by newlines
85
  paragraphs = html_text.split('\n')
86
 
87
- # Build the story (list of flowables)
88
  story = []
89
  for para in paragraphs:
90
- # Add Paragraph flowable; empty line if paragraph is empty
91
- if para.strip():
92
- p = Paragraph(para, style)
93
- else:
94
- p = Paragraph(' ', style)
95
  story.append(p)
96
- story.append(Spacer(1, 6)) # small space between paragraphs
97
 
98
- # Build the PDF document
99
  doc.build(story)
100
 
101
- return temp_pdf.name
 
102
 
103
 
104
  with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:
 
4
  from reportlab.pdfgen import canvas
5
  from reportlab.lib.pagesizes import letter
6
  import tempfile
7
+ import re
8
  from textwrap import wrap
9
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
10
+ from reportlab.lib.pagesizes import letter
11
+ from reportlab.lib.styles import getSampleStyleSheet
12
 
13
  # Load API key securely from environment
14
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
67
  if not itinerary_text.strip():
68
  return None
69
 
70
+ temp_dir = tempfile.gettempdir()
71
+ temp_path = os.path.join(temp_dir, "feelaway_itinerary.pdf")
72
+
73
+ if os.path.exists(temp_path):
74
+ os.remove(temp_path)
75
 
76
+ doc = SimpleDocTemplate(temp_path, pagesize=letter,
 
77
  rightMargin=40, leftMargin=40,
78
  topMargin=40, bottomMargin=40)
79
 
 
80
  styles = getSampleStyleSheet()
81
  style = styles["Normal"]
82
+ style.fontName = "Courier"
83
  style.fontSize = 10
84
+ style.leading = 14
85
 
 
86
  html_text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', itinerary_text)
 
 
87
  paragraphs = html_text.split('\n')
88
 
 
89
  story = []
90
  for para in paragraphs:
91
+ p = Paragraph(para if para.strip() else ' ', style)
 
 
 
 
92
  story.append(p)
93
+ story.append(Spacer(1, 6))
94
 
 
95
  doc.build(story)
96
 
97
+ return temp_path
98
+
99
 
100
 
101
  with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo: