import speech_recognition as sr
import os
from datetime import datetime, timedelta
import pvorca
import pygame
import time
from flask import Flask, request, jsonify
app = Flask(__name__)
orca = pvorca.create(access_key='89BlxJKCyiH/Eye4zhS74DxMibVpYlj/6qkLLw90NCm+ICw+AKYZqg==')
def play_audio(filename):
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
pygame.mixer.quit()
os.remove(filename)
def speak(response):
orca.synthesize_to_file(response,"output.wav")
play_audio("output.wav")
def get_speech_input(try_count=0, max_tries=3):
if try_count >= max_tries:
speak(
"It seems we're having trouble with the connection.I will call you latter. Goodbye!"
)
return None
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source, timeout=10)
try:
user_input = r.recognize_google(audio)
return user_input.lower()
except sr.UnknownValueError as e:
print(e)
speak(f"I couldn't hear you. Are you there ?")
return get_speech_input(try_count + 1, max_tries)
@app.route('/', methods=['POST'])
def greet_user():
speak("what is your name?")
user_name = get_speech_input()
curent_hour = datetime.now().hour
if 5 < curent_hour < 12:
speak(f"Hello {user_name} Good Morning ")
elif 12 <= curent_hour < 18:
speak(f"Hello {user_name} Good Afternoon")
else:
speak(f"Hello {user_name} Good Evening ")
speak(
f"my name is thanos, and I'm calling from Health Insurance system. How are you today?"
)
return "done"
if __name__ == '__main__':
app.run(debug=True)
i have this api but when i host this api then its give me portaudio error how can i host this api
This question has nothing at all to do with Streamlit…
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.