Update app.py
Browse files
app.py
CHANGED
|
@@ -9,63 +9,102 @@ import random
|
|
| 9 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 10 |
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def detect_language(text):
|
|
|
|
| 14 |
try:
|
| 15 |
lang = langdetect.detect(text)
|
| 16 |
-
return
|
| 17 |
except:
|
| 18 |
return "en"
|
| 19 |
|
| 20 |
-
# Function to search for reservation
|
| 21 |
def find_reservation_link(query):
|
|
|
|
| 22 |
search_url = f"https://www.googleapis.com/customsearch/v1?q={query}+booking&key=your_google_api_key&cx=your_custom_search_engine_id"
|
| 23 |
response = requests.get(search_url)
|
| 24 |
data = response.json()
|
| 25 |
|
|
|
|
| 26 |
if "items" in data and len(data["items"]) > 0:
|
| 27 |
return data["items"][0]["link"]
|
| 28 |
|
| 29 |
return "I couldn't find a link for that. Try searching manually."
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
options = {
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
}
|
| 38 |
return options.get(button_text, "")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# Ensure history is correctly formatted
|
| 43 |
-
history = history or [{"role": "assistant", "content": "Hey! I'm FlightAI. How can I help with your travel plans?"}]
|
| 44 |
-
|
| 45 |
-
# If button text was used
|
| 46 |
-
if message in ["Trip recommendations", "Send me somewhere!", "1 week planned vacations"]:
|
| 47 |
-
message = handle_button_click(message)
|
| 48 |
|
| 49 |
-
# Detect language
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
messages = [{"role": "system", "content": system_message[language]}] + history
|
| 59 |
messages.append({"role": "user", "content": message})
|
| 60 |
|
| 61 |
-
# Check
|
| 62 |
if any(keyword in message.lower() for keyword in ["book a flight", "hotel reservation", "car rental", "book a room"]):
|
| 63 |
link = find_reservation_link(message)
|
| 64 |
-
return history + [
|
| 65 |
|
| 66 |
# Call AI model
|
| 67 |
response = client.chat.completions.create(
|
| 68 |
-
model=
|
| 69 |
messages=messages,
|
| 70 |
temperature=0.7,
|
| 71 |
max_tokens=1024,
|
|
@@ -73,12 +112,10 @@ def chat_with_bot(message, history):
|
|
| 73 |
)
|
| 74 |
|
| 75 |
bot_reply = response.choices[0].message.content
|
| 76 |
-
history.append(
|
| 77 |
-
history.append({"role": "assistant", "content": bot_reply})
|
| 78 |
-
|
| 79 |
return history
|
| 80 |
|
| 81 |
-
# Gradio
|
| 82 |
with gr.Blocks(css="""
|
| 83 |
body { background-color: #A9B5DF; }
|
| 84 |
.gradio-container { background-color: #A9B5DF; }
|
|
@@ -86,26 +123,45 @@ with gr.Blocks(css="""
|
|
| 86 |
.gradio-button { background-color: #7886C7; color: white; border-radius: 10px; }
|
| 87 |
.gradio-chatbot-message { background-color: #FFF2F2; color: #2D336B; padding: 10px; border-radius: 10px; }
|
| 88 |
""") as demo:
|
| 89 |
-
gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
-
btn1 = gr.Button("
|
| 97 |
-
btn2 = gr.Button("
|
| 98 |
-
btn3 = gr.Button("
|
| 99 |
|
| 100 |
-
user_input = gr.Textbox(placeholder="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
# Button Click Event Handling
|
| 103 |
-
def button_click(btn_text):
|
| 104 |
-
return chat_with_bot(btn_text, chatbot.value)
|
| 105 |
-
|
| 106 |
-
btn1.click(button_click, inputs=[btn1], outputs=[chatbot])
|
| 107 |
-
btn2.click(button_click, inputs=[btn2], outputs=[chatbot])
|
| 108 |
-
btn3.click(button_click, inputs=[btn3], outputs=[chatbot])
|
| 109 |
-
user_input.submit(chat_with_bot, inputs=[user_input, chatbot], outputs=[chatbot])
|
| 110 |
-
|
| 111 |
demo.launch()
|
|
|
|
| 9 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 10 |
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
|
| 12 |
+
MODEL_ID = "llama-3.3-70b-versatile"
|
| 13 |
+
|
| 14 |
+
# Multilingual UI setup
|
| 15 |
+
translations = {
|
| 16 |
+
"en": {
|
| 17 |
+
"welcome": "Hey! I'm FlightAI. Tell me your travel plans or pick a button below!",
|
| 18 |
+
"questions": [
|
| 19 |
+
"Trip recommendations",
|
| 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 about booking a flight, hotel, or reservation, try to find a relevant link.",
|
| 24 |
+
"input_placeholder": "Type your travel question here..."
|
| 25 |
+
},
|
| 26 |
+
"fr": {
|
| 27 |
+
"welcome": "Salut ! Je suis FlightAI. Dis-moi ton projet de voyage ou choisis un bouton ci-dessous !",
|
| 28 |
+
"questions": [
|
| 29 |
+
"Recommandations de voyage",
|
| 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 une réservation.",
|
| 34 |
+
"input_placeholder": "Tapez votre question de voyage ici..."
|
| 35 |
+
},
|
| 36 |
+
"es": {
|
| 37 |
+
"welcome": "¡Hola! Soy FlightAI. Cuéntame tus planes de viaje o elige un botón a continuación.",
|
| 38 |
+
"questions": [
|
| 39 |
+
"Recomendaciones de viaje",
|
| 40 |
+
"¡Mándame a algún lugar!",
|
| 41 |
+
"Vacaciones planificadas de 1 semana"
|
| 42 |
+
],
|
| 43 |
+
"system_prompt": "Ayudas a los usuarios a planificar viajes. Responde de manera clara y concisa. Si te preguntan sobre reservas, proporciona un enlace.",
|
| 44 |
+
"input_placeholder": "Escribe tu pregunta sobre viajes aquí..."
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
def detect_language(text):
|
| 49 |
+
"""Detects whether the input text is in French, English, or Spanish."""
|
| 50 |
try:
|
| 51 |
lang = langdetect.detect(text)
|
| 52 |
+
return lang if lang in translations else "en"
|
| 53 |
except:
|
| 54 |
return "en"
|
| 55 |
|
| 56 |
+
# Function to search for reservation websites
|
| 57 |
def find_reservation_link(query):
|
| 58 |
+
"""Finds a website link for reservations using a simple search API."""
|
| 59 |
search_url = f"https://www.googleapis.com/customsearch/v1?q={query}+booking&key=your_google_api_key&cx=your_custom_search_engine_id"
|
| 60 |
response = requests.get(search_url)
|
| 61 |
data = response.json()
|
| 62 |
|
| 63 |
+
# Extract the first link
|
| 64 |
if "items" in data and len(data["items"]) > 0:
|
| 65 |
return data["items"][0]["link"]
|
| 66 |
|
| 67 |
return "I couldn't find a link for that. Try searching manually."
|
| 68 |
|
| 69 |
+
def handle_button_click(button_text, lang):
|
| 70 |
+
"""Handles button clicks by sending predefined queries."""
|
| 71 |
options = {
|
| 72 |
+
translations[lang]["questions"][0]: "What are the top travel spots right now?",
|
| 73 |
+
translations[lang]["questions"][1]: f"Pick a place for me. Maybe {random.choice(['Paris', 'Tokyo', 'New York', 'Barcelona', 'Rome', 'Bali', 'Dubai', 'Sydney'])}!",
|
| 74 |
+
translations[lang]["questions"][2]: "Plan a 1-week trip for me."
|
| 75 |
}
|
| 76 |
return options.get(button_text, "")
|
| 77 |
|
| 78 |
+
def chat_with_bot(message, history, lang="en"):
|
| 79 |
+
"""Handles user queries and maintains chat memory."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# Detect language if not provided
|
| 82 |
+
if lang == "auto":
|
| 83 |
+
lang = detect_language(message)
|
| 84 |
+
|
| 85 |
+
# If message is from a button click
|
| 86 |
+
if message in translations[lang]["questions"]:
|
| 87 |
+
message = handle_button_click(message, lang)
|
| 88 |
+
|
| 89 |
+
# Convert history to AI-compatible format
|
| 90 |
+
messages = [{"role": "system", "content": translations[lang]["system_prompt"]}]
|
| 91 |
+
|
| 92 |
+
for pair in history:
|
| 93 |
+
user_msg, bot_msg = pair
|
| 94 |
+
messages.append({"role": "user", "content": user_msg})
|
| 95 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 96 |
|
| 97 |
+
# Add latest user message
|
|
|
|
| 98 |
messages.append({"role": "user", "content": message})
|
| 99 |
|
| 100 |
+
# Check if the user is looking for reservations
|
| 101 |
if any(keyword in message.lower() for keyword in ["book a flight", "hotel reservation", "car rental", "book a room"]):
|
| 102 |
link = find_reservation_link(message)
|
| 103 |
+
return history + [(message, f"Here's a link that might help: {link}")]
|
| 104 |
|
| 105 |
# Call AI model
|
| 106 |
response = client.chat.completions.create(
|
| 107 |
+
model=MODEL_ID,
|
| 108 |
messages=messages,
|
| 109 |
temperature=0.7,
|
| 110 |
max_tokens=1024,
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
bot_reply = response.choices[0].message.content
|
| 115 |
+
history.append((message, bot_reply))
|
|
|
|
|
|
|
| 116 |
return history
|
| 117 |
|
| 118 |
+
# Gradio UI
|
| 119 |
with gr.Blocks(css="""
|
| 120 |
body { background-color: #A9B5DF; }
|
| 121 |
.gradio-container { background-color: #A9B5DF; }
|
|
|
|
| 123 |
.gradio-button { background-color: #7886C7; color: white; border-radius: 10px; }
|
| 124 |
.gradio-chatbot-message { background-color: #FFF2F2; color: #2D336B; padding: 10px; border-radius: 10px; }
|
| 125 |
""") as demo:
|
| 126 |
+
gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant / Votre Assistant de Voyage / Tu Asistente de Viaje*** ✈️\n")
|
| 127 |
|
| 128 |
+
# Language selector
|
| 129 |
+
language = gr.Radio(
|
| 130 |
+
choices=["English", "Français", "Español"],
|
| 131 |
+
value="English",
|
| 132 |
+
label="Language / Langue / Idioma"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
chatbot = gr.Chatbot(type="messages", value=[{"role": "assistant", "content": translations["en"]["welcome"]}])
|
| 136 |
+
|
| 137 |
+
state = gr.State([]) # Store conversation history
|
| 138 |
+
lang_code = gr.State("en")
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
+
btn1 = gr.Button(translations["en"]["questions"][0])
|
| 142 |
+
btn2 = gr.Button(translations["en"]["questions"][1])
|
| 143 |
+
btn3 = gr.Button(translations["en"]["questions"][2])
|
| 144 |
|
| 145 |
+
user_input = gr.Textbox(placeholder=translations["en"]["input_placeholder"])
|
| 146 |
+
|
| 147 |
+
# Function to update language settings
|
| 148 |
+
def update_ui_language(lang):
|
| 149 |
+
lang_code = {"English": "en", "Français": "fr", "Español": "es"}.get(lang, "en")
|
| 150 |
+
return lang_code, translations[lang_code]["questions"], translations[lang_code]["input_placeholder"], [{"role": "assistant", "content": translations[lang_code]["welcome"]}]
|
| 151 |
+
|
| 152 |
+
# Chat function
|
| 153 |
+
def submit_msg(message, history, lang):
|
| 154 |
+
return "", chat_with_bot(message, history, lang)
|
| 155 |
+
|
| 156 |
+
# Button click handlers
|
| 157 |
+
def button_click(button_text, history, lang):
|
| 158 |
+
return chat_with_bot(button_text, history, lang)
|
| 159 |
+
|
| 160 |
+
btn1.click(button_click, inputs=[btn1, state, lang_code], outputs=[chatbot])
|
| 161 |
+
btn2.click(button_click, inputs=[btn2, state, lang_code], outputs=[chatbot])
|
| 162 |
+
btn3.click(button_click, inputs=[btn3, state, lang_code], outputs=[chatbot])
|
| 163 |
+
user_input.submit(submit_msg, inputs=[user_input, state, lang_code], outputs=[chatbot])
|
| 164 |
+
|
| 165 |
+
language.change(update_ui_language, language, [lang_code, btn1, btn2, btn3, user_input, chatbot])
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
demo.launch()
|