Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Backend function
|
| 4 |
+
def add_to_cart(name, price, quantity, special_instructions):
|
| 5 |
+
cart = [] # This could be replaced with a database or persistent storage
|
| 6 |
+
item = {
|
| 7 |
+
"name": name,
|
| 8 |
+
"price": price,
|
| 9 |
+
"quantity": quantity,
|
| 10 |
+
"special_instructions": special_instructions
|
| 11 |
+
}
|
| 12 |
+
cart.append(item)
|
| 13 |
+
return f"Item '{name}' added to the cart with quantity {quantity}!"
|
| 14 |
+
|
| 15 |
+
# Custom JavaScript for frontend
|
| 16 |
+
custom_js = """
|
| 17 |
+
<script>
|
| 18 |
+
function openModal(name, price) {
|
| 19 |
+
const modal = document.getElementById("modal");
|
| 20 |
+
modal.style.display = "block";
|
| 21 |
+
document.getElementById("item-name").innerText = name;
|
| 22 |
+
document.getElementById("item-price").innerText = price;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function closeModal() {
|
| 26 |
+
document.getElementById("modal").style.display = "none";
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
function addToCart() {
|
| 30 |
+
const name = document.getElementById("item-name").innerText;
|
| 31 |
+
const price = document.getElementById("item-price").innerText;
|
| 32 |
+
const quantity = document.getElementById("item-quantity").value;
|
| 33 |
+
const instructions = document.getElementById("special-instructions").value;
|
| 34 |
+
|
| 35 |
+
fetch("/api/predict", {
|
| 36 |
+
method: "POST",
|
| 37 |
+
headers: { "Content-Type": "application/json" },
|
| 38 |
+
body: JSON.stringify({
|
| 39 |
+
data: [name, price, quantity, instructions],
|
| 40 |
+
fn_index: 0 // This must match the backend function index
|
| 41 |
+
})
|
| 42 |
+
})
|
| 43 |
+
.then(response => response.json())
|
| 44 |
+
.then(data => {
|
| 45 |
+
alert(data.data[0]);
|
| 46 |
+
closeModal();
|
| 47 |
+
})
|
| 48 |
+
.catch(error => {
|
| 49 |
+
console.error("Error:", error);
|
| 50 |
+
alert("Failed to add item to cart.");
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
</script>
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
# Modal HTML
|
| 57 |
+
modal_html = """
|
| 58 |
+
<div id="modal" style="display:none; position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); background:white; padding:20px; border-radius:10px; box-shadow:0 4px 8px rgba(0,0,0,0.2);">
|
| 59 |
+
<h3>Item Details</h3>
|
| 60 |
+
<p><strong>Name:</strong> <span id="item-name"></span></p>
|
| 61 |
+
<p><strong>Price:</strong> <span id="item-price"></span></p>
|
| 62 |
+
<label for="item-quantity">Quantity:</label>
|
| 63 |
+
<input type="number" id="item-quantity" value="1" min="1" />
|
| 64 |
+
<br /><br />
|
| 65 |
+
<label for="special-instructions">Special Instructions:</label>
|
| 66 |
+
<textarea id="special-instructions" rows="3" style="width:100%;"></textarea>
|
| 67 |
+
<br /><br />
|
| 68 |
+
<button onclick="addToCart()">Add to Cart</button>
|
| 69 |
+
<button onclick="closeModal()">Close</button>
|
| 70 |
+
</div>
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
# Gradio Interface
|
| 74 |
+
with gr.Blocks() as app:
|
| 75 |
+
with gr.Row():
|
| 76 |
+
gr.HTML("<h1>Welcome to the Shop</h1>")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
# Display items
|
| 80 |
+
gr.HTML(
|
| 81 |
+
"""
|
| 82 |
+
<div style="display:flex; gap:20px;">
|
| 83 |
+
<div style="border:1px solid #ddd; padding:10px; border-radius:8px;">
|
| 84 |
+
<h3>Item 1</h3>
|
| 85 |
+
<p>Price: $10</p>
|
| 86 |
+
<button onclick="openModal('Item 1', '$10')">Add to Cart</button>
|
| 87 |
+
</div>
|
| 88 |
+
<div style="border:1px solid #ddd; padding:10px; border-radius:8px;">
|
| 89 |
+
<h3>Item 2</h3>
|
| 90 |
+
<p>Price: $20</p>
|
| 91 |
+
<button onclick="openModal('Item 2', '$20')">Add to Cart</button>
|
| 92 |
+
</div>
|
| 93 |
+
</div>
|
| 94 |
+
"""
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Inject modal and JavaScript
|
| 98 |
+
gr.HTML(modal_html)
|
| 99 |
+
gr.HTML(custom_js)
|
| 100 |
+
|
| 101 |
+
# Map backend function
|
| 102 |
+
app.add_handler(
|
| 103 |
+
fn=add_to_cart,
|
| 104 |
+
inputs=["text", "text", "number", "text"], # Match parameters of `add_to_cart`
|
| 105 |
+
outputs="text"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Launch app
|
| 109 |
+
app.launch()
|