AlessandroArgiolas02 commited on
Commit
78c4391
·
verified ·
1 Parent(s): 81d48dc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import os
4
+ import re
5
+
6
+ # Configurazione API
7
+ GEMINI_KEY = os.getenv("GEMINI")
8
+ genai.configure(api_key=GEMINI_KEY)
9
+ model = genai.GenerativeModel('gemini-pro')
10
+
11
+ CSS = """
12
+ body { background: #f0f2f6; font-family: 'Arial', sans-serif; }
13
+ .msg-bot { background: #ffffff; padding: 15px; border-radius: 15px; margin: 10px; max-width: 80%; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
14
+ .msg-user { background: #e3f2fd; padding: 15px; border-radius: 15px; margin: 10px; max-width: 80%; margin-left: 20%; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
15
+ #component-0 { max-width: 800px; margin: auto; }
16
+ footer { display: none !important; }
17
+ .itinerary-day { border-left: 3px solid #2196F3; padding-left: 15px; margin: 15px 0; }
18
+ .highlight { color: #2196F3; font-weight: bold; }
19
+ """
20
+
21
+ def extract_city(text):
22
+ city_pattern = r"\b(a|in|a|per)\s+([A-Za-z\s]+?)(?=\?| per|$)"
23
+ match = re.search(city_pattern, text, re.IGNORECASE)
24
+ return match.group(2).strip() if match else None
25
+
26
+ def generate_response(history, message):
27
+ current_city = None
28
+ response = ""
29
+
30
+ # Analizza l'ultimo messaggio per la città
31
+ last_message = message.lower()
32
+ current_city = extract_city(last_message)
33
+
34
+ # Cerca nella cronologia per città precedenti
35
+ if not current_city:
36
+ for msg, _ in reversed(history):
37
+ city = extract_city(msg)
38
+ if city:
39
+ current_city = city
40
+ break
41
+
42
+ if not current_city:
43
+ return "Per favore dimmi di quale città vuoi informazioni! 🌍"
44
+
45
+ # Genera contenuto con Gemini
46
+ prompt = f"""
47
+ Sei un esperto guida turistica. Rispondi in italiano.
48
+ Utente chiede: {message}
49
+ Città: {current_city}
50
+
51
+ Genera una risposta ben formattata in Markdown con:
52
+ 1. Cose da vedere (monumenti principali)
53
+ 2. Dove mangiare (3 ristoranti tipici)
54
+ 3. Dove dormire (3 opzioni alloggio)
55
+ 4. Cose da fare (attività uniche)
56
+ 5. Itinerari per 1, 2 e 3 giorni
57
+
58
+ Struttura:
59
+ # {current_city} 🏙️
60
+ ## 🏛️ Da Vedere
61
+ - [Luogo 1]
62
+ - [Luogo 2]
63
+
64
+ ## 🍝 Dove Mangiare
65
+ 1. **Ristorante 1** - Specialità...
66
+
67
+ ## 🛌 Dove Dormire
68
+ - Hotel 1 ★★★
69
+
70
+ ## 🎡 Cose da Fare
71
+ - Attività 1
72
+
73
+ ## 📅 Itinerari
74
+ **1 Giorno**:
75
+ - Mattina: ...
76
+ """
77
+
78
+ try:
79
+ response = model.generate_content(prompt).text
80
+ # Formattazione avanzata
81
+ response = response.replace("**", "#") # Per i titoli
82
+ response = re.sub(r'\*(.*?)\*', r'**\1**', response) # Grassetti
83
+ response += f"\n\n🔍 Posso darti maggiori dettagli su qualsiasi punto! Cosa ti interessa?"
84
+ except Exception as e:
85
+ response = f"⚠️ Si è verificato un errore: {str(e)}"
86
+
87
+ return response
88
+
89
+ with gr.Blocks(css=CSS) as demo:
90
+ chatbot = gr.Chatbot(
91
+ label="Chat Turistica",
92
+ bubble_full_width=False,
93
+ show_label=False,
94
+ avatar_images=(
95
+ "https://i.imgur.com/7kQEsHU.png",
96
+ "https://i.imgur.com/7W7QEs2.png"
97
+ )
98
+ )
99
+ msg = gr.Textbox(label="Scrivi qui...")
100
+ clear = gr.ClearButton([msg, chatbot])
101
+
102
+ def init_message(history):
103
+ history += [[None, "Ciao! Sono la tua guida turistica virtuale 🌍\nDimmi una città e ti aiuterò con:\n- Cose da vedere\n- Dove mangiare\n- Itinerari\nCosa ti interessa sapere?"]]
104
+ return history
105
+
106
+ def respond(message, history):
107
+ history += [[message, None]]
108
+ bot_message = generate_response(history, message)
109
+ history[-1][1] = bot_message
110
+ return history
111
+
112
+ msg.submit(
113
+ respond,
114
+ [msg, chatbot],
115
+ [chatbot],
116
+ queue=False
117
+ ).then(
118
+ lambda: "",
119
+ None,
120
+ [msg]
121
+ )
122
+
123
+ demo.load(init_message, None, [chatbot])
124
+
125
+ demo.launch(share=True)