Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ genai.configure(api_key=GEMINI_KEY)
|
|
| 8 |
model = genai.GenerativeModel('gemini-pro')
|
| 9 |
|
| 10 |
CSS = """
|
|
|
|
| 11 |
body { background: #f5f7fa; font-family: 'Arial', sans-serif; }
|
| 12 |
.chat-container { max-width: 800px; margin: 0 auto; }
|
| 13 |
.msg-user { background: #e3f2fd; padding: 15px; border-radius: 15px; margin: 10px; max-width: 70%; float: right; }
|
|
@@ -59,14 +60,19 @@ class ChatManager:
|
|
| 59 |
|
| 60 |
chat_manager = ChatManager()
|
| 61 |
|
| 62 |
-
def respond(message,
|
|
|
|
|
|
|
|
|
|
| 63 |
# Estrai e mantieni contesto città
|
| 64 |
city = chat_manager.extract_city(message)
|
| 65 |
if city:
|
| 66 |
chat_manager.context["city"] = city
|
| 67 |
|
| 68 |
if not chat_manager.context["city"]:
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Genera risposta
|
| 72 |
prompt = f"""
|
|
@@ -82,9 +88,13 @@ def respond(message, history):
|
|
| 82 |
|
| 83 |
try:
|
| 84 |
response = model.generate_content(prompt).text
|
| 85 |
-
|
| 86 |
except Exception as e:
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def reset_context():
|
| 90 |
chat_manager.context = {"city": None}
|
|
@@ -100,9 +110,21 @@ with gr.Blocks(css=CSS) as app:
|
|
| 100 |
send_btn = gr.Button("Invia")
|
| 101 |
reset_btn = gr.Button("Cambia Città", variant="secondary")
|
| 102 |
|
| 103 |
-
send_btn.click(
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
msg.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
app.launch()
|
|
|
|
| 8 |
model = genai.GenerativeModel('gemini-pro')
|
| 9 |
|
| 10 |
CSS = """
|
| 11 |
+
/* Stili CSS rimangono invariati */
|
| 12 |
body { background: #f5f7fa; font-family: 'Arial', sans-serif; }
|
| 13 |
.chat-container { max-width: 800px; margin: 0 auto; }
|
| 14 |
.msg-user { background: #e3f2fd; padding: 15px; border-radius: 15px; margin: 10px; max-width: 70%; float: right; }
|
|
|
|
| 60 |
|
| 61 |
chat_manager = ChatManager()
|
| 62 |
|
| 63 |
+
def respond(message, chat_history):
|
| 64 |
+
# Aggiungi il messaggio dell'utente alla cronologia
|
| 65 |
+
chat_history.append((message, ""))
|
| 66 |
+
|
| 67 |
# Estrai e mantieni contesto città
|
| 68 |
city = chat_manager.extract_city(message)
|
| 69 |
if city:
|
| 70 |
chat_manager.context["city"] = city
|
| 71 |
|
| 72 |
if not chat_manager.context["city"]:
|
| 73 |
+
bot_response = "Per favore dimmi prima di quale città vuoi informazioni! 🌍"
|
| 74 |
+
chat_history[-1] = (message, bot_response)
|
| 75 |
+
return "", chat_history
|
| 76 |
|
| 77 |
# Genera risposta
|
| 78 |
prompt = f"""
|
|
|
|
| 88 |
|
| 89 |
try:
|
| 90 |
response = model.generate_content(prompt).text
|
| 91 |
+
bot_response = chat_manager.format_response(response)
|
| 92 |
except Exception as e:
|
| 93 |
+
bot_response = f"⚠️ Errore: {str(e)}"
|
| 94 |
+
|
| 95 |
+
# Aggiorna l'ultimo messaggio nella cronologia
|
| 96 |
+
chat_history[-1] = (message, bot_response)
|
| 97 |
+
return "", chat_history
|
| 98 |
|
| 99 |
def reset_context():
|
| 100 |
chat_manager.context = {"city": None}
|
|
|
|
| 110 |
send_btn = gr.Button("Invia")
|
| 111 |
reset_btn = gr.Button("Cambia Città", variant="secondary")
|
| 112 |
|
| 113 |
+
send_btn.click(
|
| 114 |
+
fn=respond,
|
| 115 |
+
inputs=[msg, chatbot],
|
| 116 |
+
outputs=[msg, chatbot]
|
| 117 |
+
)
|
| 118 |
+
reset_btn.click(
|
| 119 |
+
fn=reset_context,
|
| 120 |
+
inputs=None,
|
| 121 |
+
outputs=[chatbot]
|
| 122 |
+
)
|
| 123 |
|
| 124 |
+
msg.submit(
|
| 125 |
+
fn=respond,
|
| 126 |
+
inputs=[msg, chatbot],
|
| 127 |
+
outputs=[msg, chatbot]
|
| 128 |
+
)
|
| 129 |
|
| 130 |
app.launch()
|