munibz commited on
Commit
90f1a0e
·
verified ·
1 Parent(s): 63ff994

Update app.py

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