Infinity-1995 commited on
Commit
44a685a
·
verified ·
1 Parent(s): 4304f65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -30
app.py CHANGED
@@ -59,39 +59,42 @@ def generate_itinerary(mood, location, budget, days):
59
  image_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])
60
  return itinerary_text, image_path, gr.update(visible=True)
61
 
62
- def generate_pdf(itinerary_text):
63
  if not itinerary_text.strip():
64
  return None
65
 
66
- temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
67
- c = canvas.Canvas(temp_pdf.name, pagesize=letter)
68
- width, height = letter
69
-
70
- margin = 40
71
- line_height = 12
72
- font_size = 10
73
- c.setFont("Courier", font_size)
74
-
75
- max_chars_per_line = int((width - 2 * margin) / (font_size * 0.6))
76
-
77
- x = margin
78
- y = height - margin
79
-
80
- lines = []
81
- for paragraph in itinerary_text.split('\n'):
82
- wrapped = wrap(paragraph, width=max_chars_per_line)
83
- lines.extend(wrapped if wrapped else [''])
84
-
85
- for line in lines:
86
- if y < margin + line_height:
87
- c.showPage()
88
- c.setFont("Courier", font_size)
89
- y = height - margin
90
- c.drawString(x, y, line)
91
- y -= line_height
92
-
93
- c.save()
94
- return temp_pdf.name
 
 
 
95
 
96
  with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:
97
  # Header with gr.HTML for styles and animation
 
59
  image_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])
60
  return itinerary_text, image_path, gr.update(visible=True)
61
 
62
+ def generate_pdf(itinerary_text, location="Destination", days=5):
63
  if not itinerary_text.strip():
64
  return None
65
 
66
+ # Create a nice filename
67
+ safe_location = re.sub(r'[^A-Za-z0-9]+', '_', location.strip())
68
+ filename = f"itinerary_{safe_location}_{days}_days.pdf"
69
+ file_path = os.path.join(tempfile.gettempdir(), filename)
70
+
71
+ # Prepare the document template with letter page size
72
+ doc = SimpleDocTemplate(file_path, pagesize=letter,rightMargin=40, leftMargin=40,topMargin=40, bottomMargin=40)
73
+ # Get default stylesheet and customize the Normal style
74
+ styles = getSampleStyleSheet()
75
+ style = styles["Normal"]
76
+ style.fontName = "Courier" # Use monospaced font like your original code
77
+ style.fontSize = 10
78
+ style.leading = 14 # line height
79
+
80
+ # Convert markdown-style bold (**text**) to HTML <b>text</b>
81
+ html_text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', itinerary_text)
82
+
83
+ # Split the text into paragraphs by newlines
84
+ paragraphs = html_text.split('\n')
85
+
86
+ # Build the story (list of flowables)
87
+ story = []
88
+ for para in paragraphs:
89
+ if para.strip():
90
+ story.append(Paragraph(para, style))
91
+ else:
92
+ story.append(Paragraph(' ', style))
93
+ story.append(Spacer(1, 6))
94
+
95
+ doc.build(story)
96
+
97
+ return file_path
98
 
99
  with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:
100
  # Header with gr.HTML for styles and animation