AlessandroArgiolas02 commited on
Commit
6a22ac3
·
verified ·
1 Parent(s): 6b5f9f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -72
app.py CHANGED
@@ -6,7 +6,7 @@ import requests
6
 
7
  # Configurazione API
8
  GEMINI_KEY = os.getenv("GEMINI")
9
- UNSPLASH_KEY = os.getenv("UNSPLASH_ACCESS") # Usa la tua chiave Unsplash
10
  genai.configure(api_key=GEMINI_KEY)
11
  model = genai.GenerativeModel('gemini-pro')
12
 
@@ -22,7 +22,6 @@ CSS = """
22
  body {
23
  background: var(--background);
24
  font-family: 'Segoe UI', system-ui, sans-serif;
25
- min-height: 100vh;
26
  margin: 0;
27
  padding: 20px;
28
  }
@@ -65,7 +64,6 @@ body {
65
  padding: 15px 20px;
66
  border-radius: 15px 15px 0 15px;
67
  margin: 10px 0 10px 30%;
68
- position: relative;
69
  animation: slideIn 0.3s ease;
70
  }
71
 
@@ -75,7 +73,6 @@ body {
75
  border-radius: 15px 15px 15px 0;
76
  margin: 10px 30% 10px 0;
77
  border: 1px solid #E0E0E0;
78
- position: relative;
79
  animation: fadeIn 0.5s ease;
80
  }
81
 
@@ -84,7 +81,7 @@ body {
84
  padding: 0;
85
  border-radius: 12px;
86
  margin: 15px 0;
87
- box-shadow: 0 3px 10px rgba(0,0,0,0.05);
88
  transition: transform 0.2s;
89
  }
90
 
@@ -104,7 +101,6 @@ body {
104
  width: 100%;
105
  height: 100%;
106
  object-fit: cover;
107
- transition: transform 0.3s ease;
108
  }
109
 
110
  .logo-badge {
@@ -114,10 +110,10 @@ body {
114
  background: rgba(255,255,255,0.9);
115
  padding: 5px 10px;
116
  border-radius: 20px;
 
117
  display: flex;
118
  align-items: center;
119
  gap: 5px;
120
- font-size: 0.8em;
121
  }
122
 
123
  .card-content {
@@ -131,9 +127,8 @@ body {
131
  }
132
 
133
  .link {
134
- color: var(--secondary) !important;
135
  text-decoration: none;
136
- font-weight: 500;
137
  display: inline-flex;
138
  align-items: center;
139
  gap: 5px;
@@ -148,17 +143,6 @@ body {
148
  from { opacity: 0; transform: translateY(10px); }
149
  to { opacity: 1; transform: translateY(0); }
150
  }
151
-
152
- @media (max-width: 600px) {
153
- .gradio-container {
154
- border-radius: 0;
155
- }
156
-
157
- .msg-user, .msg-bot {
158
- margin-left: 10%;
159
- margin-right: 10%;
160
- }
161
- }
162
  """
163
 
164
  class ChatManager:
@@ -180,65 +164,53 @@ class ChatManager:
180
  }
181
  response = requests.get(url, headers=headers, params=params)
182
  results = response.json().get('results', [])
183
- return results[0]['urls']['regular'] if results else None
 
 
 
 
 
184
  except:
185
  return None
186
 
187
  def format_response(self, response):
188
- categories = {}
189
  current_category = None
190
 
191
  lines = response.split('\n')
192
  for line in lines:
193
  if line.startswith('## '):
194
  current_category = line.replace('## ', '').strip()
195
- categories[current_category] = []
196
  elif line.startswith('- **'):
197
  parts = re.split(r'\*\*:?|\*', line)
198
  if len(parts) > 2:
199
  name = parts[1].strip()
200
- desc = parts[2].strip()
201
 
202
- # Estrai prezzi e link
203
- price = re.search(r'💰 (.*?)(?=\s*•|$)', desc)
204
- link = re.search(r'(https?://\S+)', desc)
205
 
