Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,221 @@
|
|
| 1 |
-
# Define the Python script content
|
| 2 |
-
menu_script = """
|
| 3 |
import gradio as gr
|
|
|
|
| 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 |
html_content += f"""
|
| 30 |
-
<div style
|
| 31 |
-
<
|
| 32 |
-
|
| 33 |
-
<
|
| 34 |
-
<p>{item['
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
</div>
|
| 38 |
</div>
|
| 39 |
"""
|
| 40 |
-
html_content += "</div>"
|
| 41 |
return html_content
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
const popup = document.getElementById('popup');
|
| 48 |
-
popup.style.display = 'block';
|
| 49 |
-
popup.style.top = window.event.clientY + 'px';
|
| 50 |
-
popup.style.left = window.event.clientX + 'px';
|
| 51 |
-
document.getElementById('popup-name').innerText = name;
|
| 52 |
-
document.getElementById('popup-price').innerText = price;
|
| 53 |
-
document.getElementById('popup-description').innerText = description;
|
| 54 |
-
document.getElementById('popup-nutrition').innerText = nutrition;
|
| 55 |
-
document.getElementById('popup-image').src = image;
|
| 56 |
-
document.getElementById('popup-portion').innerText = portion;
|
| 57 |
-
}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
|
| 4 |
+
# Function to load the menu data
|
| 5 |
+
def load_menu():
|
| 6 |
+
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
| 7 |
+
try:
|
| 8 |
+
return pd.read_excel(menu_file)
|
| 9 |
+
except Exception as e:
|
| 10 |
+
raise ValueError(f"Error loading menu file: {e}")
|
| 11 |
+
|
| 12 |
+
# Initialize cart globally
|
| 13 |
+
cart_items = []
|
| 14 |
+
|
| 15 |
+
# Pricing for extras
|
| 16 |
+
EXTRAS_PRICES = {
|
| 17 |
+
"Extra Raitha 4oz": 1,
|
| 18 |
+
"Extra Raitha 8oz": 2,
|
| 19 |
+
"Extra Salan 4oz": 1,
|
| 20 |
+
"Extra Salan 8oz": 2,
|
| 21 |
+
"Extra Onion": 1,
|
| 22 |
+
"Extra Onion & Lemon": 2,
|
| 23 |
+
"Extra Fried Onion 4oz": 2,
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Function to filter menu items based on preference
|
| 27 |
+
def filter_menu(preference):
|
| 28 |
+
menu_data = load_menu()
|
| 29 |
+
if preference == "Halal/Non-Veg":
|
| 30 |
+
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 31 |
+
elif preference == "Vegetarian":
|
| 32 |
+
filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 33 |
+
elif preference == "Guilt-Free":
|
| 34 |
+
filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
|
| 35 |
+
else:
|
| 36 |
+
filtered_data = menu_data
|
| 37 |
+
|
| 38 |
+
html_content = ""
|
| 39 |
+
for _, item in filtered_data.iterrows():
|
| 40 |
html_content += f"""
|
| 41 |
+
<div style=\"display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);\">
|
| 42 |
+
<div style=\"flex: 1; margin-right: 15px;\">
|
| 43 |
+
<h3 style=\"margin: 0; font-size: 8px;\">{item['Dish Name']}</h3>
|
| 44 |
+
<p style=\"margin: 5px 0; font-size: 6px; color: #888;\">${item['Price ($)']}</p>
|
| 45 |
+
<p style=\"margin: 5px 0; font-size: 4px; color: #555;\">{item['Description']}</p>
|
| 46 |
+
</div>
|
| 47 |
+
<div style=\"flex-shrink: 0; text-align: center;\">
|
| 48 |
+
<img src=\"{item['Image URL']}\" alt=\"{item['Dish Name']}\" style=\"width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;\">
|
| 49 |
+
<button style=\"background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;\" onclick=\"openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')\">Add</button>
|
| 50 |
</div>
|
| 51 |
</div>
|
| 52 |
"""
|
|
|
|
| 53 |
return html_content
|
| 54 |
|
| 55 |
+
# Function to update the cart display
|
| 56 |
+
def update_cart():
|
| 57 |
+
if len(cart_items) == 0:
|
| 58 |
+
return "Your cart is empty."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
total_bill = 0
|
| 61 |
+
cart_html = "<h3>Your Cart:</h3><ul style='list-style-type: none; padding: 0;'>"
|
| 62 |
+
|
| 63 |
+
for item in cart_items:
|
| 64 |
+
extras = ", ".join(item.get("extras", []))
|
| 65 |
+
extras_cost = sum(EXTRAS_PRICES.get(extra, 0) for extra in item.get("extras", []))
|
| 66 |
+
item_price = float(item['price'].strip('$'))
|
| 67 |
+
item_total = (item_price + extras_cost) * item['quantity']
|
| 68 |
+
total_bill += item_total
|
| 69 |
+
|
| 70 |
+
cart_html += f"<li style='margin-bottom: 20px; border: 1px solid #ddd; padding: 10px; border-radius: 8px;'>"
|
| 71 |
+
cart_html += f"<strong>Item:</strong> {item['name']} - ${item_price:.2f}<br>"
|
| 72 |
+
cart_html += f"<strong>Quantity x Price:</strong> {item['quantity']} x ${item_price:.2f} = ${item_price * item['quantity']:.2f}<br>"
|
| 73 |
+
cart_html += f"<strong>Spice Level:</strong> {item['spiceLevel']}<br>"
|
| 74 |
+
cart_html += f"<strong>Extras:</strong> {extras} - ${extras_cost:.2f}<br>"
|
| 75 |
+
cart_html += f"<strong>Instructions:</strong> {item['instructions']}<br>"
|
| 76 |
+
cart_html += f"<strong>Item Total:</strong> ${item_total:.2f}"
|
| 77 |
+
cart_html += "</li>"
|
| 78 |
+
|
| 79 |
+
cart_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
|
| 80 |
+
return cart_html
|
| 81 |
+
|
| 82 |
+
# Redirect to cart details page
|
| 83 |
+
def redirect_to_cart():
|
| 84 |
+
return update_cart()
|
| 85 |
+
|
| 86 |
+
# Gradio app definition
|
| 87 |
+
def app():
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("## Dynamic Menu with Preferences")
|
| 90 |
+
|
| 91 |
+
# Radio button for selecting preference
|
| 92 |
+
selected_preference = gr.Radio(
|
| 93 |
+
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 94 |
+
value="All",
|
| 95 |
+
label="Choose a Preference",
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Output area for menu items
|
| 99 |
+
menu_output = gr.HTML(value=filter_menu("All"))
|
| 100 |
+
|
| 101 |
+
# Floating cart display
|
| 102 |
+
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
| 103 |
+
|
| 104 |
+
# JavaScript for modal and cart behavior
|
| 105 |
+
modal_and_cart_js = """
|
| 106 |
+
<script>
|
| 107 |
+
let cart = [];
|
| 108 |
+
const extrasPrices = {
|
| 109 |
+
"Extra Raitha 4oz": 1,
|
| 110 |
+
"Extra Raitha 8oz": 2,
|
| 111 |
+
"Extra Salan 4oz": 1,
|
| 112 |
+
"Extra Salan 8oz": 2,
|
| 113 |
+
"Extra Onion": 1,
|
| 114 |
+
"Extra Onion & Lemon": 2,
|
| 115 |
+
"Extra Fried Onion 4oz": 2
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
function openModal(name, image, description, price) {
|
| 119 |
+
document.getElementById('modal').style.display = 'block';
|
| 120 |
+
document.getElementById('modal-image').src = image;
|
| 121 |
+
document.getElementById('modal-name').innerText = name;
|
| 122 |
+
document.getElementById('modal-description').innerText = description;
|
| 123 |
+
document.getElementById('modal-price').innerText = price;
|
| 124 |
+
|
| 125 |
+
// Reset spice levels and extras
|
| 126 |
+
const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
|
| 127 |
+
spiceLevelInputs.forEach(input => input.checked = false);
|
| 128 |
+
|
| 129 |
+
const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
|
| 130 |
+
extrasInputs.forEach(input => input.checked = false);
|
| 131 |
+
|
| 132 |
+
document.getElementById('quantity').value = 1;
|
| 133 |
+
document.getElementById('special-instructions').value = '';
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
function closeModal() {
|
| 137 |
+
document.getElementById('modal').style.display = 'none';
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
function addToCart() {
|
| 141 |
+
const name = document.getElementById('modal-name').innerText;
|
| 142 |
+
const price = document.getElementById('modal-price').innerText;
|
| 143 |
+
const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
|
| 144 |
+
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
| 145 |
+
const instructions = document.getElementById('special-instructions').value;
|
| 146 |
+
const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
|
| 147 |
+
|
| 148 |
+
const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
|
| 149 |
+
const itemTotal = (parseFloat(price.replace('$', '')) + extrasCost) * quantity;
|
| 150 |
+
|
| 151 |
+
const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
|
| 152 |
+
cart.push(cartItem);
|
| 153 |
+
|
| 154 |
+
alert(`${name} added to cart!`);
|
| 155 |
+
updateCartDisplay();
|
| 156 |
+
closeModal();
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
function updateCartDisplay() {
|
| 160 |
+
let totalBill = 0;
|
| 161 |
+
let cartHTML = "<h3>Your Cart:</h3><ul>";
|
| 162 |
+
cart.forEach(item => {
|
| 163 |
+
totalBill += item.itemTotal;
|
| 164 |
+
const extras = item.extras.join(', ');
|
| 165 |
+
cartHTML += `<li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
|
| 166 |
+
});
|
| 167 |
+
cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
|
| 168 |
+
document.getElementById('floating-cart').innerHTML = cartHTML;
|
| 169 |
+
}
|
| 170 |
+
</script>
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
# Modal window
|
| 174 |
+
modal_window = gr.HTML("""
|
| 175 |
+
<div id="modal" style="display: none; position: fixed; top: 40%; left: 50%; transform: translate(-50%, -40%); width: 50%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
| 176 |
+
<div style="text-align: right;">
|
| 177 |
+
<button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">×</button>
|
| 178 |
+
</div>
|
| 179 |
+
<img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
|
| 180 |
+
<h2 id="modal-name"></h2>
|
| 181 |
+
<p id="modal-description"></p>
|
| 182 |
+
<p id="modal-price"></p>
|
| 183 |
+
<!-- Spice Levels -->
|
| 184 |
+
<label for="spice-level">Choose a Spice Level (Required):</label>
|
| 185 |
+
<!-- Biryani Extras -->
|
| 186 |
+
<label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
|
| 187 |
+
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
| 188 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
|
| 189 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
|
| 190 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
|
| 191 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
|
| 192 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
|
| 193 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
|
| 194 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
|
| 195 |
+
</div>
|
| 196 |
+
<!-- Quantity and Special Instructions -->
|
| 197 |
+
<label for="quantity">Quantity:</label>
|
| 198 |
+
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
|
| 199 |
+
<br><br>
|
| 200 |
+
<textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
|
| 201 |
+
<br><br>
|
| 202 |
+
<!-- Add to Cart Button -->
|
| 203 |
+
<button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
|
| 204 |
+
</div>
|
| 205 |
+
""")
|
| 206 |
+
|
| 207 |
+
# Interactivity
|
| 208 |
+
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 209 |
+
|
| 210 |
+
# Layout
|
| 211 |
+
gr.Row([selected_preference])
|
| 212 |
+
gr.Row(menu_output)
|
| 213 |
+
gr.Row(cart_output)
|
| 214 |
+
gr.Row(modal_window)
|
| 215 |
+
gr.HTML(modal_and_cart_js)
|
| 216 |
+
|
| 217 |
+
return demo
|
| 218 |
+
|
| 219 |
+
if __name__ == "__main__":
|
| 220 |
+
demo = app()
|
| 221 |
+
demo.launch()
|