dschandra commited on
Commit
1b89955
·
verified ·
1 Parent(s): f5bdb44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -71
app.py CHANGED
@@ -1,75 +1,49 @@
 
 
1
  import speech_recognition as sr
2
- import pyttsx3
3
- from flask import Flask, jsonify, request
4
- import pandas as pd
5
 
6
- # Initialize Flask app
7
- app = Flask(__name__)
8
 
9
- # Initialize TTS engine
10
- engine = pyttsx3.init()
11
- engine.setProperty('rate', 150) # Speed of speech
12
-
13
- # Sample restaurant menu
14
- menu = pd.DataFrame({
15
- 'ID': [1, 2, 3, 4],
16
- 'Name': ['Pizza', 'Burger', 'Pasta', 'Salad'],
17
- 'Price': [8.99, 5.49, 7.29, 3.99]
18
- })
19
-
20
- # Function to speak text
21
- def speak(text):
22
- engine.say(text)
23
- engine.runAndWait()
24
-
25
- # Function to listen for a command
26
- def listen():
27
  recognizer = sr.Recognizer()
28
- with sr.Microphone() as source:
29
- print("Listening for your order...")
30
- audio = recognizer.listen(source)
31
- try:
32
- command = recognizer.recognize_google(audio)
33
- print("You said: ", command)
34
- return command.lower()
35
- except sr.UnknownValueError:
36
- speak("Sorry, I did not understand. Can you please repeat?")
37
- return listen()
38
- except sr.RequestError:
39
- speak("Could not request results. Please check your internet connection.")
40
- return None
41
-
42
- # Function to process the order
43
- def process_order(command):
44
- if 'order' in command:
45
- food_item = None
46
- for item in menu['Name']:
47
- if item.lower() in command:
48
- food_item = item
49
- break
50
- if food_item:
51
- price = menu[menu['Name'] == food_item]['Price'].values[0]
52
- speak(f"You've ordered {food_item}. The price is ${price}.")
53
- return jsonify({'order': food_item, 'price': price})
54
- else:
55
- speak("Sorry, I couldn't find that item on the menu. Please try again.")
56
- return None
57
- else:
58
- speak("Please say 'order' followed by the food item.")
59
- return None
60
-
61
- @app.route('/start_order', methods=['GET'])
62
- def start_order():
63
- speak("Welcome to the restaurant. What would you like to order?")
64
- command = listen()
65
- if command:
66
- response = process_order(command)
67
- if response:
68
- return response
69
- else:
70
- return jsonify({'message': 'No valid order detected. Please try again.'}), 400
71
- else:
72
- return jsonify({'message': 'Error in voice recognition.'}), 500
73
-
74
- if __name__ == '__main__':
75
- app.run(debug=True)
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
  import speech_recognition as sr
4
+ from gtts import gTTS
5
+ import os
 
6
 
7
+ # Set up Hugging Face conversational model
8
+ conversational_pipeline = pipeline("conversational", model="microsoft/DialoGPT-medium")
9
 
10
+ def process_audio(audio_file):
11
+ # Convert the audio file to text using SpeechRecognition
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  recognizer = sr.Recognizer()
13
+ with sr.AudioFile(audio_file.name) as source:
14
+ audio = recognizer.record(source)
15
+ try:
16
+ print("Recognizing...")
17
+ text = recognizer.recognize_google(audio)
18
+ print(f"You said: {text}")
19
+ except sr.UnknownValueError:
20
+ text = "Sorry, I couldn't understand that."
21
+ except sr.RequestError:
22
+ text = "Could not request results."
23
+
24
+ # Get the bot's response using Hugging Face's model
25
+ response = conversational_pipeline(text)
26
+ bot_response = response[0]['generated_text']
27
+ print(f"Bot: {bot_response}")
28
+
29
+ # Convert the bot's response to speech using gTTS
30
+ tts = gTTS(bot_response)
31
+ tts.save("response.mp3")
32
+
33
+ # Play the audio
34
+ os.system("mpg321 response.mp3")
35
+
36
+ return bot_response, "response.mp3" # Return the bot's text response and the audio file
37
+
38
+ # Create Gradio interface
39
+ iface = gr.Interface(
40
+ fn=process_audio,
41
+ inputs=gr.inputs.Audio(source="microphone", type="file"),
42
+ outputs=[gr.outputs.Textbox(), gr.outputs.Audio(type="file")],
43
+ live=True,
44
+ title="Voice Bot",
45
+ description="Speak to the bot, and it will respond to you!"
46
+ )
47
+
48
+ # Launch the interface
49
+ iface.launch()