206
- categories[current_category].append({
207
- "name": name,
208
- "description": re.sub(r'💰.*?• |https?://\S+', '', desc).strip(),
209
- "price": price.group(1) if price else None,
210
- "link": link.group(1) if link else None,
211
- "image": self.get_place_image(name)
212
- })
213
-
214
- html = ""
215
- for category, items in categories.items():
216
- section = f"""
217
- <div class='category'>
218
- <div class='category-title'>{category}</div>
219
- """
220
-
221
- for item in items:
222
- section += f"""
223
- <div class='card'>
224
- <div class='image-container'>
225
- <img src='{item["image"] or "https://via.placeholder.com/400x200?text=Immagine+non+disponibile"}'
226
- class='place-image'
227
- alt='{item["name"]}'>
228
- </div>
229
- <div class='card-content'>
230
- <h3>{item["name"]}</h3>
231
- <p>{item["description"]}</p>
232
- {f"<p class='price'>💰 {item['price']}</p>" if item['price'] else ''}
233
- {f"<a href='{item['link']}' class='link' target='_blank'>🌐 Sito Ufficiale</a>" if item['link'] else ''}
234
  </div>
235
- </div>
236
- """
237
-
238
- section += "</div>"
239
- html += section
240
-
241
- return html if html else response
242
 
243
  chat_manager = ChatManager()
244
 
@@ -250,22 +222,22 @@ def respond(message, chat_history):
250
  chat_manager.context["city"] = city
251
 
252
  if not chat_manager.context["city"]:
253
- bot_response = "🌍 Per favore dimmi prima di quale città vuoi informazioni!"
254
  chat_history[-1] = (message, bot_response)
255
  return "", chat_history
256
 
257
  prompt = f"""
258
  Sei un esperto guida turistica per {chat_manager.context["city"]}.
259
- Rispondi in italiano strutturando la risposta in markdown con:
260
 
261
  ## Cose da Vedere
262
- - **Nome Luogo** 💰 Prezzo (se disponibile) • Descrizione breve (max 30 parole) • https://link.com
263
  ## Dove Mangiare
264
- - **Nome Ristorante** 💰 Fascia prezzi • Specialità • https://link.com
265
  ## Dove Dormire
266
- - **Nome Alloggio** 💰 Prezzo per notte • Servizi • https://link.com
267
  ## Itinerari
268
- - **1 Giorno**: Attività principali
269
  - **2 Giorni**: Estensione itinerario
270
  """
271
 
@@ -294,21 +266,20 @@ with gr.Blocks(css=CSS) as app:
294
  </svg>
295
  Travel Assistant PRO
296
  </h1>
297
- <p>Scopri il mondo con la nostra AI guidata</p>
298
  </div>
299
  """)
300
 
301
  chatbot = gr.Chatbot(
302
  label="Chat",
303
  elem_classes="chat-container",
304
- value=[[None, "Ciao! 🌍 Dimmi la città che vuoi esplorare"]],
305
  show_label=False
306
  )
307
 
308
  with gr.Row():
309
  msg = gr.Textbox(
310
- label="",
311
- placeholder="Scrivi qui la tua richiesta...",
312
  show_label=False,
313
  container=False
314
  )
 
6
 
7
  # Configurazione API
8
  GEMINI_KEY = os.getenv("GEMINI")
9
+ UNSPLASH_KEY = os.getenv("UNSPLASH_ACCESS")
10
  genai.configure(api_key=GEMINI_KEY)
11
  model = genai.GenerativeModel('gemini-pro')
12
 
 
22
  body {
23
  background: var(--background);
24
  font-family: 'Segoe UI', system-ui, sans-serif;
 
25
  margin: 0;
26
  padding: 20px;
27
  }
 
64
  padding: 15px 20px;
65
  border-radius: 15px 15px 0 15px;
66
  margin: 10px 0 10px 30%;
 
67
  animation: slideIn 0.3s ease;
68
  }
69
 
 
73
  border-radius: 15px 15px 15px 0;
74
  margin: 10px 30% 10px 0;
75
  border: 1px solid #E0E0E0;
 
76
  animation: fadeIn 0.5s ease;
77
  }
78
 
 
81
  padding: 0;
82
  border-radius: 12px;
83
  margin: 15px 0;
84
+ box-shadow: 0 3px 6px rgba(0,0,0,0.1);
85
  transition: transform 0.2s;
86
  }
87
 
 
101
  width: 100%;
102
  height: 100%;
103
  object-fit: cover;
 
104
  }
105
 
106
  .logo-badge {
 
110
  background: rgba(255,255,255,0.9);
111
  padding: 5px 10px;
112
  border-radius: 20px;
113
+ font-size: 0.8em;
114
  display: flex;
115
  align-items: center;
116
  gap: 5px;
 
117
  }
118
 
119
  .card-content {
 
127
  }
128
 
129
  .link {
130
+ color: var(--secondary);
131
  text-decoration: none;
 
132
  display: inline-flex;
133
  align-items: center;
134
  gap: 5px;
 
143
  from { opacity: 0; transform: translateY(10px); }
144
  to { opacity: 1; transform: translateY(0); }
145
  }
 
 
 
 
 
 
 
 
 
 
 
146
  """
