alapl063 commited on
Commit
afe8500
·
verified ·
1 Parent(s): ac9ebe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -20,7 +20,7 @@ translations = {
20
  "Send me somewhere!",
21
  "1-week planned vacations"
22
  ],
23
- "system_prompt": "You help users plan trips. Keep responses short and clear. If a user asks specifically about booking a flight, hotel, or reservation, try to find a relevant link, else don't. Be friendly.",
24
  "input_placeholder": "Type your travel question here..."
25
  },
26
  "fr": {
@@ -30,7 +30,7 @@ translations = {
30
  "Envoyez-moi quelque part !",
31
  "Voyage d'une semaine planifié"
32
  ],
33
- "system_prompt": "Vous aidez les utilisateurs à planifier des voyages. Réponses courtes et simples. Fournissez un lien si on vous demande spécifiquement une réservation. Si ce n'est pas nécessaire, ne le faites pas. Soyez amical.",
34
  "input_placeholder": "Tapez votre question de voyage ici..."
35
  }
36
  }
@@ -94,7 +94,7 @@ with gr.Blocks(css="""
94
  """) as demo:
95
  gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant / Votre Assistant de Voyage*** ✈️\n")
96
 
97
- chatbot = gr.Chatbot(value=[("", translations["en"]["welcome"])], elem_id="chatbox")
98
 
99
  state = gr.State([]) # Store conversation history
100
 
@@ -108,15 +108,16 @@ with gr.Blocks(css="""
108
  def submit_msg(message, history):
109
  if not message.strip():
110
  return "", history # Ignore empty messages
111
-
112
  # Ensure history exists
113
  if not isinstance(history, list):
114
  history = []
115
-
116
  # Process chatbot response
117
  updated_history, _ = chat_with_bot(message, history)
 
 
118
 
119
- return updated_history, ""
120
 
121
  def button_click(btn_text, history):
122
  updated_history, _ = chat_with_bot(btn_text, history)
@@ -130,24 +131,24 @@ with gr.Blocks(css="""
130
  # Inject JavaScript for auto-scroll when a new message appears
131
  gr.HTML("""
132
  <script>
133
- function scrollChatToBottom() {
134
- let chatElements = document.querySelectorAll('[data-testid="chatbot"]');
135
- chatElements.forEach(chat => {
136
- setTimeout(() => { chat.scrollTop = chat.scrollHeight; }, 100);
137
- });
138
  }
139
-
 
140
  document.addEventListener("DOMContentLoaded", function() {
141
- let observer = new MutationObserver(scrollChatToBottom);
142
- let chatElements = document.querySelectorAll('[data-testid="chatbot"]');
143
- chatElements.forEach(chat => {
144
  observer.observe(chat, { childList: true, subtree: true });
145
- });
146
  });
147
-
148
- // Also trigger scroll when chatbot receives new messages
149
- setInterval(scrollChatToBottom, 500);
150
  </script>
151
  """)
152
 
 
 
153
  demo.launch()
 
20
  "Send me somewhere!",
21
  "1-week planned vacations"
22
  ],
23
+ "system_prompt": "You help users plan trips. Keep responses clear. If a user asks specifically about booking a flight, hotel, or reservation, try to find a relevant link, else don't. Be friendly.",
24
  "input_placeholder": "Type your travel question here..."
25
  },
26
  "fr": {
 
30
  "Envoyez-moi quelque part !",
31
  "Voyage d'une semaine planifié"
32
  ],
33
+ "system_prompt": "Vous aidez les utilisateurs à planifier des voyages. Réponses précises. Fournissez un lien si on vous demande spécifiquement une réservation. Si ce n'est pas nécessaire, ne le faites pas. Soyez amical.",
34
  "input_placeholder": "Tapez votre question de voyage ici..."
35
  }
36
  }
 
94
  """) as demo:
95
  gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant / Votre Assistant de Voyage*** ✈️\n")
96
 
97
+ chatbot = gr.Chatbot(value=[("", translations["en"]["welcome"])], elem_id="chat-container")
98
 
99
  state = gr.State([]) # Store conversation history
100
 
 
108
  def submit_msg(message, history):
109
  if not message.strip():
110
  return "", history # Ignore empty messages
111
+
112
  # Ensure history exists
113
  if not isinstance(history, list):
114
  history = []
115
+
116
  # Process chatbot response
117
  updated_history, _ = chat_with_bot(message, history)
118
+
119
+ return gr.update(value=updated_history), "" # Ensure UI refresh triggers scroll
120
 
 
121
 
122
  def button_click(btn_text, history):
123
  updated_history, _ = chat_with_bot(btn_text, history)
 
131
  # Inject JavaScript for auto-scroll when a new message appears
132
  gr.HTML("""
133
  <script>
134
+ function scrollToBottom() {
135
+ let chat = document.querySelector('#chat-container');
136
+ if (chat) {
137
+ setTimeout(() => { chat.scrollTop = chat.scrollHeight; }, 100);
138
+ }
139
  }
140
+
141
+ // Observe changes to chat and scroll when new messages arrive
142
  document.addEventListener("DOMContentLoaded", function() {
143
+ let observer = new MutationObserver(scrollToBottom);
144
+ let chat = document.querySelector('#chat-container');
145
+ if (chat) {
146
  observer.observe(chat, { childList: true, subtree: true });
147
+ }
148
  });
 
 
 
149
  </script>
150
  """)
151
 
152
+
153
+
154
  demo.launch()