Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
Created By Lewis Kamau Kimaru
|
3 |
+
https://towardsdev.com/building-a-voice-assistant-using-openai-api-and-flask-by-chatgpt-9f90a430b242
|
4 |
+
https://github.com/prathmeshChaudhari05/Voice-Assistant-Flask
|
5 |
+
August 2023
|
6 |
+
'''
|
7 |
+
|
8 |
+
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
|
9 |
+
from playsound import playsound
|
10 |
+
import speech_recognition as sr
|
11 |
+
from pyngrok import ngrok
|
12 |
+
from gtts import gTTS
|
13 |
+
import openai
|
14 |
+
import os
|
15 |
+
|
16 |
+
app = Flask(__name__)
|
17 |
+
|
18 |
+
# Set your ngrok authtoken
|
19 |
+
#ngrok.set_auth_token("2UAhCqf5zP0cCgJzeadNANkbIqx_7ZJvhkDSNWccqMX2hyxXP")
|
20 |
+
ngrok.set_auth_token("2S6xeFEoSVFWr2egtDRcqgeUtSx_2juefHFkEW6nGbpRHS37W")
|
21 |
+
#ngrok.set_auth_token("2UAmdjHdAFV9x84TdyEknIfNhYk_4Ye8n4YK7ZhfCMob3yPBh")
|
22 |
+
#ngrok.set_auth_token("2UAqm26HuWiWvQjzK58xYufSGpy_6tStKSyLLyR9f7pcezh6R")
|
23 |
+
#ngrok.set_auth_token("2UGQqzZoI3bx7SSk8H4wuFC3iaC_2WniWyNAsW5fd2rFyKVq1")
|
24 |
+
#ngrok.set_auth_token("2UISOtStHwytO70NQK38dFhS1at_5opQaXnoQCKeyhEe4qfT2")
|
25 |
+
|
26 |
+
# Set up OpenAI API credentials
|
27 |
+
openai.api_key = 'YOUR_API_KEY'
|
28 |
+
|
29 |
+
html_content = """
|
30 |
+
<!DOCTYPE html>
|
31 |
+
<html>
|
32 |
+
<head>
|
33 |
+
<title>Voice Assistant</title>
|
34 |
+
</head>
|
35 |
+
<body>
|
36 |
+
<h1>Voice Assistant</h1>
|
37 |
+
<form method="POST">
|
38 |
+
<button type="submit">Ask me something!</button>
|
39 |
+
</form>
|
40 |
+
<audio controls>
|
41 |
+
<source src="{{ url_for('static', filename='response.mp3') }}" type="audio/mpeg">
|
42 |
+
</audio>
|
43 |
+
</body>
|
44 |
+
</html>
|
45 |
+
"""
|
46 |
+
|
47 |
+
@app.route('/')
|
48 |
+
def home():
|
49 |
+
|
50 |
+
return html_content
|
51 |
+
|
52 |
+
|
53 |
+
def handle_form():
|
54 |
+
r = sr.Recognizer()
|
55 |
+
with sr.Microphone() as source:
|
56 |
+
print("Listening...")
|
57 |
+
audio = r.listen(source)
|
58 |
+
|
59 |
+
try:
|
60 |
+
result = r.recognize_google(audio)
|
61 |
+
print("result2:")
|
62 |
+
print(r.recognize_google(audio, show_all=True))
|
63 |
+
response = openai.Completion.create(
|
64 |
+
engine="davinci",
|
65 |
+
prompt=result,
|
66 |
+
max_tokens=60,
|
67 |
+
n=1,
|
68 |
+
stop=None,
|
69 |
+
temperature=0.5,
|
70 |
+
)
|
71 |
+
if response.choices:
|
72 |
+
tts = gTTS(text=response.choices[0].text, lang='en')
|
73 |
+
else:
|
74 |
+
tts = gTTS(text="I'm sorry, I didn't understand what you said", lang='en')
|
75 |
+
filename = 'response.mp3'
|
76 |
+
tts.save(filename)
|
77 |
+
playsound(filename)
|
78 |
+
os.remove(filename)
|
79 |
+
except sr.UnknownValueError:
|
80 |
+
print("Google Speech Recognition could not understand audio")
|
81 |
+
except sr.RequestError as e:
|
82 |
+
print("Could not request results from Google Speech Recognition service; {0}".format(e))
|
83 |
+
return html_content
|
84 |
+
|
85 |
+
@app.route('/', methods=['POST'])
|
86 |
+
def submit_textarea():
|
87 |
+
return handle_form()
|
88 |
+
|
89 |
+
|
90 |
+
ngrok_tunnel = ngrok.connect(7860)
|
91 |
+
public_url = ngrok_tunnel.public_url
|
92 |
+
print('\nPublic URL✅:', public_url)
|
93 |
+
|
94 |
+
print("\nFlask APP starting .......\n")
|