forked from Malak-Oualid/NoMorals-CodeJam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
291 lines (238 loc) · 9.47 KB
/
app.py
File metadata and controls
291 lines (238 loc) · 9.47 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# @app.route('/scoring')
# def score():
# generated_text = "Grapse" #request.form.get('generated_text').translate(str.maketrans('', '', string.punctuation)).lower()
# transcript = "Grapes" #request.form.get('transcript').translate(str.maketrans('', '', string.punctuation)).lower()
# gamemode = "medium" #request.form.get('gamemode')
# score = fuzz.partial_ratio(transcript, generated_text)
# =======
from flask import Flask, render_template, request, redirect, url_for, session
import speech_recognition as sr
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
import subprocess
import os
from fuzzywuzzy import fuzz
import string
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from io import BytesIO
import base64
OPENAI_API_KEY = "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"
# Prompts
BEGIN_PROMPT = "Write a speech practice text, consisting of two\
sentences, with themes related to jams, fruits, or breakfasts. "
EASY_TEMPLATE = "Create 2 sentences with straightforward words and\
clear pronunciation. "
MEDIUM_TEMPLATE = "Introduce sentences with slightly more complex words or\
phrases that require careful articulation. "
HARD_TEMPLATE = "Include sentences with challenging sounds or tongue-twisters\
to test fluency. "
END_PROMPT = "Your writing should be able to be read aloud in 20 seconds or less,\
and maintain a casual and creative style. Your writing: "
# Levels
EASY = 'easy'
MEDIUM = 'medium'
HARD = 'hard'
# Final generated text
final_text = ""
all_generated_texts = []
app = Flask(__name__)
app.secret_key = "JellyJabber"
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
@app.route('/', methods=['GET', 'POST'])
@app.route('/level', methods=['GET', 'POST'])
def level():
input_level = ""
# Received data
if request.method == "POST":
# Render buttons
if request.form.get(EASY) == 'Easy':
input_level = EASY
elif request.form.get(MEDIUM) == "Medium":
input_level = MEDIUM
elif request.form.get(HARD) == "Hard":
input_level = HARD
# Store value in variable
print(input_level)
session['input_level'] = input_level
print("Session level:", session['input_level'])
return redirect(url_for('read'))
else:
return render_template('level.html')
@app.route('/recording', methods=['GET', 'POST'])
def read():
input_level = session['input_level']
print(input_level)
# Query model
generated_text = generate_speech_text(input_level)
# generated_text = "Breakfast is the most important meal of the day so why not start off with a delicious homemade spread"
generated_text = generated_text.strip('\"\"')
print("Generated Text: ", generated_text)
all_generated_texts.append(generated_text)
# Process the audio file as needed
# For example, save it to disk
input_path = 'uploaded.wav'
output_path = 'converted_file.wav'
audio_file_exists = False
transcript = ""
# Collect audio from browser and save generated_text as final_text
if 'audio_data' in request.files:
audio_data = request.files['audio_data']
audio_data.save(input_path)
audio_file_exists = True
print("Saved audio file")
# Convert the audio to PCM WAV format using ffmpeg
# convert_to_pcm_wav(input_path, output_path)
else:
print("No 'audio_data' file found in the request")
# Received data
if audio_file_exists:
print("Audio exists! Now transcribing...")
# sound = wave.open(output_path, 'rb')
success = convert_to_wav(input_path, output_path)
if success:
recognizer = sr.Recognizer()
audioFile = sr.AudioFile(output_path)
with audioFile as source:
data = recognizer.record(source)
transcript = recognizer.recognize_google(data, key=None)
# Print out original passage and then transcript
print("Original passage: ", all_generated_texts[0])
session['generated_text'] = all_generated_texts[0]
print("Transcript: ", transcript)
session['transcript'] = transcript
print("Level: ", session['input_level'])
# Delete file
os.remove(output_path)
print("Deleted OS")
return redirect(url_for('score'))
else:
print("Cannot transcribe, wrong format")
else:
print("Cannot transcribe, audio file doesn't exist")
# Redirect the user to a new page or display a message
return render_template('recording.html', transcript=transcript,
generated_text=generated_text)
@app.route('/scoring', methods=['GET','POST'])
def score():
generated_text = session['generated_text'].translate(str.maketrans('', '', string.punctuation)).lower()
transcript = session['transcript'].translate(str.maketrans('', '', string.punctuation)).lower()
gamemode = session['input_level'].translate(str.maketrans('', '', string.punctuation)).lower()
score = fuzz.ratio(transcript, generated_text)
print(score)
message = ""
if 0 <= score and score < 50:
message = "Better luck next time, Jelly Warrior!"
elif score <= 80:
message = "Not too shabby, Jelly Warrior!"
else:
message = "You're out of this world, Jelly Warrior!"
# return render_template('results.html', gamemode = gamemode, score = score, message = message)
# @app.route('/recording', methods = ['GET'])
# def recording():
# return render_template('recording.html')
# =======
print(message)
# Generate diff between two strings
strings_diff = compare_strings(transcript, generated_text)
generic_x_list, generic_y_list = create_horizontal_function(strings_diff)
plt.plot(generic_x_list, generic_y_list, marker='o', linestyle='',
color='purple')
discontinuity_list = find_discontinuity_points(strings_diff)
for point in discontinuity_list:
plt.scatter(*point, color='red', marker='x', s=100)
plt.xlabel('Words you said')
plt.ylabel('Correct or incorrect')
plt.title('How well did you jam in this level?')
# Save the plot to a BytesIO object
img = BytesIO()
plt.savefig(img, format='png')
img.seek(0)
# Encode the plot image as base64
plot_url = base64.b64encode(img.getvalue()).decode()
plt.close()
if request.method == "POST":
if "restart" in request.form:
# User wants to restart
return redirect(url_for('level'))
# return render_template('results.html', gamemode = gamemode, score = score,
# message = message, strings_diff=strings_diff)
return render_template('results.html', gamemode = gamemode, score = score,
message = message, strings_diff = strings_diff,
plot_url=plot_url)
def generate_speech_text(input_level):
prompt_level = ""
# Generate text
print("I am going to generate text with ", input_level)
chat_model = OpenAI(openai_api_key=OPENAI_API_KEY)
print(chat_model)
if input_level == HARD:
prompt_level = HARD_TEMPLATE
elif input_level == MEDIUM:
prompt_level = MEDIUM_TEMPLATE
else:
prompt_level = EASY
prompt = str(PromptTemplate.from_template(BEGIN_PROMPT + prompt_level +
END_PROMPT))
print(prompt)
result = chat_model.predict(prompt)
if result != "":
return str(result)
else:
return "No text found"
def convert_to_wav(input_path, output_path):
print("Beginning function convert_to_wav")
try:
print("Creating command")
command = [
'ffmpeg',
'-i', input_path,
'-acodec', 'pcm_s16le',
'-ar', '44100',
output_path
]
print(command)
# subprocess.run(command, check=True)
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print("function converted file")
print("Conversion completed")
except subprocess.CalledProcessError as e:
print(f"Error occurred while converting file: {e}")
return False
return True
def create_horizontal_function(strings_diff):
x_list = []
y_list = []
strings_list = strings_diff.split(" ")
# x-coordinates are a counter of the number of words in generated_text
for i in range(len(strings_list)):
if not '*' in strings_list[i]:
x_list.append(i + 1)
y_list.append(1)
return x_list, y_list
def find_discontinuity_points(strings_diff):
discontinuity_list = []
strings_list = strings_diff.split(" ")
for index, word in enumerate(strings_list):
print("strings list", strings_list)
if '*' in word:
discontinuity_list.append((index + 1, 1))
print("discontinuity list", discontinuity_list)
return discontinuity_list
def compare_strings(transcript, generated_text):
transcript_list = transcript.split(" ")
generated_text_list = generated_text.split(" ")
output_list = []
for word in generated_text_list:
if word in transcript_list:
output_list.append(word)
else:
output_list.append('*' + word + '*')
output_list_string = " ".join(output_list)
print(output_list_string)
return output_list_string
if __name__ == '__main__':
app.run(debug=True)