-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
150 lines (117 loc) · 4.84 KB
/
app.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import glob
import json
import os
import time
import soundfile as sf
from flask import (Flask, redirect, render_template, request,
send_from_directory, session)
from gtts import gTTS, lang
from chat_bot import prompts
from chat_bot.chatbot import ChatBot
from keys.keys import *
from speech_recognition.audio_transcriber import AudioTranscriber
DEVELOPMENT_ENV = False
app = Flask(__name__)
app.secret_key = 'BAD_SECRET_KEY'
app_data = {
"name": "Triolingo",
"description": "A chatbot app to allow you to practice deep conversations when learning a new language.",
"author": "Team Name",
"html_title": "Triolingo",
"project_name": "Triolingo",
"slogan": "Bringing the power of conversation to your hands"
}
chatbot = ChatBot(OPENAI_API_KEY, session)
transcriber = AudioTranscriber(DEEPGRAM_API_KEY)
cwd = os.path.dirname(os.path.realpath(__file__)) + '/'
@app.route('/')
def index():
return render_template('index.html', app_data=app_data)
@app.route('/chat-topics')
def chat_topics():
return render_template('chat-topics.html', app_data=app_data)
@app.route('/chat/<string:topic>')
def chat(topic):
# Initialise language if not set
if not "language-code" in session:
session["language-code"] = "en-GB"
if not "language-name" in session:
session["language-name"] = "English"
# Get random subtopic and associated prompt
(session["prompt"], session["subtopic"]) = prompts.get_prompt(topic)
return render_template('chat.html', app_data=app_data, language=session["language-name"], topic=session["subtopic"])
@app.route('/help')
def help():
return render_template('help.html', app_data=app_data)
@app.route('/about')
def about():
return render_template('about.html', app_data=app_data)
@app.route('/scripts/<path:path>')
def send_js(path):
return send_from_directory('scripts', path)
@app.route('/images/<path:path>')
def send_img(path):
return send_from_directory('images', path)
@app.route('/temp/<path:path>')
def send_temp(path):
return send_from_directory('temp', path)
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
# Save audio file from POST data
f = request.files['audio_data']
f.save(cwd+'temp/upload.wav')
# Re-encode audio file ready for transcription
data, samplerate = sf.read(cwd+'temp/upload.wav')
sf.write(cwd+'temp/upload2.wav', data, samplerate, subtype='PCM_16')
# Transcribe audio file
with open(cwd+'temp/upload2.wav', 'rb') as file:
text = asyncio.run(transcriber.transcribe_audio(file, session.get("language-code", "en-GB")))
# Get response from ChatBot and generate speech from it
answer = chat(text)
tts_file = tts(answer, session["language-code"])
response_dict = {'user': text, 'ai': answer, 'tts_file': tts_file}
return json.dumps(response_dict)
return "fail"
@app.route('/initial_prompt', methods=['GET', 'POST'])
def initial_prompt():
if request.method == 'POST':
# Start with a basic question as the initial prompt to the user, append this to the GPT-3 hidden prompt
start_prompt = f"What is your opinion of {session['subtopic']}?"
session["prompt"] += f"\nFriend_q: " + start_prompt
# Translate into target language and generate speech
if session["language-name"] != "English":
start_prompt = chatbot.translate(start_prompt, "English", session["language-name"])
tts_file = tts(start_prompt, session["language-code"])
response_dict = {'ai': start_prompt, 'tts_file': tts_file}
return json.dumps(response_dict)
return "fail"
@app.route('/set_language', methods=['POST'])
def set_language():
session['language-code'] = request.form.get('lang-selected')
session["language-name"] = lang.tts_langs()[session["language-code"][:2]]
print(request.referrer)
return redirect(request.referrer)
# Utility functions
def delete_temp_contents():
files = glob.glob(cwd+'temp/*')
for f in files:
os.remove(f)
def tts(text, language):
tts = gTTS(text, lang=language)
delete_temp_contents()
curr_time = str(round(time.time()))
filename = f"tts{curr_time}.wav"
tts.save(f"{cwd}temp/{filename}")
return filename
def chat(text):
if session["language-name"] != "English":
text = chatbot.translate(text, session["language-name"], "English")
answer = chatbot.chat(text)
return chatbot.translate(answer, "English", session["language-name"])
return chatbot.chat(text)
if __name__ == '__main__':
app.run(debug=DEVELOPMENT_ENV)