File size: 6,652 Bytes
408a4d8
de42bf3
5e1848a
 
de42bf3
cce0f39
 
408a4d8
 
cce0f39
 
b18d651
 
 
408a4d8
 
 
 
 
 
 
 
a1ff101
408a4d8
 
 
 
0347f6c
 
408a4d8
 
0347f6c
408a4d8
 
 
 
283764f
0347f6c
283764f
 
 
 
 
 
 
 
 
 
de42bf3
cce0f39
de42bf3
 
 
 
cce0f39
de42bf3
 
 
 
 
 
 
 
956a08c
 
 
 
a1ff101
 
 
 
 
 
 
 
 
 
956a08c
 
 
 
 
 
de42bf3
 
 
283764f
 
de42bf3
 
a1ff101
283764f
 
 
 
 
408a4d8
 
 
 
 
 
 
 
 
 
 
 
cce0f39
 
 
 
 
5e1848a
cce0f39
 
 
de42bf3
30e1c24
956a08c
 
 
cce0f39
408a4d8
30e1c24
408a4d8
 
 
30e1c24
5e1848a
cce0f39
 
956a08c
408a4d8
956a08c
cce0f39
956a08c
 
cce0f39
408a4d8
b18d651
408a4d8
b18d651
408a4d8
a1ff101
b18d651
 
 
 
 
26eed15
b18d651
 
 
 
de42bf3
 
a1ff101
408a4d8
 
a011d11
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from flask import Flask, render_template_string, request, jsonify
import speech_recognition as sr
from tempfile import NamedTemporaryFile
import os
import ffmpeg
import logging
from werkzeug.exceptions import BadRequest

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

# Global cart to store items
cart = []

html_code = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Dining Assistant</title>
    <style>
        /* Your CSS styles here */
    </style>
</head>
<body>
    <h1>AI Dining Assistant</h1>
    <button class="mic-button" id="mic-button">🎤</button>
    <div class="status" id="status">Press the mic button to start the conversation...</div>
    <div class="response" id="response" style="display: none;">Response will appear here...</div>
    <script>
        const micButton = document.getElementById('mic-button');
        const status = document.getElementById('status');
        const response = document.getElementById('response');
        let mediaRecorder;
        let audioChunks = [];
        let isConversationActive = false;
        micButton.addEventListener('click', () => {
            if (!isConversationActive) {
                isConversationActive = true;
                startConversation();
            }
        });
        function startConversation() {
            status.textContent = 'Listening...';
            startListening();
        }
        function startListening() {
            navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
                mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
                mediaRecorder.start();
                audioChunks = [];
                mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
                mediaRecorder.onstop = async () => {
                    const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
                    const formData = new FormData();
                    formData.append('audio', audioBlob);
                    status.textContent = 'Processing...';
                    try {
                        const result = await fetch('/process-audio', { method: 'POST', body: formData });
                        const data = await result.json();
                        response.textContent = data.response;
                        response.style.display = 'block';
                        const utterance = new SpeechSynthesisUtterance(data.response);
                        speechSynthesis.speak(utterance);
                        utterance.onend = () => {
                            console.log("Speech synthesis completed.");
                            if (data.response.includes("Goodbye")) {
                                status.textContent = 'Conversation ended. Press the mic button to start again.';
                                isConversationActive = false;
                            } else {
                                status.textContent = 'Waiting for next input...';
                                setTimeout(() => {
                                    status.textContent = 'Listening...';
                                    startListening();
                                }, 2000);
                            }
                        };
                        utterance.onerror = (e) => {
                            console.error("Speech synthesis error:", e.error);
                            status.textContent = 'Error with speech output.';
                            isConversationActive = false;
                        };
                    } catch (error) {
                        response.textContent = 'Error occurred. Please try again.';
                        response.style.display = 'block';
                        status.textContent = 'Press the mic button to restart the conversation.';
                        isConversationActive = false;
                    }
                };
                setTimeout(() => mediaRecorder.stop(), 5000);
            }).catch(() => {
                status.textContent = 'Microphone access denied.';
                isConversationActive = false;
            });
        }
    </script>
</body>
</html>
"""

@app.route('/')
def index():
    return render_template_string(html_code)

@app.route('/process-audio', methods=['POST'])
def process_audio():
    try:
        audio_file = request.files.get('audio')
        if not audio_file:
            raise BadRequest("No audio file provided.")

        temp_file = NamedTemporaryFile(delete=False, suffix=".webm")
        audio_file.save(temp_file.name)

        if os.path.getsize(temp_file.name) == 0:
            raise BadRequest("Uploaded audio file is empty.")

        converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
        ffmpeg.input(temp_file.name).output(
            converted_file.name, acodec='pcm_s16le', ac=1, ar='16000'
        ).run(overwrite_output=True)

        recognizer = sr.Recognizer()
        with sr.AudioFile(converted_file.name) as source:
            audio_data = recognizer.record(source)
            command = recognizer.recognize_google(audio_data)
            response = process_command(command)

        return jsonify({"response": response})

    except BadRequest as br:
        return jsonify({"response": f"Bad Request: {str(br)}"}), 400
    except Exception as e:
        return jsonify({"response": f"An error occurred: {str(e)}"}), 500
    finally:
        os.unlink(temp_file.name)
        os.unlink(converted_file.name)

def process_command(command):
    global cart
    command = command.lower()
    menu_items = ["idli", "dosa", "vada", "pongal", "biryani"]
    if "menu" in command:
        return "Here is our menu: Idli, Dosa, Vada, Pongal, Biryani."
    elif any(item in command for item in menu_items):
        item = next((item for item in menu_items if item in command), None)
        if item:
            cart.append(item)
            return f"{item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}."
    elif "order" in command:
        if cart:
            return f"Your order has been placed for: {', '.join(cart)}. Would you like anything else?"
        else:
            return "Your cart is empty. Please add items to your cart first."
    elif "no" in command or "nothing" in command:
        return "Goodbye! Thank you for using AI Dining Assistant."
    return "Sorry, I didn't understand your request."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)