147
 
148
  class ChatManager:
 
164
  }
165
  response = requests.get(url, headers=headers, params=params)
166
  results = response.json().get('results', [])
167
+ if results:
168
+ return {
169
+ "image": results[0]['urls']['regular'],
170
+ "credit": results[0]['user']['name']
171
+ }
172
+ return None
173
  except:
174
  return None
175
 
176
  def format_response(self, response):
177
+ html = ""
178
  current_category = None
179
 
180
  lines = response.split('\n')
181
  for line in lines:
182
  if line.startswith('## '):
183
  current_category = line.replace('## ', '').strip()
184
+ html += f"<h2 style='color: var(--primary); margin-top: 30px;'>{current_category}</h2>"
185
  elif line.startswith('- **'):
186
  parts = re.split(r'\*\*:?|\*', line)
187
  if len(parts) > 2:
188
  name = parts[1].strip()
189
+ details = parts[2].split('•')
190
 
191
+ price = next((d.replace('💰', '').strip() for d in details if '💰' in d), None)
192
+ link = next((d.strip() for d in details if 'http' in d), None)
193
+ desc = ' • '.join([d.strip() for d in details if '💰' not in d and 'http' not in d])
194
 
195
+ image_data = self.get_place_image(name)
196
+
197
+ html += f"""
198
+ <div class='card'>
199
+ <div class='image-container'>
200
+ <img src='{image_data['image'] if image_data else 'https://via.placeholder.com/400x200?text=Immagine+non+disponibile'}'
201
+ class='place-image'
202
+ alt='{name}'>
203
+ {f"<div class='logo-badge'>📸 {image_data['credit']}</div>" if image_data else ''}
204
+ </div>
205
+ <div class='card-content'>
206
+ <h3>{name}</h3>
207
+ <p>{desc}</p>
208
+ {f"<p class='price'>💰 {price}</p>" if price else ''}
209
+ {f"<a href='{link}' class='link' target='_blank'>🌐 Sito Ufficiale</a>" if link else ''}
210
+ </div>
 
 
 
 
 
 
 
 
 
 
 
 
211
  </div>
212
+ """
213
+ return html
 
 
 
 
 
214
 
215
  chat_manager = ChatManager()
216
 
 
222
  chat_manager.context["city"] = city
223
 
224
  if not chat_manager.context["city"]:
225
+ bot_response = "🌍 Per favore dimmi di quale città vuoi informazioni!"
226
  chat_history[-1] = (message, bot_response)
227
  return "", chat_history
228
 
229
  prompt = f"""
230
  Sei un esperto guida turistica per {chat_manager.context["city"]}.
231
+ Fornisci raccomandazioni in italiano con formato:
232
 
233
  ## Cose da Vedere
234
+ - **Nome Luogo** • Descrizione breve (max 30 parole) 💰 Prezzo • https://link.com
235
  ## Dove Mangiare
236
+ - **Nome Ristorante** • Specialità 💰 Fascia prezzi • https://link.com
237
  ## Dove Dormire
238
+ - **Nome Alloggio** • Servizi offerti 💰 Prezzo per notte • https://link.com
239
  ## Itinerari
240
+ - **1 Giorno**: Attrazioni principali
241
  - **2 Giorni**: Estensione itinerario
242
  """
243
 
 
266
  </svg>
267
  Travel Assistant PRO
268
  </h1>
269
+ <p>La tua guida turistica intelligente</p>
270
  </div>
271
  """)
272
 
273
  chatbot = gr.Chatbot(
274
  label="Chat",
275
  elem_classes="chat-container",
276
+ avatar_images=(None, "https://i.ibb.co/8XJp0dN/bot-avatar.png"),
277
  show_label=False
278
  )
279
 
280
  with gr.Row():
281
  msg = gr.Textbox(
282
+ placeholder="✍️ Scrivi qui la tua richiesta...",
 
283
  show_label=False,
284
  container=False
285
  )