Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import langdetect
|
| 4 |
import requests
|
| 5 |
import random
|
| 6 |
-
from groq import Groq
|
| 7 |
-
|
| 8 |
|
| 9 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 10 |
|
| 11 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 12 |
|
| 13 |
-
model="llama-3.3-70b-versatile"
|
| 14 |
-
import os
|
| 15 |
-
import gradio as gr
|
| 16 |
-
from huggingface_hub import InferenceClient
|
| 17 |
-
import langdetect
|
| 18 |
-
import requests
|
| 19 |
-
import random
|
| 20 |
-
|
| 21 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 22 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 23 |
-
|
| 24 |
def detect_language(text):
|
| 25 |
"""Detects whether the input text is in French or English."""
|
| 26 |
lang = langdetect.detect(text)
|
|
@@ -33,6 +21,7 @@ def find_reservation_link(query):
|
|
| 33 |
response = requests.get(search_url)
|
| 34 |
data = response.json()
|
| 35 |
|
|
|
|
| 36 |
if "items" in data and len(data["items"]) > 0:
|
| 37 |
return data["items"][0]["link"]
|
| 38 |
|
|
@@ -49,51 +38,71 @@ def handle_button_click(button_text):
|
|
| 49 |
return "Plan a 1-week trip for me."
|
| 50 |
return ""
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
history: list[tuple[str, str]],
|
| 55 |
-
system_message,
|
| 56 |
-
max_tokens,
|
| 57 |
-
temperature,
|
| 58 |
-
top_p,
|
| 59 |
-
):
|
| 60 |
-
messages = [{"role": "system", "content": system_message}]
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
messages.append({"role": "user", "content": val[0]})
|
| 65 |
-
if val[1]:
|
| 66 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 67 |
-
|
| 68 |
-
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
for
|
| 73 |
-
messages,
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 87 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 88 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 89 |
-
gr.Slider(
|
| 90 |
-
minimum=0.1,
|
| 91 |
-
maximum=1.0,
|
| 92 |
-
value=0.95,
|
| 93 |
-
step=0.05,
|
| 94 |
-
label="Top-p (nucleus sampling)",
|
| 95 |
-
),
|
| 96 |
-
],
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
import langdetect
|
| 5 |
import requests
|
| 6 |
import random
|
|
|
|
|
|
|
| 7 |
|
| 8 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 9 |
|
| 10 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def detect_language(text):
|
| 13 |
"""Detects whether the input text is in French or English."""
|
| 14 |
lang = langdetect.detect(text)
|
|
|
|
| 21 |
response = requests.get(search_url)
|
| 22 |
data = response.json()
|
| 23 |
|
| 24 |
+
# Extract the first link
|
| 25 |
if "items" in data and len(data["items"]) > 0:
|
| 26 |
return data["items"][0]["link"]
|
| 27 |
|
|
|
|
| 38 |
return "Plan a 1-week trip for me."
|
| 39 |
return ""
|
| 40 |
|
| 41 |
+
def chat_with_bot(message, history):
|
| 42 |
+
"""Handles user queries and maintains chat memory."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
if message in ["Trip recommendations", "Send me somewhere!", "1 week planned vacations"]:
|
| 45 |
+
message = handle_button_click(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Detect language (default to English if detection is uncertain)
|
| 48 |
+
language = detect_language(message)
|
| 49 |
+
|
| 50 |
+
system_message = {
|
| 51 |
+
"en": "You help users plan trips. Keep responses short and clear. If a user asks about specifically booking a flight, hotel, or reservation, try to find a relevant link. Else, don't give a link.",
|
| 52 |
+
"fr": "Vous aidez les utilisateurs à planifier des voyages. Réponses courtes et simples. Si un utilisateur demande spécifiquement une réservation d'hôtel ou de vol, essayez de trouver un lien pertinent. Sinon, pas besoin."
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# Convert history into Groq-compatible message format
|
| 56 |
+
messages = [{"role": "system", "content": system_message[language]}]
|
| 57 |
|
| 58 |
+
for user_msg, bot_msg in history:
|
| 59 |
+
messages.append({"role": "user", "content": user_msg})
|
| 60 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 61 |
+
|
| 62 |
+
# Add the latest user message
|
| 63 |
+
messages.append({"role": "user", "content": message})
|
| 64 |
+
|
| 65 |
+
# Check if the user is looking for reservations
|
| 66 |
+
if any(keyword in message.lower() for keyword in ["book a flight", "hotel reservation", "car rental", "book a room"]):
|
| 67 |
+
link = find_reservation_link(message)
|
| 68 |
+
return history + [(message, f"Here's a link that might help: {link}")], ""
|
| 69 |
+
|
| 70 |
+
# Call Groq's AI model
|
| 71 |
+
response = client.chat.completions.create(
|
| 72 |
+
model="llama-3.3-70b-versatile",
|
| 73 |
+
messages=messages,
|
| 74 |
+
temperature=0.7,
|
| 75 |
+
max_tokens=1024,
|
| 76 |
+
top_p=1
|
| 77 |
+
)
|
| 78 |
|
| 79 |
+
bot_reply = response.choices[0].message.content
|
| 80 |
+
history.append((message, bot_reply))
|
| 81 |
+
return history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Gradio interface with memory
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant - Votre Assistant de Voyage*** ✈️\n")
|
| 86 |
+
|
| 87 |
+
chatbot = gr.Chatbot()
|
| 88 |
+
chatbot.value = [
|
| 89 |
+
("", "Hey! I'm FlightAI. Tell me your travel plans or pick a button below!\n\nSalut! Je suis FlightAI. Dis-moi ton projet de voyage ou choisis un bouton ci-dessous!")
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
btn1 = gr.Button("Trip recommendations / Recommendations de Voyage")
|
| 94 |
+
btn2 = gr.Button("Send me somewhere! / Envoyez-moi quelques part!")
|
| 95 |
+
btn3 = gr.Button("1 week planned vacations / Voyage d'une semaine planifié")
|
| 96 |
+
|
| 97 |
+
user_input = gr.Textbox(placeholder="Type your travel question here...")
|
| 98 |
+
|
| 99 |
+
def button_click(btn_text):
|
| 100 |
+
response, _ = chat_with_bot(btn_text, chatbot.value)
|
| 101 |
+
return response, ""
|
| 102 |
+
|
| 103 |
+
btn1.click(button_click, inputs=[btn1], outputs=[chatbot, user_input])
|
| 104 |
+
btn2.click(button_click, inputs=[btn2], outputs=[chatbot, user_input])
|
| 105 |
+
btn3.click(button_click, inputs=[btn3], outputs=[chatbot, user_input])
|
| 106 |
+
user_input.submit(chat_with_bot, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
|
| 107 |
+
|
| 108 |
+
demo.launch()
|