File size: 2,675 Bytes
e84b5e6 3d22ea4 e84b5e6 3d22ea4 e84b5e6 3d22ea4 e84b5e6 3d22ea4 e84b5e6 3d22ea4 e84b5e6 232f345 3d22ea4 d7da748 3d22ea4 d7da748 3d22ea4 d7da748 3d22ea4 d7da748 3d22ea4 e84b5e6 3d22ea4 d7da748 3d22ea4 d7da748 3d22ea4 d7da748 3d22ea4 e84b5e6 3d22ea4 e84b5e6 3d22ea4 |
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 |
'''
Created By Lewis Kamau Kimaru
https://towardsdev.com/building-a-voice-assistant-using-openai-api-and-flask-by-chatgpt-9f90a430b242
https://github.com/prathmeshChaudhari05/Voice-Assistant-Flask
August 2023
'''
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from playsound import playsound
import speech_recognition as sr
from pyngrok import ngrok
from gtts import gTTS
import openai
import os
app = Flask(__name__)
# Set your ngrok authtoken
ngrok.set_auth_token("2UAhCqf5zP0cCgJzeadNANkbIqx_7ZJvhkDSNWccqMX2hyxXP")
#ngrok.set_auth_token("2S6xeFEoSVFWr2egtDRcqgeUtSx_2juefHFkEW6nGbpRHS37W")
#ngrok.set_auth_token("2UAmdjHdAFV9x84TdyEknIfNhYk_4Ye8n4YK7ZhfCMob3yPBh")
#ngrok.set_auth_token("2UAqm26HuWiWvQjzK58xYufSGpy_6tStKSyLLyR9f7pcezh6R")
#ngrok.set_auth_token("2UGQqzZoI3bx7SSk8H4wuFC3iaC_2WniWyNAsW5fd2rFyKVq1")
#ngrok.set_auth_token("2UISOtStHwytO70NQK38dFhS1at_5opQaXnoQCKeyhEe4qfT2")
# Set up OpenAI API credentials
openai.api_key = 'YOUR_API_KEY'
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Voice Assistant</title>
</head>
<body>
<h1>Voice Assistant</h1>
<form method="POST">
<button type="submit">Ask me something!</button>
</form>
<audio controls>
<source src="{{ url_for('static', filename='response.mp3') }}" type="audio/mpeg">
</audio>
</body>
</html>
"""
@app.route('/')
def home():
return html_content
def handle_form():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
result = r.recognize_google(audio)
print("result2:")
print(r.recognize_google(audio, show_all=True))
response = openai.Completion.create(
engine="davinci",
prompt=result,
max_tokens=60,
n=1,
stop=None,
temperature=0.5,
)
if response.choices:
tts = gTTS(text=response.choices[0].text, lang='en')
else:
tts = gTTS(text="I'm sorry, I didn't understand what you said", lang='en')
filename = 'response.mp3'
tts.save(filename)
playsound(filename)
os.remove(filename)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return html_content
@app.route('/', methods=['POST'])
def submit_textarea():
return handle_form()
ngrok_tunnel = ngrok.connect(7860)
public_url = ngrok_tunnel.public_url
print('\nPublic URL✅:', public_url)
print("\nFlask APP starting .......\n")
|