Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import time
|
| 3 |
-
import re
|
| 4 |
-
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from groq import Groq
|
| 7 |
from reportlab.pdfgen import canvas
|
|
@@ -35,49 +32,6 @@ MOOD_IMAGES = {
|
|
| 35 |
"Any": "images/default.png"
|
| 36 |
}
|
| 37 |
|
| 38 |
-
# --- Map related functions ---
|
| 39 |
-
def extract_places(itinerary_text):
|
| 40 |
-
pattern = r"\b([A-Z][a-z]+(?:\s[A-Z][a-z]+)*)\b"
|
| 41 |
-
matches = re.findall(pattern, itinerary_text)
|
| 42 |
-
blacklist = {"Day", "Morning", "Evening", "Afternoon", "Breakfast", "Lunch", "Dinner"}
|
| 43 |
-
places = [m for m in matches if m not in blacklist and len(m) > 2]
|
| 44 |
-
return list(dict.fromkeys(places)) # unique order preserved
|
| 45 |
-
|
| 46 |
-
def geocode_location(place_name):
|
| 47 |
-
url = "https://nominatim.openstreetmap.org/search"
|
| 48 |
-
params = {"q": place_name, "format": "json", "limit": 1}
|
| 49 |
-
headers = {"User-Agent": "FeelAwayApp/1.0 (mathushawlika1221@gmail.com)"}
|
| 50 |
-
try:
|
| 51 |
-
response = requests.get(url, params=params, headers=headers, timeout=5)
|
| 52 |
-
if response.status_code == 200 and response.json():
|
| 53 |
-
data = response.json()[0]
|
| 54 |
-
return float(data["lat"]), float(data["lon"])
|
| 55 |
-
except:
|
| 56 |
-
pass
|
| 57 |
-
return None, None
|
| 58 |
-
|
| 59 |
-
def create_leaflet_map(locations):
|
| 60 |
-
markers_js = ""
|
| 61 |
-
for loc in locations:
|
| 62 |
-
markers_js += f"L.marker([{loc['lat']}, {loc['lng']}]).addTo(map).bindPopup('<b>{loc['name']}</b>');\n"
|
| 63 |
-
|
| 64 |
-
center_lat = locations[0]['lat'] if locations else 20
|
| 65 |
-
center_lng = locations[0]['lng'] if locations else 0
|
| 66 |
-
|
| 67 |
-
html = f"""
|
| 68 |
-
<div id="map" style="width: 100%; height: 400px; margin-top: 20px;"></div>
|
| 69 |
-
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.3/dist/leaflet.css" />
|
| 70 |
-
<script src="https://unpkg.com/leaflet@1.9.3/dist/leaflet.js"></script>
|
| 71 |
-
<script>
|
| 72 |
-
var map = L.map('map').setView([{center_lat}, {center_lng}], 6);
|
| 73 |
-
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
|
| 74 |
-
attribution: '© OpenStreetMap contributors',
|
| 75 |
-
}}).addTo(map);
|
| 76 |
-
{markers_js}
|
| 77 |
-
</script>
|
| 78 |
-
"""
|
| 79 |
-
return html
|
| 80 |
-
|
| 81 |
def generate_itinerary(mood, location, budget, days):
|
| 82 |
mood_text = "any mood" if mood == "Any" else mood
|
| 83 |
budget_text = "any budget" if budget == "Any" else budget
|
|
@@ -103,76 +57,100 @@ def generate_itinerary(mood, location, budget, days):
|
|
| 103 |
|
| 104 |
itinerary_text = completion.choices[0].message.content
|
| 105 |
image_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])
|
| 106 |
-
|
| 107 |
-
# --- Map generation ---
|
| 108 |
-
places = extract_places(itinerary_text)
|
| 109 |
-
locations = []
|
| 110 |
-
for p in places:
|
| 111 |
-
lat, lng = geocode_location(p)
|
| 112 |
-
if lat and lng:
|
| 113 |
-
locations.append({"name": p, "lat": lat, "lng": lng})
|
| 114 |
-
time.sleep(1) # respect Nominatim usage policy
|
| 115 |
-
if not locations:
|
| 116 |
-
locations = [{"name": "World", "lat": 20, "lng": 0}]
|
| 117 |
-
map_html = create_leaflet_map(locations)
|
| 118 |
-
|
| 119 |
-
return itinerary_text, image_path, gr.update(value=map_html, visible=True)
|
| 120 |
|
| 121 |
def generate_pdf(itinerary_text):
|
| 122 |
if not itinerary_text.strip():
|
| 123 |
return None
|
|
|
|
| 124 |
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 125 |
c = canvas.Canvas(temp_pdf.name, pagesize=letter)
|
| 126 |
width, height = letter
|
|
|
|
| 127 |
margin = 40
|
| 128 |
line_height = 12
|
| 129 |
font_size = 10
|
| 130 |
c.setFont("Courier", font_size)
|
|
|
|
| 131 |
max_chars_per_line = int((width - 2 * margin) / (font_size * 0.6))
|
|
|
|
| 132 |
x = margin
|
| 133 |
y = height - margin
|
|
|
|
|
|
|
| 134 |
for paragraph in itinerary_text.split('\n'):
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
c.
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
c.save()
|
| 143 |
return temp_pdf.name
|
| 144 |
|
| 145 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:
|
|
|
|
| 146 |
header_html = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
<div style='text-align: center;'>
|
| 148 |
-
<h1
|
| 149 |
-
<p
|
| 150 |
-
"Your Mood, Your Journey – Personalized Itineraries, Instantly"</p>
|
| 151 |
</div>
|
| 152 |
"""
|
| 153 |
-
gr.
|
|
|
|
| 154 |
|
|
|
|
| 155 |
with gr.Row():
|
| 156 |
-
mood = gr.Dropdown(MOODS, label="Mood", value="Any")
|
| 157 |
-
location = gr.Textbox(label="Destination", placeholder="e.g., India, Japan, France")
|
|
|
|
| 158 |
with gr.Row():
|
| 159 |
-
budget = gr.Dropdown(["Low", "Medium", "High", "Any"], label="Budget", value="Any")
|
| 160 |
-
days = gr.Slider(1, 15, value=5, step=1, label="Days")
|
| 161 |
-
|
| 162 |
generate_btn = gr.Button("✨ Generate Itinerary", variant="primary")
|
| 163 |
-
itinerary_output = gr.Markdown(label="Generated Itinerary")
|
| 164 |
-
image_output = gr.Image(label="Travel Mood Image", type="filepath", visible=False)
|
| 165 |
-
map_output = gr.HTML(visible=False)
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
generate_btn.click(
|
| 171 |
generate_itinerary,
|
| 172 |
inputs=[mood, location, budget, days],
|
| 173 |
-
outputs=[itinerary_output, image_output,
|
| 174 |
)
|
| 175 |
|
| 176 |
-
download_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
from reportlab.pdfgen import canvas
|
|
|
|
| 32 |
"Any": "images/default.png"
|
| 33 |
}
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def generate_itinerary(mood, location, budget, days):
|
| 36 |
mood_text = "any mood" if mood == "Any" else mood
|
| 37 |
budget_text = "any budget" if budget == "Any" else budget
|
|
|
|
| 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():
|
| 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
|
| 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():
|
| 127 |
+
mood = gr.Dropdown(MOODS, label="Mood", value="Any", info="Select the mood of your trip")
|
| 128 |
+
location = gr.Textbox(label="Destination", placeholder="e.g., India, Japan, France (leave blank for Any)", info="Where do you want to go?")
|
| 129 |
+
|
| 130 |
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(
|
| 151 |
+
generate_pdf,
|
| 152 |
+
inputs=[itinerary_output],
|
| 153 |
+
outputs=pdf_file
|
| 154 |
+
)
|
| 155 |
|
| 156 |
+
demo.launch()
|