Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
GEMINI_KEY = os.getenv("GEMINI")
|
| 7 |
genai.configure(api_key=GEMINI_KEY)
|
|
@@ -64,6 +65,8 @@ body {
|
|
| 64 |
margin: 10px 0 10px 30%;
|
| 65 |
position: relative;
|
| 66 |
animation: slideIn 0.3s ease;
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
.msg-bot {
|
|
@@ -74,14 +77,23 @@ body {
|
|
| 74 |
border: 1px solid #E0E0E0;
|
| 75 |
position: relative;
|
| 76 |
animation: fadeIn 0.5s ease;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
-
.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
font-size: 1.5em;
|
| 85 |
}
|
| 86 |
|
| 87 |
.card {
|
|
@@ -94,54 +106,20 @@ body {
|
|
| 94 |
transition: transform 0.2s;
|
| 95 |
}
|
| 96 |
|
| 97 |
-
.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
.price {
|
| 102 |
-
color: var(--primary);
|
| 103 |
-
font-weight: 600;
|
| 104 |
-
margin: 8px 0;
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
.link {
|
| 108 |
-
color: var(--secondary) !important;
|
| 109 |
-
text-decoration: none;
|
| 110 |
-
font-weight: 500;
|
| 111 |
-
display: inline-flex;
|
| 112 |
-
align-items: center;
|
| 113 |
-
gap: 5px;
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
.link:hover {
|
| 117 |
-
text-decoration: underline;
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
.controls {
|
| 121 |
-
padding: 20px;
|
| 122 |
-
background: #F8FAFD;
|
| 123 |
-
border-top: 1px solid #E0E0E0;
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
input[type="text"] {
|
| 127 |
-
border: 2px solid var(--secondary) !important;
|
| 128 |
-
border-radius: 12px !important;
|
| 129 |
-
padding: 15px !important;
|
| 130 |
-
font-size: 1em;
|
| 131 |
}
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
padding: 12px 25px !important;
|
| 138 |
-
border-radius: 10px !important;
|
| 139 |
-
transition: all 0.2s !important;
|
| 140 |
}
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
box-shadow: 0 5px 15px rgba(42,92,130,0.2) !important;
|
| 145 |
}
|
| 146 |
|
| 147 |
@keyframes slideIn {
|
|
@@ -154,16 +132,7 @@ button:hover {
|
|
| 154 |
to { opacity: 1; transform: translateY(0); }
|
| 155 |
}
|
| 156 |
|
| 157 |
-
|
| 158 |
-
.gradio-container {
|
| 159 |
-
border-radius: 0;
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
.msg-user, .msg-bot {
|
| 163 |
-
margin-left: 10%;
|
| 164 |
-
margin-right: 10%;
|
| 165 |
-
}
|
| 166 |
-
}
|
| 167 |
"""
|
| 168 |
|
| 169 |
class ChatManager:
|
|
@@ -201,7 +170,7 @@ class ChatManager:
|
|
| 201 |
<p style="margin:10px 0;color:var(--text)">{item.get('descrizione', '')}</p>
|
| 202 |
<div style="display:flex;gap:15px;align-items:center">
|
| 203 |
{f"<div class='price'>💰 {item.get('prezzo', '')}</div>" if item.get('prezzo') else ''}
|
| 204 |
-
{f"<a href='{item['link']}' class='link' target='_blank'>
|
| 205 |
</div>
|
| 206 |
</div>
|
| 207 |
"""
|
|
@@ -210,7 +179,7 @@ class ChatManager:
|
|
| 210 |
chat_manager = ChatManager()
|
| 211 |
|
| 212 |
def respond(message, chat_history):
|
| 213 |
-
chat_history.append((message, ""))
|
| 214 |
|
| 215 |
city = chat_manager.extract_city(message)
|
| 216 |
if city:
|
|
@@ -218,7 +187,7 @@ def respond(message, chat_history):
|
|
| 218 |
|
| 219 |
if not chat_manager.context["city"]:
|
| 220 |
bot_response = "🌍 Per favore dimmi prima di quale città vuoi informazioni!"
|
| 221 |
-
chat_history[-1] = (message, bot_response)
|
| 222 |
return "", chat_history
|
| 223 |
|
| 224 |
prompt = f"""
|
|
@@ -238,7 +207,15 @@ def respond(message, chat_history):
|
|
| 238 |
except Exception as e:
|
| 239 |
bot_response = f"⚠️ Errore: {str(e)}"
|
| 240 |
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
return "", chat_history
|
| 243 |
|
| 244 |
def reset_context(chat_history):
|
|
@@ -265,10 +242,10 @@ with gr.Blocks(css=CSS) as app:
|
|
| 265 |
label="Chat",
|
| 266 |
elem_classes="chat-container",
|
| 267 |
show_label=False,
|
| 268 |
-
avatar_images=
|
| 269 |
-
|
| 270 |
-
"https://i.ibb.co/8XJp0dN/bot-avatar.png"
|
| 271 |
-
|
| 272 |
value=[[None, "Ciao sono la tua guida turistica, come posso aiutarti? 🌍"]]
|
| 273 |
)
|
| 274 |
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
GEMINI_KEY = os.getenv("GEMINI")
|
| 8 |
genai.configure(api_key=GEMINI_KEY)
|
|
|
|
| 65 |
margin: 10px 0 10px 30%;
|
| 66 |
position: relative;
|
| 67 |
animation: slideIn 0.3s ease;
|
| 68 |
+
display: flex;
|
| 69 |
+
align-items: center;
|
| 70 |
}
|
| 71 |
|
| 72 |
.msg-bot {
|
|
|
|
| 77 |
border: 1px solid #E0E0E0;
|
| 78 |
position: relative;
|
| 79 |
animation: fadeIn 0.5s ease;
|
| 80 |
+
display: flex;
|
| 81 |
+
align-items: center;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.user-avatar {
|
| 85 |
+
width: 40px;
|
| 86 |
+
height: 40px;
|
| 87 |
+
border-radius: 50%;
|
| 88 |
+
margin-right: 15px;
|
| 89 |
+
border: 2px solid white;
|
| 90 |
}
|
| 91 |
|
| 92 |
+
.bot-avatar {
|
| 93 |
+
width: 40px;
|
| 94 |
+
height: 40px;
|
| 95 |
+
border-radius: 50%;
|
| 96 |
+
margin-right: 15px;
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
.card {
|
|
|
|
| 106 |
transition: transform 0.2s;
|
| 107 |
}
|
| 108 |
|
| 109 |
+
.typing-animation {
|
| 110 |
+
display: inline-block;
|
| 111 |
+
position: relative;
|
| 112 |
+
padding-right: 5px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
+
.typing-animation::after {
|
| 116 |
+
content: "|";
|
| 117 |
+
animation: blink 1s infinite;
|
| 118 |
+
opacity: 1;
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
|
| 121 |
+
@keyframes blink {
|
| 122 |
+
50% { opacity: 0; }
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
@keyframes slideIn {
|
|
|
|
| 132 |
to { opacity: 1; transform: translateY(0); }
|
| 133 |
}
|
| 134 |
|
| 135 |
+
/* Resto dello CSS invariato */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
"""
|
| 137 |
|
| 138 |
class ChatManager:
|
|
|
|
| 170 |
<p style="margin:10px 0;color:var(--text)">{item.get('descrizione', '')}</p>
|
| 171 |
<div style="display:flex;gap:15px;align-items:center">
|
| 172 |
{f"<div class='price'>💰 {item.get('prezzo', '')}</div>" if item.get('prezzo') else ''}
|
| 173 |
+
{f"<a href='{item['link']}' class='link' target='_blank'>Maggiori info</a>" if 'link' in item else ''}
|
| 174 |
</div>
|
| 175 |
</div>
|
| 176 |
"""
|
|
|
|
| 179 |
chat_manager = ChatManager()
|
| 180 |
|
| 181 |
def respond(message, chat_history):
|
| 182 |
+
chat_history.append(((message,), ""))
|
| 183 |
|
| 184 |
city = chat_manager.extract_city(message)
|
| 185 |
if city:
|
|
|
|
| 187 |
|
| 188 |
if not chat_manager.context["city"]:
|
| 189 |
bot_response = "🌍 Per favore dimmi prima di quale città vuoi informazioni!"
|
| 190 |
+
chat_history[-1] = ((message,), bot_response)
|
| 191 |
return "", chat_history
|
| 192 |
|
| 193 |
prompt = f"""
|
|
|
|
| 207 |
except Exception as e:
|
| 208 |
bot_response = f"⚠️ Errore: {str(e)}"
|
| 209 |
|
| 210 |
+
# Simulazione effetto battitura
|
| 211 |
+
full_response = ""
|
| 212 |
+
for char in bot_response:
|
| 213 |
+
full_response += char
|
| 214 |
+
chat_history[-1] = ((message,), f"<div class='typing-animation'>{full_response}</div>")
|
| 215 |
+
time.sleep(0.02)
|
| 216 |
+
yield "", chat_history
|
| 217 |
+
|
| 218 |
+
chat_history[-1] = ((message,), full_response)
|
| 219 |
return "", chat_history
|
| 220 |
|
| 221 |
def reset_context(chat_history):
|
|
|
|
| 242 |
label="Chat",
|
| 243 |
elem_classes="chat-container",
|
| 244 |
show_label=False,
|
| 245 |
+
avatar_images=[
|
| 246 |
+
("https://i.ibb.co/8XJp0dN/user-avatar.png", "user"), # Sostituisci
|
| 247 |
+
("https://i.ibb.co/8XJp0dN/bot-avatar.png", "bot") # Sostituisci
|
| 248 |
+
],
|
| 249 |
value=[[None, "Ciao sono la tua guida turistica, come posso aiutarti? 🌍"]]
|
| 250 |
)
|
| 251 |
|