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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
client = Groq(api_key= "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") # Assumes GROQ_API_KEY is in .env file
|
| 6 |
+
|
| 7 |
+
system_prompt = "You are a Travel Advisor. Provide helpful travel tips, recommend destinations, and suggest itineraries based on user queries."
|
| 8 |
+
|
| 9 |
+
def chatbot_response(user_message, history, output_length):
|
| 10 |
+
# Initialize chat history if None
|
| 11 |
+
if history is None:
|
| 12 |
+
history = []
|
| 13 |
+
|
| 14 |
+
# Build messages list
|
| 15 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 16 |
+
for user_msg, bot_msg in history:
|
| 17 |
+
messages.append({"role": "user", "content": user_msg})
|
| 18 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 19 |
+
messages.append({"role": "user", "content": user_message})
|
| 20 |
+
|
| 21 |
+
# Adjust response length
|
| 22 |
+
length_modifier = {
|
| 23 |
+
"Concise": "Respond briefly.",
|
| 24 |
+
"Moderate": "Respond with a balanced explanation.",
|
| 25 |
+
"Explained": "Provide a detailed and thorough response."
|
| 26 |
+
}
|
| 27 |
+
messages.append({"role": "system", "content": length_modifier[output_length]})
|
| 28 |
+
|
| 29 |
+
# Call Groq API
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model="llama-3.3-70b-versatile",
|
| 32 |
+
messages=messages
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Update history
|
| 36 |
+
history.append((user_message, response.choices[0].message.content))
|
| 37 |
+
return history, history, "" # Return history for chatbot/state and "" to clear textbox
|
| 38 |
+
|
| 39 |
+
# Custom CSS for a travel-themed UI
|
| 40 |
+
custom_css = """
|
| 41 |
+
/* General container styling */
|
| 42 |
+
.gradio-container {
|
| 43 |
+
background: linear-gradient(to bottom, #e6f3ff, #ffffff);
|
| 44 |
+
font-family: 'Poppins', sans-serif;
|
| 45 |
+
color: #333;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/* Header styling */
|
| 49 |
+
h1, h3 {
|
| 50 |
+
color: #1a5f7a;
|
| 51 |
+
text-align: center;
|
| 52 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
/* Chatbot area */
|
| 56 |
+
.gr-chatbot {
|
| 57 |
+
background: #ffffff;
|
| 58 |
+
border-radius: 15px;
|
| 59 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 60 |
+
padding: 20px;
|
| 61 |
+
max-height: 500px;
|
| 62 |
+
overflow-y: auto;
|
| 63 |
+
border: 2px solid #95c8d8;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
/* Chat messages */
|
| 67 |
+
.gr-chatbot .message {
|
| 68 |
+
border-radius: 10px;
|
| 69 |
+
margin: 10px 0;
|
| 70 |
+
padding: 10px 15px;
|
| 71 |
+
}
|
| 72 |
+
.gr-chatbot .user {
|
| 73 |
+
background: #95c8d8;
|
| 74 |
+
color: #fff;
|
| 75 |
+
text-align: right;
|
| 76 |
+
}
|
| 77 |
+
.gr-chatbot .assistant {
|
| 78 |
+
background: #e6f3ff;
|
| 79 |
+
color: #333;
|
| 80 |
+
text-align: left;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/* Textbox */
|
| 84 |
+
.gr-textbox input {
|
| 85 |
+
border: 2px solid #95c8d8;
|
| 86 |
+
border-radius: 10px;
|
| 87 |
+
padding: 10px;
|
| 88 |
+
font-size: 16px;
|
| 89 |
+
transition: border-color 0.3s ease;
|
| 90 |
+
}
|
| 91 |
+
.gr-textbox input:focus {
|
| 92 |
+
border-color: #1a5f7a;
|
| 93 |
+
outline: none;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
/* Radio buttons */
|
| 97 |
+
.gr-radio {
|
| 98 |
+
background: #ffffff;
|
| 99 |
+
border-radius: 10px;
|
| 100 |
+
padding: 15px;
|
| 101 |
+
border: 2px solid #95c8d8;
|
| 102 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 103 |
+
}
|
| 104 |
+
.gr-radio label {
|
| 105 |
+
color: #1a5f7a;
|
| 106 |
+
font-weight: 600;
|
| 107 |
+
}
|
| 108 |
+
.gr-radio input[type="radio"]:checked + label {
|
| 109 |
+
color: #0a3d62;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
/* Buttons */
|
| 113 |
+
.gr-button {
|
| 114 |
+
background: #1a5f7a;
|
| 115 |
+
color: #ffffff;
|
| 116 |
+
border: none;
|
| 117 |
+
border-radius: 10px;
|
| 118 |
+
padding: 10px 20px;
|
| 119 |
+
font-size: 16px;
|
| 120 |
+
font-weight: 600;
|
| 121 |
+
transition: background 0.3s ease, transform 0.2s ease;
|
| 122 |
+
}
|
| 123 |
+
.gr-button:hover {
|
| 124 |
+
background: #0a3d62;
|
| 125 |
+
transform: translateY(-2px);
|
| 126 |
+
}
|
| 127 |
+
.gr-button:active {
|
| 128 |
+
transform: translateY(0);
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/* Row layout */
|
| 132 |
+
.gr-row {
|
| 133 |
+
gap: 20px;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/* Add a subtle background image */
|
| 137 |
+
body::before {
|
| 138 |
+
content: '';
|
| 139 |
+
position: fixed;
|
| 140 |
+
top: 0;
|
| 141 |
+
left: 0;
|
| 142 |
+
width: 100%;
|
| 143 |
+
height: 100%;
|
| 144 |
+
background: url('https://www.transparenttextures.com/patterns/white-wave.png');
|
| 145 |
+
opacity: 0.1;
|
| 146 |
+
z-index: -1;
|
| 147 |
+
}
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
# Create Gradio interface with Blocks
|
| 151 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 152 |
+
gr.Markdown("# 🌍 My Travel Advisor Chatbot")
|
| 153 |
+
gr.Markdown("Embark on your next adventure with personalized travel tips and itineraries!")
|
| 154 |
+
|
| 155 |
+
# Chatbot display
|
| 156 |
+
chatbot = gr.Chatbot(label="Travel Chat", height=500)
|
| 157 |
+
|
| 158 |
+
# Input components
|
| 159 |
+
with gr.Row():
|
| 160 |
+
user_message = gr.Textbox(
|
| 161 |
+
label="Your Travel Question",
|
| 162 |
+
placeholder="Ask about destinations, tips, or itineraries...",
|
| 163 |
+
lines=2
|
| 164 |
+
)
|
| 165 |
+
output_length = gr.Radio(
|
| 166 |
+
choices=["Concise", "Moderate", "Explained"],
|
| 167 |
+
value="Moderate",
|
| 168 |
+
label="Response Length",
|
| 169 |
+
info="Choose how detailed you want the response to be."
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Submit and clear buttons
|
| 173 |
+
with gr.Row():
|
| 174 |
+
submit_button = gr.Button("Send")
|
| 175 |
+
clear_button = gr.Button("Clear Chat")
|
| 176 |
+
|
| 177 |
+
# State to store chat history
|
| 178 |
+
chat_state = gr.State(value=[])
|
| 179 |
+
|
| 180 |
+
# Connect button to function
|
| 181 |
+
submit_button.click(
|
| 182 |
+
fn=chatbot_response,
|
| 183 |
+
inputs=[user_message, chat_state, output_length],
|
| 184 |
+
outputs=[chatbot, chat_state, user_message]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Clear chat functionality
|
| 188 |
+
clear_button.click(
|
| 189 |
+
fn=lambda: ([], [], ""),
|
| 190 |
+
inputs=None,
|
| 191 |
+
outputs=[chatbot, chat_state, user_message]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Launch the app
|
| 195 |
+
demo.launch()
|