Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from reportlab.lib.pagesizes import letter
|
|
| 6 |
import tempfile
|
| 7 |
from textwrap import wrap
|
| 8 |
|
| 9 |
-
# Load API key securely from
|
| 10 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
|
|
@@ -15,7 +15,6 @@ MOODS = [
|
|
| 15 |
"Relaxing",
|
| 16 |
"Romantic",
|
| 17 |
"Cultural & Heritage",
|
| 18 |
-
"Nature & Wildlife",
|
| 19 |
"Foodie & Culinary",
|
| 20 |
"Shopping & Urban",
|
| 21 |
"Beach & Island",
|
|
@@ -23,15 +22,14 @@ MOODS = [
|
|
| 23 |
]
|
| 24 |
|
| 25 |
MOOD_IMAGES = {
|
| 26 |
-
"Adventure": "images/adventure.
|
| 27 |
-
"Relaxing": "images/relaxing.
|
| 28 |
-
"Romantic": "images/romantic.
|
| 29 |
-
"Cultural & Heritage": "images/cultural.
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"Any": "images/default.jpg"
|
| 35 |
}
|
| 36 |
|
| 37 |
def generate_itinerary(mood, location, budget, days):
|
|
@@ -59,7 +57,7 @@ def generate_itinerary(mood, location, budget, days):
|
|
| 59 |
|
| 60 |
itinerary_text = completion.choices[0].message.content
|
| 61 |
image_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])
|
| 62 |
-
return itinerary_text, image_path
|
| 63 |
|
| 64 |
def generate_pdf(itinerary_text):
|
| 65 |
if not itinerary_text.strip():
|
|
@@ -70,25 +68,24 @@ def generate_pdf(itinerary_text):
|
|
| 70 |
width, height = letter
|
| 71 |
|
| 72 |
margin = 40
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 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",
|
| 92 |
y = height - margin
|
| 93 |
c.drawString(x, y, line)
|
| 94 |
y -= line_height
|
|
@@ -97,34 +94,33 @@ def generate_pdf(itinerary_text):
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
with gr.Row():
|
| 102 |
-
gr.
|
| 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 |
-
)
|
| 128 |
|
| 129 |
# Inputs
|
| 130 |
with gr.Row():
|
|
@@ -135,24 +131,20 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")
|
|
| 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 |
-
|
| 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(
|
|
@@ -162,4 +154,3 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")
|
|
| 162 |
)
|
| 163 |
|
| 164 |
demo.launch()
|
| 165 |
-
|
|
|
|
| 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")
|
| 11 |
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
|
|
|
|
| 15 |
"Relaxing",
|
| 16 |
"Romantic",
|
| 17 |
"Cultural & Heritage",
|
|
|
|
| 18 |
"Foodie & Culinary",
|
| 19 |
"Shopping & Urban",
|
| 20 |
"Beach & Island",
|
|
|
|
| 22 |
]
|
| 23 |
|
| 24 |
MOOD_IMAGES = {
|
| 25 |
+
"Adventure": "images/adventure.png",
|
| 26 |
+
"Relaxing": "images/relaxing.png",
|
| 27 |
+
"Romantic": "images/romantic.png",
|
| 28 |
+
"Cultural & Heritage": "images/cultural.png",
|
| 29 |
+
"Foodie & Culinary": "images/foodie.png",
|
| 30 |
+
"Shopping & Urban": "images/shopping.png",
|
| 31 |
+
"Beach & Island": "images/beach.png",
|
| 32 |
+
"Any": "images/default.png"
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
def generate_itinerary(mood, location, budget, days):
|
|
|
|
| 57 |
|
| 58 |
itinerary_text = completion.choices[0].message.content
|
| 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():
|
|
|
|
| 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
|
|
|
|
| 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
|
| 98 |
+
header_html = """
|
| 99 |
+
<style>
|
| 100 |
+
@keyframes colorCycle {
|
| 101 |
+
0% { color: #008080; }
|
| 102 |
+
25% { color: #20b2aa; }
|
| 103 |
+
50% { color: #40e0d0; }
|
| 104 |
+
75% { color: #008080; }
|
| 105 |
+
100% { color: #20b2aa; }
|
| 106 |
+
}
|
| 107 |
+
.color-cycle {
|
| 108 |
+
animation: colorCycle 4s infinite;
|
| 109 |
+
}
|
| 110 |
+
.tagline {
|
| 111 |
+
font-style: italic;
|
| 112 |
+
color: #007777;
|
| 113 |
+
font-size: 18px;
|
| 114 |
+
margin-top: 4px;
|
| 115 |
+
}
|
| 116 |
+
</style>
|
| 117 |
+
<div style='text-align: center;'>
|
| 118 |
+
<h1 class="color-cycle" style="font-size: 38px; margin-bottom: 0;">FEEL AWAY APP</h1>
|
| 119 |
+
<p class="tagline">"Your Mood, Your Journey – Personalized Itineraries, Instantly"</p>
|
| 120 |
+
</div>
|
| 121 |
+
"""
|
| 122 |
with gr.Row():
|
| 123 |
+
gr.HTML(header_html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Inputs
|
| 126 |
with gr.Row():
|
|
|
|
| 131 |
budget = gr.Dropdown(["Low", "Medium", "High", "Any"], label="Budget", value="Any", info="Choose your budget level")
|
| 132 |
days = gr.Slider(1, 15, value=5, step=1, label="Days", info="Number of days for the itinerary")
|
| 133 |
|
|
|
|
| 134 |
generate_btn = gr.Button("✨ Generate Itinerary", variant="primary")
|
| 135 |
|
|
|
|
| 136 |
with gr.Column():
|
| 137 |
itinerary_output = gr.Markdown(label="Generated Itinerary")
|
| 138 |
+
image_output = gr.Image(label="Travel Mood Image", type="filepath", visible=False)
|
| 139 |
|
| 140 |
+
with gr.Column():
|
|
|
|
| 141 |
download_btn = gr.Button("📥 Download Itinerary as PDF", variant="secondary")
|
| 142 |
pdf_file = gr.File(label="Download PDF", file_types=[".pdf"])
|
| 143 |
|
|
|
|
| 144 |
generate_btn.click(
|
| 145 |
generate_itinerary,
|
| 146 |
inputs=[mood, location, budget, days],
|
| 147 |
+
outputs=[itinerary_output, image_output, image_output]
|
| 148 |
)
|
| 149 |
|
| 150 |
download_btn.click(
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
demo.launch()
|
|
|