| import os |
| import gradio as gr |
| from groq import Groq |
| import langdetect |
| import requests |
| import random |
|
|
|
|
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
| client = Groq(api_key=GROQ_API_KEY) |
|
|
| MODEL_ID = "llama-3.3-70b-versatile" |
|
|
| |
| translations = { |
| "en": { |
| "welcome": "Hey! I'm FlightAI. Tell me your travel plans or pick a button below!", |
| "questions": [ |
| "Trip recommendations", |
| "Send me somewhere!", |
| "1-week planned vacations" |
| ], |
| "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.", |
| "input_placeholder": "Type your travel question here..." |
| }, |
| "fr": { |
| "welcome": "Salut ! Je suis FlightAI. Dis-moi ton projet de voyage ou choisis un bouton ci-dessous !", |
| "questions": [ |
| "Recommandations de voyage", |
| "Envoyez-moi quelque part !", |
| "Voyage d'une semaine planifié" |
| ], |
| "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.", |
| "input_placeholder": "Tapez votre question de voyage ici..." |
| } |
| } |
|
|
| def detect_language(text): |
| """Detects whether the input text is in French, English, or Spanish.""" |
| try: |
| lang = langdetect.detect(text) |
| return lang if lang in translations else "en" |
| except: |
| return "en" |
|
|
| def handle_button_click(button_text, lang): |
| """Handles button clicks by sending predefined queries.""" |
| options = { |
| translations[lang]["questions"][0]: "What are the top travel spots right now?", |
| translations[lang]["questions"][1]: "Pick a place in the world for me.", |
| translations[lang]["questions"][2]: "Plan a 1-week trip for me." |
| } |
| return options.get(button_text, "") |
|
|
| def chat_with_bot(message, history, lang="en"): |
| """Handles user queries and maintains chat memory.""" |
| |
| if lang == "auto": |
| lang = detect_language(message) |
|
|
| |
| if message in translations[lang]["questions"]: |
| message = handle_button_click(message, lang) |
|
|
| |
| if history is None or not isinstance(history, list): |
| history = [] |
|
|
| |
| history.append((message, "")) |
|
|
| |
| response = client.chat.completions.create( |
| model=MODEL_ID, |
| messages=[{"role": "system", "content": translations[lang]["system_prompt"]}] + |
| [{"role": "user", "content": msg[0]} if msg[1] == "" else {"role": "assistant", "content": msg[1]} for msg in history], |
| temperature=0.7, |
| max_tokens=1024, |
| top_p=1 |
| ) |
|
|
| bot_reply = response.choices[0].message.content |
| history[-1] = (message, bot_reply) |
|
|
| return history, "" |
|
|
| def reset_chat(): |
| return [( "", translations["en"]["welcome"])], "" |
|
|
| |
| |
| with gr.Blocks(css=""" |
| body { background-color: #A9B5DF; } |
| .gradio-container { background-color: #A9B5DF; } |
| |
| .gradio-button { |
| background-color: #7886C7 !important; |
| color: white !important; |
| font-weight: bold !important; |
| } |
| |
| .gradio-button:hover { |
| background-color: #5f6bb4 !important; |
| } |
| |
| #chat-container { |
| height: 50vh !important; |
| max-height: 50vh !important; |
| } |
| """) as demo: |
| gr.Markdown(""" |
| # 🌍 ***FlightAI - Your Travel Assistant / Votre Assistant de Voyage*** ✈️ |
| |
| **EN:** Welcome to FlightAI! This chatbot helps you plan your trips effortlessly. You can ask for travel recommendations, request a surprise destination, or get a detailed one-week itinerary. Simply type your query in the chat box or use one of the predefined buttons below. |
| |
| **FR:** Bienvenue sur FlightAI ! Ce chatbot vous aide à organiser vos voyages facilement. Vous pouvez demander des recommandations de voyage, choisir une destination surprise ou obtenir un itinéraire détaillé pour une semaine. Tapez votre question dans la boîte de dialogue ou utilisez l'un des boutons prédéfinis ci-dessous. |
| """) |
| |
| chatbot = gr.Chatbot(value=[("", translations["en"]["welcome"])], elem_id="chat-container") |
|
|
|
|
| state = gr.State([]) |
| |
| with gr.Row(): |
| btn1 = gr.Button("Trip recommendations / Recommendations de Voyage", elem_classes=["gradio-button"]) |
| btn2 = gr.Button("Send me somewhere! / Envoyez-moi quelque part!", elem_classes=["gradio-button"]) |
| btn3 = gr.Button("1 week planned vacation / Voyage d'une semaine planifié", elem_classes=["gradio-button"]) |
|
|
| with gr.Row(): |
| user_input = gr.Textbox(placeholder="Type your travel question here...", interactive=True, scale=9) |
| send_button = gr.Button("Send", scale=1, elem_classes=["gradio-button"]) |
|
|
| with gr.Column(elem_classes=["centered"]): |
| reset_button = gr.Button("Reset Chat", elem_classes=["gradio-button"]) |
| |
| def submit_msg(message, history): |
| if not message.strip(): |
| return chat_with_bot("", history) |
| |
| if not isinstance(history, list): |
| history = [] |
| |
| updated_history, _ = chat_with_bot(message, history) |
| |
| return gr.update(value=updated_history), "" |
|
|
|
|
|
|
| def button_click(btn_text, history): |
| updated_history, _ = chat_with_bot(btn_text, history) |
| return updated_history, "" |
|
|
| user_input.submit(submit_msg, inputs=[user_input, chatbot], outputs=[chatbot, user_input]) |
| send_button.click(submit_msg, inputs=[user_input, chatbot], outputs=[chatbot, user_input], queue=False) |
| btn1.click(button_click, inputs=[btn1, chatbot], outputs=[chatbot, user_input]) |
| btn2.click(button_click, inputs=[btn2, chatbot], outputs=[chatbot, user_input]) |
| btn3.click(button_click, inputs=[btn3, chatbot], outputs=[chatbot, user_input]) |
| reset_button.click(reset_chat, inputs=[], outputs=[chatbot, user_input]) |
|
|
|
|
| gr.HTML(""" |
| <script> |
| function scrollToLatestMessage() { |
| let chatContainer = document.getElementById('chat-container'); |
| if (chatContainer) { |
| let messages = chatContainer.getElementsByClassName('message'); |
| if (messages.length > 0) { |
| let lastMessage = messages[messages.length - 1]; |
| lastMessage.scrollIntoView({ behavior: 'smooth', block: 'end' }); |
| } |
| } |
| } |
| |
| function observeChatUpdates() { |
| let chatContainer = document.getElementById('chat-container'); |
| if (chatContainer) { |
| let observer = new MutationObserver(scrollToLatestMessage); |
| observer.observe(chatContainer, { childList: true, subtree: true }); |
| } |
| } |
| |
| // Ensure scroll happens every time a new message appears |
| document.addEventListener("DOMContentLoaded", observeChatUpdates); |
| setInterval(observeChatUpdates, 1000); // Reattach observer in case of UI updates |
| </script> |
| """) |
| |
| demo.launch() |