Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|