dschandra commited on
Commit
c8ff656
·
verified ·
1 Parent(s): 2f01078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -118
app.py CHANGED
@@ -1,135 +1,63 @@
1
- import bcrypt
2
  import gradio as gr
3
- from datetime import datetime
4
  from simple_salesforce import Salesforce
5
 
6
  # Salesforce Connection
7
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
8
 
9
- # Function to Hash Password
10
- def hash_password(password):
11
- return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
12
-
13
- # Function to Verify Password
14
- def verify_password(plain_password, hashed_password):
15
- return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
16
-
17
- # Fetch menu items from Salesforce
18
- def fetch_menu_items():
19
  try:
20
  query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
21
- result = sf.query(query)
22
- return result['records']
23
- except Exception as e:
24
- print(f"Error fetching menu items: {e}")
25
- return []
26
-
27
- # Fetch add-ons from Salesforce
28
- def fetch_add_ons():
29
- try:
30
- query = "SELECT Name, Price__c FROM Add_Ons__c"
31
- result = sf.query(query)
32
- return result['records']
33
- except Exception as e:
34
- print(f"Error fetching add-ons: {e}")
35
- return []
36
-
37
- # Save cart details to Salesforce
38
- def save_cart(cart_items, total_cost):
39
- try:
40
- # Create order record
41
- order_record = {
42
- 'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
43
- 'Total_Cost__c': total_cost,
44
- 'Order_Date__c': datetime.now().isoformat()
45
- }
46
- order_result = sf.Order__c.create(order_record)
47
- order_id = order_result['id']
48
-
49
- # Save cart items as order items
50
- for item in cart_items:
51
- extras = ", ".join(extra['name'] for extra in item.get('extras', []))
52
- order_item_record = {
53
- 'Name': item['name'],
54
- 'Order__c': order_id,
55
- 'Quantity__c': item['quantity'],
56
- 'Price__c': item['price'],
57
- 'Extras__c': extras,
58
- 'Instructions__c': item.get('instructions', ''),
59
- 'Total_Cost__c': item['total_cost']
60
- }
61
- sf.Order_Item__c.create(order_item_record)
62
-
63
- return f"Order {order_id} saved successfully!"
64
  except Exception as e:
65
- print(f"Error saving cart: {e}")
66
- return f"Error saving cart: {str(e)}"
67
-
68
- # Function to filter menu based on preference
69
- def filter_menu(preference):
70
- menu_items = fetch_menu_items()
71
- filtered_items = [item for item in menu_items if preference == "All" or item['Veg_NonVeg__c'] == preference]
72
-
73
- menu_output = {}
74
- for item in filtered_items:
75
- section = item.get('Section__c', 'Uncategorized')
76
- if section not in menu_output:
77
- menu_output[section] = []
78
- menu_output[section].append(item)
79
- return menu_output
 
 
 
 
 
80
 
81
  # Gradio App
82
  with gr.Blocks() as app:
83
  with gr.Row():
84
- gr.HTML("<h1>Welcome to Biryani Hub</h1>")
85
-
86
- # Menu and Cart Interface
87
- with gr.Row():
88
- preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], value="All", label="Filter Preference")
89
- menu_display = gr.JSON(label="Menu Items")
90
- cart_display = gr.JSON(label="Cart Items")
91
- total_cost_display = gr.Number(label="Total Cost", value=0)
92
-
93
- with gr.Row():
94
- item_name = gr.Textbox(label="Item Name")
95
- item_price = gr.Number(label="Price", value=0)
96
- item_quantity = gr.Number(label="Quantity", value=1)
97
- add_ons_display = gr.JSON(label="Add-Ons")
98
- special_instructions = gr.Textbox(label="Special Instructions")
99
 
100
  with gr.Row():
101
- add_to_cart_button = gr.Button("Add to Cart")
102
- save_order_button = gr.Button("Save Order")
103
-
104
- # State
105
- cart = gr.State([]) # Cart state to maintain items
106
-
107
- def update_menu(preference):
108
- menu_items = filter_menu(preference)
109
- return menu_items, fetch_add_ons()
110
-
111
- def add_to_cart(cart, name, price, quantity, add_ons, instructions):
112
- if not name or quantity <= 0:
113
- return cart, "Invalid item details!"
114
- total_cost = price * quantity
115
- cart.append({
116
- "name": name,
117
- "price": price,
118
- "quantity": quantity,
119
- "extras": add_ons,
120
- "instructions": instructions,
121
- "total_cost": total_cost
122
- })
123
- return cart, f"Added {name} to cart!"
124
-
125
- def save_order(cart):
126
- total_cost = sum(item['total_cost'] for item in cart)
127
- response = save_cart(cart, total_cost)
128
- return response
129
 
130
- # Interactivity
131
- preference.change(update_menu, inputs=[preference], outputs=[menu_display, add_ons_display])
132
- add_to_cart_button.click(add_to_cart, inputs=[cart, item_name, item_price, item_quantity, add_ons_display, special_instructions], outputs=[cart_display, total_cost_display])
133
- save_order_button.click(save_order, inputs=[cart], outputs=[total_cost_display])
134
 
135
- app.launch()
 
 
1
  import gradio as gr
 
2
  from simple_salesforce import Salesforce
3
 
4
  # Salesforce Connection
5
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
+ # Function to fetch menu items from Salesforce
8
+ def fetch_menu():
 
 
 
 
 
 
 
 
9
  try:
10
  query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
11
+ menu_items = sf.query(query)['records']
12
+
13
+ # Format the menu items into a dictionary grouped by section
14
+ formatted_menu = {}
15
+ for item in menu_items:
16
+ section = item['Section__c'] or "Miscellaneous"
17
+ if section not in formatted_menu:
18
+ formatted_menu[section] = []
19
+
20
+ formatted_menu[section].append({
21
+ "name": item['Name'],
22
+ "price": item['Price__c'],
23
+ "description": item['Description__c'],
24
+ "image1": item['Image1__c'],
25
+ "image2": item['Image2__c'],
26
+ "veg_nonveg": item['Veg_NonVeg__c']
27
+ })
28
+
29
+ return formatted_menu
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  except Exception as e:
31
+ return f"Error fetching menu from Salesforce: {str(e)}"
32
+
33
+ # Function to display menu based on user preference
34
+ def display_menu(preference):
35
+ menu = fetch_menu()
36
+ if isinstance(menu, str): # Error handling
37
+ return menu
38
+
39
+ html_output = ""
40
+ for section, items in menu.items():
41
+ html_output += f"<h2>{section}</h2>"
42
+ for item in items:
43
+ if preference == "All" or item['veg_nonveg'] == preference:
44
+ html_output += f"<div style='border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;'>"
45
+ html_output += f"<h3>{item['name']}</h3>"
46
+ html_output += f"<p>{item['description']}</p>"
47
+ html_output += f"<p>Price: ${item['price']}</p>"
48
+ html_output += f"<img src='{item['image1']}' alt='{item['name']}' style='width: 100px; height: 100px;'>"
49
+ html_output += f"</div>"
50
+ return html_output
51
 
52
  # Gradio App
53
  with gr.Blocks() as app:
54
  with gr.Row():
55
+ gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  with gr.Row():
58
+ preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
59
+ menu_display = gr.HTML()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ preference.change(display_menu, inputs=preference, outputs=menu_display)
 
 
 
62
 
63
+ app.launch()