Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -37,6 +37,108 @@ def extract_quantity(command):
|
|
37 |
return number_words[word]
|
38 |
return None
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
@app.route("/")
|
41 |
def index():
|
42 |
return render_template_string(html_code)
|
|
|
37 |
return number_words[word]
|
38 |
return None
|
39 |
|
40 |
+
# HTML Template for Frontend
|
41 |
+
html_code = """
|
42 |
+
<!DOCTYPE html>
|
43 |
+
<html lang="en">
|
44 |
+
<head>
|
45 |
+
<meta charset="UTF-8">
|
46 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
47 |
+
<title>AI Dining Assistant</title>
|
48 |
+
<style>
|
49 |
+
body {
|
50 |
+
font-family: Arial, sans-serif;
|
51 |
+
text-align: center;
|
52 |
+
background-color: #f4f4f9;
|
53 |
+
}
|
54 |
+
h1 {
|
55 |
+
color: #333;
|
56 |
+
}
|
57 |
+
.mic-button {
|
58 |
+
width: 80px;
|
59 |
+
height: 80px;
|
60 |
+
border-radius: 50%;
|
61 |
+
background-color: #007bff;
|
62 |
+
color: white;
|
63 |
+
font-size: 24px;
|
64 |
+
border: none;
|
65 |
+
cursor: pointer;
|
66 |
+
}
|
67 |
+
.status, .response {
|
68 |
+
margin-top: 20px;
|
69 |
+
}
|
70 |
+
</style>
|
71 |
+
</head>
|
72 |
+
<body>
|
73 |
+
<h1>AI Dining Assistant</h1>
|
74 |
+
<button class="mic-button" id="mic-button">🎤</button>
|
75 |
+
<div class="status" id="status">Press the mic button to start...</div>
|
76 |
+
<div class="response" id="response" style="display: none;">Response will appear here...</div>
|
77 |
+
<script>
|
78 |
+
const micButton = document.getElementById('mic-button');
|
79 |
+
const status = document.getElementById('status');
|
80 |
+
const response = document.getElementById('response');
|
81 |
+
let isListening = false;
|
82 |
+
|
83 |
+
micButton.addEventListener('click', () => {
|
84 |
+
if (!isListening) {
|
85 |
+
isListening = true;
|
86 |
+
greetUser();
|
87 |
+
}
|
88 |
+
});
|
89 |
+
|
90 |
+
function greetUser() {
|
91 |
+
const utterance = new SpeechSynthesisUtterance("Hi. Welcome to Biryani Hub. Can I show you the menu?");
|
92 |
+
speechSynthesis.speak(utterance);
|
93 |
+
utterance.onend = () => {
|
94 |
+
status.textContent = "Listening...";
|
95 |
+
startListening();
|
96 |
+
};
|
97 |
+
}
|
98 |
+
|
99 |
+
async function startListening() {
|
100 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
101 |
+
const mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm;codecs=opus" });
|
102 |
+
const audioChunks = [];
|
103 |
+
|
104 |
+
mediaRecorder.ondataavailable = (event) => audioChunks.push(event.data);
|
105 |
+
mediaRecorder.onstop = async () => {
|
106 |
+
const audioBlob = new Blob(audioChunks, { type: "audio/webm" });
|
107 |
+
const formData = new FormData();
|
108 |
+
formData.append("audio", audioBlob);
|
109 |
+
|
110 |
+
status.textContent = "Processing...";
|
111 |
+
try {
|
112 |
+
const result = await fetch("/process-audio", { method: "POST", body: formData });
|
113 |
+
const data = await result.json();
|
114 |
+
response.textContent = data.response;
|
115 |
+
response.style.display = "block";
|
116 |
+
|
117 |
+
const utterance = new SpeechSynthesisUtterance(data.response);
|
118 |
+
speechSynthesis.speak(utterance);
|
119 |
+
utterance.onend = () => {
|
120 |
+
if (!data.response.includes("Goodbye") && !data.response.includes("final order")) {
|
121 |
+
startListening(); // Continue listening
|
122 |
+
} else {
|
123 |
+
status.textContent = "Conversation ended.";
|
124 |
+
isListening = false;
|
125 |
+
}
|
126 |
+
};
|
127 |
+
} catch (error) {
|
128 |
+
response.textContent = "Error processing your request. Please try again.";
|
129 |
+
status.textContent = "Press the mic button to restart.";
|
130 |
+
isListening = false;
|
131 |
+
}
|
132 |
+
};
|
133 |
+
|
134 |
+
mediaRecorder.start();
|
135 |
+
setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
|
136 |
+
}
|
137 |
+
</script>
|
138 |
+
</body>
|
139 |
+
</html>
|
140 |
+
"""
|
141 |
+
|
142 |
@app.route("/")
|
143 |
def index():
|
144 |
return render_template_string(html_code)
|