-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.py
60 lines (47 loc) · 1.67 KB
/
server.py
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
import os
import whisper
from flask import Flask, render_template, request
from chains import get_chat_chain, get_search_agent, get_qa_chain
import pyttsx3
# get path for static files
static_dir = os.path.join(os.path.dirname(__file__), 'static')
if not os.path.exists(static_dir):
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
# audio file
audio_file = 'recording.webm'
# load whisper model
model = whisper.load_model('small.en')
chat_chain = get_chat_chain()
search_agent = get_search_agent()
qa_chain = get_qa_chain()
# start server
server = Flask(__name__, static_folder=static_dir, template_folder=static_dir)
@server.route('/')
def landing():
return render_template('index.html')
@server.route('/record', methods=['POST'])
def record():
# get file from request and save it
file = request.files['audio']
file.save(audio_file)
# transcribe the audio file using Whisper and extract the text
audio = whisper.load_audio(audio_file)
result = model.transcribe(audio)
text = result["text"]
# remove the temp audio file
if os.path.exists(audio_file):
os.remove(audio_file)
# predict the response to get the output
# output = chat_chain.predict(human_input=text)
# output = search_agent.run(input=text)
output = qa_chain.run(text)
# say out the response
engine = pyttsx3.init()
engine.setProperty('rate', 190)
engine.setProperty('voice', os.environ['VOICE'])
engine.say(output)
engine.startLoop()
# remove the temp audio file
if os.path.exists(audio_file):
os.remove(audio_file)
return {"input": text, "output": output.replace("\n", "<br />")}