Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from groq import Groq
|
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
from reportlab.lib.pagesizes import letter
|
| 6 |
import tempfile
|
|
|
|
| 7 |
|
| 8 |
# Load API key securely from Hugging Face secrets
|
| 9 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
@@ -14,6 +15,7 @@ MOODS = [
|
|
| 14 |
"Relaxing",
|
| 15 |
"Romantic",
|
| 16 |
"Cultural & Heritage",
|
|
|
|
| 17 |
"Foodie & Culinary",
|
| 18 |
"Shopping & Urban",
|
| 19 |
"Beach & Island",
|
|
@@ -21,13 +23,14 @@ MOODS = [
|
|
| 21 |
]
|
| 22 |
|
| 23 |
MOOD_IMAGES = {
|
| 24 |
-
"Adventure": "images/adventure.
|
| 25 |
-
"Relaxing": "images/relaxing.
|
| 26 |
-
"Romantic": "images/romantic.
|
| 27 |
-
"Cultural & Heritage": "images/cultural.
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
|
|
|
| 31 |
"Any": "images/default.jpg"
|
| 32 |
}
|
| 33 |
|
|
@@ -61,30 +64,64 @@ def generate_itinerary(mood, location, budget, days):
|
|
| 61 |
def generate_pdf(itinerary_text):
|
| 62 |
if not itinerary_text.strip():
|
| 63 |
return None
|
|
|
|
| 64 |
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 65 |
c = canvas.Canvas(temp_pdf.name, pagesize=letter)
|
| 66 |
width, height = letter
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
c.save()
|
| 75 |
-
|
| 76 |
return temp_pdf.name
|
| 77 |
|
| 78 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:
|
| 79 |
-
# Header
|
| 80 |
with gr.Row():
|
| 81 |
gr.Markdown(
|
| 82 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
<div style='text-align: center;'>
|
| 84 |
-
<h1
|
| 85 |
-
<p
|
| 86 |
-
Your Mood, Your Journey – Personalized Itineraries, Instantly
|
| 87 |
-
</p>
|
| 88 |
</div>
|
| 89 |
"""
|
| 90 |
)
|
|
@@ -98,30 +135,31 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")
|
|
| 98 |
budget = gr.Dropdown(["Low", "Medium", "High", "Any"], label="Budget", value="Any", info="Choose your budget level")
|
| 99 |
days = gr.Slider(1, 15, value=5, step=1, label="Days", info="Number of days for the itinerary")
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
with gr.Row():
|
| 107 |
-
image_output = gr.Image(label="Travel Mood Image", type="filepath")
|
| 108 |
itinerary_output = gr.Markdown(label="Generated Itinerary")
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
with gr.Row():
|
| 111 |
download_btn = gr.Button("📥 Download Itinerary as PDF", variant="secondary")
|
| 112 |
pdf_file = gr.File(label="Download PDF", file_types=[".pdf"])
|
| 113 |
-
|
| 114 |
-
#
|
| 115 |
generate_btn.click(
|
| 116 |
-
generate_itinerary,
|
| 117 |
-
inputs=[mood, location, budget, days],
|
| 118 |
outputs=[itinerary_output, image_output]
|
| 119 |
)
|
| 120 |
-
|
| 121 |
download_btn.click(
|
| 122 |
-
generate_pdf,
|
| 123 |
-
inputs=[itinerary_output],
|
| 124 |
outputs=pdf_file
|
| 125 |
)
|
| 126 |
|
| 127 |
demo.launch()
|
|
|
|
|
|
| 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 Hugging Face secrets
|
| 10 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
| 15 |
"Relaxing",
|
| 16 |
"Romantic",
|
| 17 |
"Cultural & Heritage",
|
| 18 |
+
"Nature & Wildlife",
|
| 19 |
"Foodie & Culinary",
|
| 20 |
"Shopping & Urban",
|
| 21 |
"Beach & Island",
|
|
|
|
| 23 |
]
|
| 24 |
|
| 25 |
MOOD_IMAGES = {
|
| 26 |
+
"Adventure": "images/adventure.jpg",
|
| 27 |
+
"Relaxing": "images/relaxing.jpg",
|
| 28 |
+
"Romantic": "images/romantic.jpg",
|
| 29 |
+
"Cultural & Heritage": "images/cultural.jpg",
|
| 30 |
+
"Nature & Wildlife": "images/nature.jpg",
|
| 31 |
+
"Foodie & Culinary": "images/foodie.jpg",
|
| 32 |
+
"Shopping & Urban": "images/shopping.jpg",
|
| 33 |
+
"Beach & Island": "images/beach.jpg",
|
| 34 |
"Any": "images/default.jpg"
|
| 35 |
}
|
| 36 |
|
|
|
|
| 64 |
def generate_pdf(itinerary_text):
|
| 65 |
if not itinerary_text.strip():
|
| 66 |
return None
|
| 67 |
+
|
| 68 |
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 69 |
c = canvas.Canvas(temp_pdf.name, pagesize=letter)
|
| 70 |
width, height = letter
|
| 71 |
+
|
| 72 |
+
margin = 40
|
| 73 |
+
max_width = width - 2 * margin
|
| 74 |
+
max_height = height - 2 * margin
|
| 75 |
+
line_height = 14
|
| 76 |
+
x = margin
|
| 77 |
+
y = height - margin
|
| 78 |
+
|
| 79 |
+
# Use a fixed-width font for better alignment
|
| 80 |
+
c.setFont("Courier", 11)
|
| 81 |
+
|
| 82 |
+
# Split text into paragraphs and wrap each to fit the page width
|
| 83 |
+
lines = []
|
| 84 |
+
for paragraph in itinerary_text.split('\n'):
|
| 85 |
+
wrapped = wrap(paragraph, width=95) # adjust width for wrapping
|
| 86 |
+
lines.extend(wrapped if wrapped else [''])
|
| 87 |
+
|
| 88 |
+
for line in lines:
|
| 89 |
+
if y < margin + line_height:
|
| 90 |
+
c.showPage()
|
| 91 |
+
c.setFont("Courier", 11)
|
| 92 |
+
y = height - margin
|
| 93 |
+
c.drawString(x, y, line)
|
| 94 |
+
y -= line_height
|
| 95 |
+
|
| 96 |
c.save()
|
|
|
|
| 97 |
return temp_pdf.name
|
| 98 |
|
| 99 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:
|
| 100 |
+
# Header with animated "Feel Away" and italicized tagline in quotes
|
| 101 |
with gr.Row():
|
| 102 |
gr.Markdown(
|
| 103 |
"""
|
| 104 |
+
<style>
|
| 105 |
+
@keyframes colorCycle {
|
| 106 |
+
0% { color: #008080; }
|
| 107 |
+
25% { color: #20b2aa; }
|
| 108 |
+
50% { color: #40e0d0; }
|
| 109 |
+
75% { color: #008080; }
|
| 110 |
+
100% { color: #20b2aa; }
|
| 111 |
+
}
|
| 112 |
+
.color-cycle {
|
| 113 |
+
animation: colorCycle 4s infinite;
|
| 114 |
+
}
|
| 115 |
+
.tagline {
|
| 116 |
+
font-style: italic;
|
| 117 |
+
color: #007777;
|
| 118 |
+
font-size: 18px;
|
| 119 |
+
margin-top: 4px;
|
| 120 |
+
}
|
| 121 |
+
</style>
|
| 122 |
<div style='text-align: center;'>
|
| 123 |
+
<h1 class="color-cycle" style="font-size: 38px; margin-bottom: 0;">FEEL AWAY APP</h1>
|
| 124 |
+
<p class="tagline">"Your Mood, Your Journey – Personalized Itineraries, Instantly"</p>
|
|
|
|
|
|
|
| 125 |
</div>
|
| 126 |
"""
|
| 127 |
)
|
|
|
|
| 135 |
budget = gr.Dropdown(["Low", "Medium", "High", "Any"], label="Budget", value="Any", info="Choose your budget level")
|
| 136 |
days = gr.Slider(1, 15, value=5, step=1, label="Days", info="Number of days for the itinerary")
|
| 137 |
|
| 138 |
+
# Generate button
|
| 139 |
+
generate_btn = gr.Button("✨ Generate Itinerary", variant="primary")
|
| 140 |
+
|
| 141 |
+
# Outputs stacked vertically: itinerary markdown then image below
|
| 142 |
+
with gr.Column():
|
|
|
|
|
|
|
| 143 |
itinerary_output = gr.Markdown(label="Generated Itinerary")
|
| 144 |
+
image_output = gr.Image(label="Travel Mood Image", type="filepath")
|
| 145 |
+
|
| 146 |
+
# PDF download row
|
| 147 |
with gr.Row():
|
| 148 |
download_btn = gr.Button("📥 Download Itinerary as PDF", variant="secondary")
|
| 149 |
pdf_file = gr.File(label="Download PDF", file_types=[".pdf"])
|
| 150 |
+
|
| 151 |
+
# Event bindings
|
| 152 |
generate_btn.click(
|
| 153 |
+
generate_itinerary,
|
| 154 |
+
inputs=[mood, location, budget, days],
|
| 155 |
outputs=[itinerary_output, image_output]
|
| 156 |
)
|
| 157 |
+
|
| 158 |
download_btn.click(
|
| 159 |
+
generate_pdf,
|
| 160 |
+
inputs=[itinerary_output],
|
| 161 |
outputs=pdf_file
|
| 162 |
)
|
| 163 |
|
| 164 |
demo.launch()
|
| 165 |
+
|