abdo-Mansour commited on
Commit
a1ad8cb
Β·
1 Parent(s): 7662a43

edited the readme file

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +11 -351
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: BuyVerse
3
  emoji: πŸ›’
4
- colorFrom: quartz
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.33.1
 
1
  ---
2
  title: BuyVerse
3
  emoji: πŸ›’
4
+ colorFrom: indigo
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.33.1
app.py CHANGED
@@ -1,361 +1,21 @@
1
  import gradio as gr
2
- import random
3
 
4
- def generate_sample_products(count=5):
5
- """Generate sample product data"""
6
- products = []
7
- for i in range(1, count + 1):
8
- product = {
9
- "name": f"Sample Product {i}",
10
- "price": f"${random.randint(10, 500):.2f}",
11
- "rating": f"{random.uniform(1, 5):.1f} ",
12
- "description": f"This is a sample product description {i} with some details about the item."
13
- }
14
- products.append(product)
15
- return products
16
 
17
- def shopping_agent(user_request, chat_history):
18
- # Simulate LlamaIndex response (replace with actual call)
19
- response = {
20
- "chat_response": f"I found some products based on: {user_request}",
21
- "agent_actions": "Searched for products\nFiltered by price\nSorted by rating"
22
- }
23
-
24
- # Generate sample products
25
- products = generate_sample_products(3) # Show 3 recommended products
26
-
27
- # Format the chat response
28
- chat_message = response.get("chat_response", "Processing...")
29
- chat_history.append((user_request, chat_message))
30
-
31
- return chat_history, response["agent_actions"], products
32
-
33
- # Custom CSS for better styling
34
- css = """
35
- :root {
36
- --primary-color: #2563eb;
37
- --secondary-color: #1e40af;
38
- --accent-color: #3b82f6;
39
- --text-color: #1f2937;
40
- --light-bg: #f3f4f6;
41
- --card-bg: #ffffff;
42
- --border-color: #e5e7eb;
43
- }
44
-
45
- body {
46
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
47
- color: var(--text-color);
48
- background-color: var(--light-bg);
49
- margin: 0;
50
- padding: 0;
51
- overflow-x: hidden;
52
- width: 100%;
53
- box-sizing: border-box;
54
- }
55
-
56
- * {
57
- box-sizing: border-box;
58
- }
59
-
60
- #main-container {
61
- width: 100%;
62
- max-width: 1600px;
63
- margin: 0 auto;
64
- padding: 0 1rem;
65
- }
66
-
67
- .header-container {
68
- text-align: center;
69
- margin-bottom: 1.5rem;
70
- padding: 1rem;
71
- max-width: 100%;
72
- }
73
-
74
- .header-container h1 {
75
- margin-bottom: 0.5rem !important;
76
- font-size: 2rem !important;
77
- }
78
-
79
- .header-container p {
80
- color: #6b7280;
81
- font-size: 1rem;
82
- max-width: 600px;
83
- margin: 0 auto;
84
- padding: 0 1rem;
85
- }
86
-
87
- .scrollable-column {
88
- height: calc(100vh - 200px);
89
- overflow-y: auto;
90
- overflow-x: hidden;
91
- padding: 1rem;
92
- border-radius: 12px;
93
- background: var(--card-bg);
94
- box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
95
- margin: 0 0.25rem;
96
- flex: 1;
97
- min-width: 0;
98
- display: flex;
99
- flex-direction: column;
100
- }
101
-
102
- .product-card {
103
- border: 1px solid var(--border-color);
104
- border-radius: 12px;
105
- padding: 15px;
106
- margin-bottom: 15px;
107
- background: var(--card-bg);
108
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
109
- transition: transform 0.2s ease, box-shadow 0.2s ease;
110
- width: 100%;
111
- word-wrap: break-word;
112
- overflow-wrap: break-word;
113
- }
114
-
115
- .product-card:hover {
116
- transform: translateY(-2px);
117
- box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
118
- }
119
-
120
- .product-card h3 {
121
- margin: 0 0 10px 0;
122
- color: var(--primary-color);
123
- font-size: 1.25rem;
124
- font-weight: 600;
125
- }
126
-
127
- .product-card .price {
128
- font-weight: 700;
129
- color: #059669;
130
- margin: 8px 0;
131
- font-size: 1.1rem;
132
- }
133
-
134
- .product-card .rating {
135
- color: #f59e0b;
136
- margin: 8px 0;
137
- display: flex;
138
- align-items: center;
139
- gap: 4px;
140
- }
141
-
142
- .product-card p {
143
- color: #4b5563;
144
- margin: 12px 0;
145
- line-height: 1.5;
146
- }
147
-
148
- .product-card button {
149
- background-color: var(--primary-color) !important;
150
- color: white !important;
151
- border: none !important;
152
- padding: 10px 20px !important;
153
- border-radius: 8px !important;
154
- font-weight: 500 !important;
155
- transition: background-color 0.2s ease !important;
156
- }
157
-
158
- .product-card button:hover {
159
- background-color: var(--secondary-color) !important;
160
- }
161
-
162
- .action-log {
163
- height: calc(100% - 50px);
164
- overflow-y: auto;
165
- border: 1px solid var(--border-color);
166
- padding: 16px;
167
- border-radius: 8px;
168
- background: var(--card-bg);
169
- box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
170
- flex-grow: 1;
171
- min-height: 100px;
172
- }
173
 
174
- .action-log div {
175
- padding: 8px 0;
176
- border-bottom: 1px solid var(--border-color);
177
- color: #4b5563;
178
- }
179
-
180
- .action-log div:last-child {
181
- border-bottom: none;
182
- }
183
-
184
- .gr-chat {
185
- border-radius: 12px !important;
186
- border: 1px solid var(--border-color) !important;
187
- background: var(--card-bg) !important;
188
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
189
- max-width: 100% !important;
190
- height: calc(100% - 60px) !important;
191
- min-height: 200px;
192
- margin-bottom: 12px;
193
- }
194
-
195
- .chat-column {
196
- display: flex;
197
- flex-direction: column;
198
- }
199
-
200
- .chatbot-container {
201
- flex-grow: 1;
202
- min-height: 200px;
203
- display: flex;
204
- flex-direction: column;
205
- }
206
-
207
- #main-row {
208
- width: 100%;
209
- margin: 0;
210
- gap: 16px;
211
- }
212
-
213
- .chat-input {
214
- width: 100% !important;
215
- max-width: 100% !important;
216
- }
217
-
218
- .send-button {
219
- align-self: flex-end;
220
- height: 100%;
221
- }
222
-
223
- @media (max-width: 1200px) {
224
- #main-row {
225
- flex-direction: column;
226
- }
227
 
228
- .scrollable-column {
229
- width: 100% !important;
230
- margin: 0 0 16px 0 !important;
231
- height: auto !important;
232
- max-height: 50vh;
233
- }
234
-
235
- .chatbot-container {
236
- min-height: 300px;
237
- }
238
- }
239
-
240
- /* Fix for Gradio's internal styles */
241
- .gr-block {
242
- margin: 0 !important;
243
- padding: 0 !important;
244
- }
245
 
246
- .gr-box {
247
- border: none !important;
248
- padding: 0 !important;
249
- }
250
 
251
- .gr-input {
252
- border-radius: 8px !important;
253
- border: 1px solid var(--border-color) !important;
254
- padding: 12px !important;
255
- max-width: 100% !important;
256
- }
257
 
258
- .gr-button {
259
- border-radius: 8px !important;
260
- font-weight: 500 !important;
261
- }
262
-
263
- h1 {
264
- color: var(--primary-color) !important;
265
- font-size: 2.5rem !important;
266
- font-weight: 700 !important;
267
- margin-bottom: 1.5rem !important;
268
- }
269
-
270
- h2 {
271
- color: var(--text-color) !important;
272
- font-size: 1.5rem !important;
273
- font-weight: 600 !important;
274
- margin-bottom: 1rem !important;
275
- }
276
- """
277
-
278
- def create_product_card(product):
279
- """Create HTML for a product card"""
280
- return f"""
281
- <div class="product-card">
282
- <h3>{product['name']}</h3>
283
- <div class="price">{product['price']}</div>
284
- <div class="rating">β˜… {product['rating']}</div>
285
- <p>{product['description']}</p>
286
- <button class="gr-button gr-button-secondary">View Details</button>
287
- </div>
288
- """
289
-
290
- with gr.Blocks(css=css) as demo:
291
- with gr.Column(elem_id="main-container"):
292
- with gr.Column(elem_classes="header-container"):
293
- gr.Markdown("# Buyverse Shopping Assistant")
294
- gr.Markdown("Your AI-powered shopping companion that helps you find the perfect products")
295
-
296
- with gr.Row(equal_height=True, elem_id="main-row"):
297
- # Left Column: Recommended Products
298
- with gr.Column(scale=3, min_width=250, elem_classes="scrollable-column"):
299
- gr.Markdown("## Recommended Products")
300
- recommended_products = gr.HTML()
301
-
302
- # Middle Column: Chat Interface
303
- with gr.Column(scale=5, min_width=350, elem_classes=["scrollable-column", "chat-column"]):
304
- chat_preview = gr.Chatbot(label="Chat with Assistant", height=400, show_label=True, elem_classes=["chatbot-container"])
305
- with gr.Row() as input_row:
306
- user_input = gr.Textbox(
307
- label="",
308
- placeholder="Describe what you're looking for...",
309
- container=False,
310
- scale=8,
311
- show_label=False,
312
- elem_classes=["chat-input"],
313
- max_lines=3
314
- )
315
- submit_btn = gr.Button("Send", variant="primary", scale=1, min_width=80, elem_classes=["send-button"])
316
-
317
- # Right Column: Agent Actions Log
318
- with gr.Column(scale=2, min_width=220, elem_classes="scrollable-column"):
319
- gr.Markdown("## Agent Actions")
320
- action_log = gr.HTML("<div class='action-log'></div>")
321
-
322
- # Initialize with some sample products
323
- demo.load(
324
- fn=lambda: [create_product_card(p) for p in generate_sample_products(5)],
325
- outputs=[recommended_products]
326
- )
327
-
328
- def update_chat_and_actions(user_input, chat_history):
329
- if not user_input.strip():
330
- return "", chat_history, ""
331
-
332
- # Get agent response
333
- chat_history, actions, products = shopping_agent(user_input, chat_history)
334
-
335
- # Update products display
336
- product_cards = "".join([create_product_card(p) for p in products])
337
-
338
- # Format actions log
339
- action_log_content = "<div class='action-log'>"
340
- for action in actions.split("\n"):
341
- if action.strip():
342
- action_log_content += f"<div>β€’ {action}</div>"
343
- action_log_content += "</div>"
344
-
345
- return "", chat_history, action_log_content, product_cards
346
-
347
- # Connect the submit button and Enter key
348
- submit_btn.click(
349
- fn=update_chat_and_actions,
350
- inputs=[user_input, chat_preview],
351
- outputs=[user_input, chat_preview, action_log, recommended_products]
352
- )
353
-
354
- user_input.submit(
355
- fn=update_chat_and_actions,
356
- inputs=[user_input, chat_preview],
357
- outputs=[user_input, chat_preview, action_log, recommended_products]
358
- )
359
 
360
  if __name__ == "__main__":
361
- demo.launch(share=False)
 
1
  import gradio as gr
2
+ from gradio import ChatMessage
3
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def interact_with_agent(prompt, messages):
7
+ messages.append(ChatMessage(role="user", content=prompt))
8
+ yield messages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
 
 
 
 
11
 
12
+ with gr.Blocks() as demo:
13
+ chatbot = gr.Chatbot(label="Agent",
14
+ msg_format="messages",
15
+ avatar_images=(None, "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png"))
16
+ text_input = gr.Textbox(lines=1, label="Chat Message")
17
+ text_input.submit(interact_with_agent, [text_input, chatbot], [chatbot])
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  if __name__ == "__main__":
21
+ demo.launch()