Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def load_menu():
|
| 6 |
-
menu_file = "menu.xlsx"
|
| 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 |
-
#
|
| 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":
|
|
@@ -35,183 +31,82 @@ def filter_menu(preference):
|
|
| 35 |
else:
|
| 36 |
filtered_data = menu_data
|
| 37 |
|
| 38 |
-
|
| 39 |
for _, item in filtered_data.iterrows():
|
| 40 |
-
|
| 41 |
-
<div
|
| 42 |
-
<
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
<p style=\"margin: 5px 0; font-size: 14px; 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
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 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: 500;">
|
| 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: 70%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
|
| 180 |
-
<h2 id="modal-name"></h2>
|
| 181 |
-
<p id="modal-description"></p>
|
| 182 |
-
<label for="extras"> Choose as many as you like:</label>
|
| 183 |
-
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
| 184 |
-
<label><input type="checkbox" name="extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
|
| 185 |
-
<label><input type="checkbox" name="extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
|
| 186 |
-
<label><input type="checkbox" name="extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
|
| 187 |
-
<label><input type="checkbox" name="extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
|
| 188 |
-
<label><input type="checkbox" name="extra" value="Extra Onion" /> Extra Onion + $1.00</label>
|
| 189 |
-
<label><input type="checkbox" name="extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
|
| 190 |
-
<label><input type="checkbox" name="extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
|
| 191 |
-
</div>
|
| 192 |
-
<!-- Quantity and Special Instructions -->
|
| 193 |
-
<label for="quantity">Quantity:</label>
|
| 194 |
-
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
|
| 195 |
-
<br><br>
|
| 196 |
-
<textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
|
| 197 |
-
<br><br>
|
| 198 |
-
<!-- Add to Cart Button -->
|
| 199 |
-
<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>
|
| 200 |
</div>
|
| 201 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 205 |
-
|
| 206 |
-
# Layout
|
| 207 |
-
gr.Row([selected_preference])
|
| 208 |
-
gr.Row(menu_output)
|
| 209 |
-
gr.Row(cart_output)
|
| 210 |
-
gr.Row(modal_window)
|
| 211 |
-
gr.HTML(modal_and_cart_js)
|
| 212 |
-
|
| 213 |
-
return demo
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
-
demo
|
| 217 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import edge_tts
|
| 4 |
+
import asyncio
|
| 5 |
+
import tempfile
|
| 6 |
+
import numpy as np
|
| 7 |
+
import soxr
|
| 8 |
+
from pydub import AudioSegment
|
| 9 |
+
import torch
|
| 10 |
+
import sentencepiece as spm
|
| 11 |
+
import onnxruntime as ort
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
+
|
| 14 |
+
# Load Menu Data
|
| 15 |
def load_menu():
|
| 16 |
+
menu_file = "menu.xlsx"
|
| 17 |
try:
|
| 18 |
return pd.read_excel(menu_file)
|
| 19 |
except Exception as e:
|
| 20 |
raise ValueError(f"Error loading menu file: {e}")
|
| 21 |
|
| 22 |
+
# Filter Menu Items
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def filter_menu(preference):
|
| 24 |
menu_data = load_menu()
|
| 25 |
if preference == "Halal/Non-Veg":
|
|
|
|
| 31 |
else:
|
| 32 |
filtered_data = menu_data
|
| 33 |
|
| 34 |
+
menu_html = """" # Prepare dynamic HTML for the menu
|
| 35 |
for _, item in filtered_data.iterrows():
|
| 36 |
+
menu_html += f"""
|
| 37 |
+
<div>
|
| 38 |
+
<h3>{item['Dish Name']}</h3>
|
| 39 |
+
<p>Price: ${item['Price ($)']}</p>
|
| 40 |
+
<p>Description: {item['Description']}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
</div>
|
| 42 |
"""
|
| 43 |
+
return menu_html
|
| 44 |
+
|
| 45 |
+
# Speech Recognition Model Configuration
|
| 46 |
+
model_name = "neongeckocom/stt_en_citrinet_512_gamma_0_25"
|
| 47 |
+
sample_rate = 16000
|
| 48 |
+
|
| 49 |
+
preprocessor = torch.jit.load(hf_hub_download(model_name, "preprocessor.ts", subfolder="onnx"))
|
| 50 |
+
encoder = ort.InferenceSession(hf_hub_download(model_name, "model.onnx", subfolder="onnx"))
|
| 51 |
+
tokenizer = spm.SentencePieceProcessor(hf_hub_download(model_name, "tokenizer.spm", subfolder="onnx"))
|
| 52 |
+
|
| 53 |
+
async def respond(audio_path, preference):
|
| 54 |
+
# Transcribe audio to text
|
| 55 |
+
transcription = transcribe(audio_path)
|
| 56 |
+
|
| 57 |
+
# Voice-based interaction logic
|
| 58 |
+
if "vegetarian" in transcription.lower():
|
| 59 |
+
preference = "Vegetarian"
|
| 60 |
+
elif "non-veg" in transcription.lower() or "halal" in transcription.lower():
|
| 61 |
+
preference = "Halal/Non-Veg"
|
| 62 |
+
elif "guilt-free" in transcription.lower():
|
| 63 |
+
preference = "Guilt-Free"
|
| 64 |
+
elif "menu details" in transcription.lower():
|
| 65 |
+
preference = "All"
|
| 66 |
+
|
| 67 |
+
# Filter menu based on preference
|
| 68 |
+
menu_html = filter_menu(preference)
|
| 69 |
+
|
| 70 |
+
# Text-to-Speech Response
|
| 71 |
+
reply = f"Here are some {preference} dishes available." if preference != "All" else "Here are all the menu details available."
|
| 72 |
+
communicate = edge_tts.Communicate(reply)
|
| 73 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 74 |
+
tmp_path = tmp_file.name
|
| 75 |
+
await communicate.save(tmp_path)
|
| 76 |
+
|
| 77 |
+
return tmp_path, menu_html
|
| 78 |
+
|
| 79 |
+
def transcribe(audio_path):
|
| 80 |
+
audio_file = AudioSegment.from_file(audio_path)
|
| 81 |
+
sr = audio_file.frame_rate
|
| 82 |
+
audio_buffer = np.array(audio_file.get_array_of_samples())
|
| 83 |
+
audio_fp32 = np.divide(audio_buffer, np.iinfo(audio_buffer.dtype).max, dtype=np.float32)
|
| 84 |
+
audio_16k = soxr.resample(audio_fp32, sr, sample_rate)
|
| 85 |
+
|
| 86 |
+
input_signal = torch.tensor(audio_16k).unsqueeze(0)
|
| 87 |
+
length = torch.tensor(len(audio_16k)).unsqueeze(0)
|
| 88 |
+
processed_signal, _ = preprocessor.forward(input_signal=input_signal, length=length)
|
| 89 |
+
logits = encoder.run(None, {'audio_signal': processed_signal.numpy(), 'length': length.numpy()})[0][0]
|
| 90 |
+
|
| 91 |
+
blank_id = tokenizer.vocab_size()
|
| 92 |
+
decoded_prediction = [p for p in logits.argmax(axis=1).tolist() if p != blank_id]
|
| 93 |
+
text = tokenizer.decode_ids(decoded_prediction)
|
| 94 |
+
|
| 95 |
+
return text
|
| 96 |
+
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
with gr.Row():
|
| 99 |
+
gr.Markdown("""
|
| 100 |
+
<div style="text-align: right; margin-bottom: 10px;">
|
| 101 |
+
<img src="/mnt/data/Screenshot%202024-12-28%20102122.png" alt="Microphone Icon" style="width: 30px; height: 30px; cursor: pointer;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
</div>
|
| 103 |
""")
|
| 104 |
+
audio_input = gr.Audio(label="Speak your preference", source="microphone", type="filepath")
|
| 105 |
+
preference = gr.Textbox(label="Current Preference", value="All")
|
| 106 |
+
audio_output = gr.Audio(label="Assistant Response", autoplay=True)
|
| 107 |
+
menu_output = gr.HTML(label="Menu Suggestions")
|
| 108 |
|
| 109 |
+
audio_input.change(respond, inputs=[audio_input, preference], outputs=[audio_output, menu_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
+
demo.queue().launch()
|
|
|