Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,49 @@
|
|
|
|
|
|
|
|
| 1 |
import speech_recognition as sr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 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.
|
| 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 |
-
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|