File size: 5,043 Bytes
90f1a0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d44663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import gradio as gr
import os
from groq import Groq

import os

api_key = os.getenv("GROQ_API_KEY")
client = Groq(api_key=api_key)

#client = Groq(api_key= "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")  # Assumes GROQ_API_KEY is in .env file

system_prompt = "You are a Travel Advisor. Provide helpful travel tips, recommend destinations, and suggest itineraries based on user queries."

def chatbot_response(user_message, history, output_length):
    # Initialize chat history if None
    if history is None:
        history = []
    
    # Build messages list
    messages = [{"role": "system", "content": system_prompt}]
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})
    messages.append({"role": "user", "content": user_message})
    
    # Adjust response length
    length_modifier = {
        "Concise": "Respond briefly.",
        "Moderate": "Respond with a balanced explanation.",
        "Explained": "Provide a detailed and thorough response."
    }
    messages.append({"role": "system", "content": length_modifier[output_length]})
    
    # Call Groq API
    response = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=messages
    )
    
    # Update history
    history.append((user_message, response.choices[0].message.content))
    return history, history, ""  # Return history for chatbot/state and "" to clear textbox

# Custom CSS for a travel-themed UI
custom_css = """
/* General container styling */
.gradio-container {
    background: linear-gradient(to bottom, #e6f3ff, #ffffff);
    font-family: 'Poppins', sans-serif;
    color: #333;
}

/* Header styling */
h1, h3 {
    color: #1a5f7a;
    text-align: center;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}

/* Chatbot area */
.gr-chatbot {
    background: #ffffff;
    border-radius: 15px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    padding: 20px;
    max-height: 500px;
    overflow-y: auto;
    border: 2px solid #95c8d8;
}

/* Chat messages */
.gr-chatbot .message {
    border-radius: 10px;
    margin: 10px 0;
    padding: 10px 15px;
}
.gr-chatbot .user {
    background: #95c8d8;
    color: #fff;
    text-align: right;
}
.gr-chatbot .assistant {
    background: #e6f3ff;
    color: #333;
    text-align: left;
}

/* Textbox */
.gr-textbox input {
    border: 2px solid #95c8d8;
    border-radius: 10px;
    padding: 10px;
    font-size: 16px;
    transition: border-color 0.3s ease;
}
.gr-textbox input:focus {
    border-color: #1a5f7a;
    outline: none;
}

/* Radio buttons */
.gr-radio {
    background: #ffffff;
    border-radius: 10px;
    padding: 15px;
    border: 2px solid #95c8d8;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.gr-radio label {
    color: #1a5f7a;
    font-weight: 600;
}
.gr-radio input[type="radio"]:checked + label {
    color: #0a3d62;
}

/* Buttons */
.gr-button {
    background: #1a5f7a;
    color: #ffffff;
    border: none;
    border-radius: 10px;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: 600;
    transition: background 0.3s ease, transform 0.2s ease;
}
.gr-button:hover {
    background: #0a3d62;
    transform: translateY(-2px);
}
.gr-button:active {
    transform: translateY(0);
}

/* Row layout */
.gr-row {
    gap: 20px;
}

/* Add a subtle background image */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url('https://www.transparenttextures.com/patterns/white-wave.png');
    opacity: 0.1;
    z-index: -1;
}
"""

# Create Gradio interface with Blocks
with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("# 🌍 My Travel Advisor Chatbot")
    gr.Markdown("Embark on your next adventure with personalized travel tips and itineraries!")
    
    # Chatbot display
    chatbot = gr.Chatbot(label="Travel Chat", height=500)
    
    # Input components
    with gr.Row():
        user_message = gr.Textbox(
            label="Your Travel Question",
            placeholder="Ask about destinations, tips, or itineraries...",
            lines=2
        )
        output_length = gr.Radio(
            choices=["Concise", "Moderate", "Explained"],
            value="Moderate",
            label="Response Length",
            info="Choose how detailed you want the response to be."
        )
    
    # Submit and clear buttons
    with gr.Row():
        submit_button = gr.Button("Send")
        clear_button = gr.Button("Clear Chat")
    
    # State to store chat history
    chat_state = gr.State(value=[])
    
    # Connect button to function
    submit_button.click(
        fn=chatbot_response,
        inputs=[user_message, chat_state, output_length],
        outputs=[chatbot, chat_state, user_message]
    )
    
    # Clear chat functionality
    clear_button.click(
        fn=lambda: ([], [], ""),
        inputs=None,
        outputs=[chatbot, chat_state, user_message]
    )

# Launch the app
demo.launch()