R3BN's picture
Update app.py
fa35d87 verified
import gradio as gr
import requests
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Groq API endpoint and key
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# Check if the API key is loaded
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY not found in .env file")
def get_travel_guide_response(message, history):
# Prepare the conversation history
conversation = [
{"role": "system", "content": "You are a travel guide chatbot. Only provide information and answer questions strictly related to travel, destinations, itineraries, local tips, and cultural information. If a question is not related to travel, politely ask the user to only ask travel-related questions."}
]
for human, assistant in history:
conversation.append({"role": "user", "content": human})
conversation.append({"role": "assistant", "content": assistant})
conversation.append({"role": "user", "content": message})
# Make API request to Groq
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "mixtral-8x7b-32768",
"messages": conversation,
"max_tokens": 500
}
response = requests.post(GROQ_API_URL, headers=headers, json=data)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
return f"Error: {response.status_code} - {response.text}"
# Custom CSS for better UI
custom_css = """
.container {
max-width: 800px !important;
margin: auto;
padding-top: 1.5rem;
}
.chat-message {
padding: 1rem;
border-radius: 0.5rem;
margin-bottom: 1rem;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}
.user-message {
background-color: #e2e8f0;
}
.bot-message {
background-color: #edf2f7;
}
"""
# Create Gradio interface with custom theme
theme = gr.themes.Default().set(
body_background_fill="*neutral_50",
block_background_fill="*neutral_100",
button_primary_background_fill="*primary_500",
button_primary_background_fill_dark="*primary_600",
)
with gr.Blocks(theme=theme, css=custom_css) as iface:
gr.Markdown("# Travel Guide Chatbot")
gr.Markdown("Ask me about travel destinations, itineraries, and local tips!")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Type your travel question here")
clear = gr.Button("Clear")
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history):
bot_message = get_travel_guide_response(history[-1][0], history[:-1])
history[-1][1] = bot_message
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
# Add dark mode toggle
with gr.Row():
dark_mode = gr.Checkbox(label="Dark Mode", value=False)
def toggle_dark_mode(dark):
if dark:
return gr.themes.Default(primary_hue="blue", neutral_hue="slate", text_size="lg").set(
body_background_fill="*neutral_900",
block_background_fill="*neutral_800",
button_primary_background_fill="*primary_600",
button_primary_background_fill_dark="*primary_700",
)
else:
return gr.themes.Default(primary_hue="blue", neutral_hue="slate", text_size="lg").set(
body_background_fill="*neutral_50",
block_background_fill="*neutral_100",
button_primary_background_fill="*primary_500",
button_primary_background_fill_dark="*primary_600",
)
dark_mode.change(toggle_dark_mode, dark_mode, None)
# Launch the interface
iface.launch()
if __name__ == "__main__":
launch_chatbot()