munibz commited on
Commit
808a6fe
·
verified ·
1 Parent(s): 51edfdf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +195 -64
app.py CHANGED
@@ -1,64 +1,195 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()