Infinity-1995 commited on
Commit
5f931a3
·
verified ·
1 Parent(s): fceff5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -14
app.py CHANGED
@@ -1,13 +1,47 @@
1
  import os
2
  import gradio as gr
3
  from groq import Groq
 
 
 
4
 
5
- # Load API key securely
6
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
7
  client = Groq(api_key=GROQ_API_KEY)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def generate_itinerary(mood, location, budget, days):
10
- prompt = f"Create a {days}-day travel itinerary to {location}. The mood of the traveler is {mood} and the budget is {budget}."
 
 
 
 
 
 
 
 
 
11
  completion = client.chat.completions.create(
12
  model="gemma2-9b-it",
13
  messages=[
@@ -19,24 +53,75 @@ def generate_itinerary(mood, location, budget, days):
19
  top_p=1,
20
  stream=False
21
  )
22
- return completion.choices[0].message.content
 
 
 
23
 
24
- with gr.Blocks() as demo:
25
- gr.Markdown("# Mood-based Travel Itinerary Planner")
 
 
 
 
 
 
 
 
 
 
 
 
26
 
 
 
 
 
27
  with gr.Row():
28
- mood = gr.Dropdown(
29
- ["Adventure", "Relaxing", "Romantic", "Cultural"],
30
- label="Mood"
 
 
 
 
 
 
31
  )
32
- location = gr.Textbox(label="Destination", placeholder="e.g., India, Japan, France")
 
 
 
 
33
 
34
  with gr.Row():
35
- budget = gr.Dropdown(["Low", "Medium", "High"], label="Budget")
36
- days = gr.Number(value=5, precision=0, label="Days")
37
 
38
- output = gr.Textbox(label="Generated Itinerary", lines=20)
39
- btn = gr.Button("Generate Itinerary")
40
- btn.click(generate_itinerary, inputs=[mood, location, budget, days], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
  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")
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
12
+ MOODS = [
13
+ "Adventure",
14
+ "Relaxing",
15
+ "Romantic",
16
+ "Cultural & Heritage",
17
+ "Foodie & Culinary",
18
+ "Shopping & Urban",
19
+ "Beach & Island",
20
+ "Any"
21
+ ]
22
+
23
+ MOOD_IMAGES = {
24
+ "Adventure": "images/adventure.png",
25
+ "Relaxing": "images/relaxing.png",
26
+ "Romantic": "images/romantic.png",
27
+ "Cultural & Heritage": "images/cultural.png",
28
+ "Foodie & Culinary": "images/foodie.png",
29
+ "Shopping & Urban": "images/shopping.png",
30
+ "Beach & Island": "images/beach.png",
31
+ "Any": "images/default.jpg"
32
+ }
33
+
34
  def generate_itinerary(mood, location, budget, days):
35
+ mood_text = "any mood" if mood == "Any" else mood
36
+ budget_text = "any budget" if budget == "Any" else budget
37
+ location_text = location if location.strip() != "" else "any destination"
38
+
39
+ prompt = (
40
+ f"Create a {days}-day travel itinerary to {location_text}. "
41
+ f"The mood of the traveler is {mood_text} and the budget is {budget_text}. "
42
+ f"Use Markdown formatting with **bold headings** for days and time slots."
43
+ )
44
+
45
  completion = client.chat.completions.create(
46
  model="gemma2-9b-it",
47
  messages=[
 
53
  top_p=1,
54
  stream=False
55
  )
56
+
57
+ itinerary_text = completion.choices[0].message.content
58
+ image_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])
59
+ return itinerary_text, image_path
60
 
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
+ text_obj = c.beginText(40, height - 40)
68
+ text_obj.setFont("Helvetica", 11)
69
+
70
+ for line in itinerary_text.splitlines():
71
+ text_obj.textLine(line)
72
+ c.drawText(text_obj)
73
+ c.showPage()
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 style='color: #006d6d; font-size: 38px; margin-bottom: 0;'>Feel Away</h1>
85
+ <p style='font-size: 18px; color: #007777; margin-top: 4px;'>
86
+ Your Mood, Your Journey – Personalized Itineraries, Instantly
87
+ </p>
88
+ </div>
89
+ """
90
  )
91
+
92
+ # Inputs
93
+ with gr.Row():
94
+ mood = gr.Dropdown(MOODS, label="Mood", value="Any", info="Select the mood of your trip")
95
+ location = gr.Textbox(label="Destination", placeholder="e.g., India, Japan, France (leave blank for Any)", info="Where do you want to go?")
96
 
97
  with gr.Row():
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
+ # Buttons
102
+ with gr.Row():
103
+ generate_btn = gr.Button("✨ Generate Itinerary", variant="primary")
104
+
105
+ # Outputs
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
+ # Events
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()