|
1 |
| -from flask import Flask, render_template, url_for, redirect |
| 1 | +from flask import Flask, render_template, url_for, redirect, current_app |
2 | 2 | import azure.cognitiveservices.speech as speechsdk
|
3 |
| -# #import text_to_speech as t_t_s |
| 3 | +from flask_sqlalchemy import SQLAlchemy |
4 | 4 | from text_to_speech import get_correct_sound
|
5 | 5 | from text_to_speech import get_audio_length
|
6 |
| -import sounddevice as sd |
| 6 | +#import sounddevice as sd |
7 | 7 | from scipy.io.wavfile import write
|
| 8 | +from forms import CorrectSpeechForm, UserSpeechForm |
| 9 | +import os.path |
| 10 | +from os import path |
| 11 | +import time |
| 12 | + |
8 | 13 | # from record_audio import record_audio
|
9 |
| -# from speech_to_text import get_text_from_input |
| 14 | +from speech_to_text import get_text_from_input |
10 | 15 | # from compare_text import *
|
| 16 | + |
11 | 17 | app = Flask(__name__)
|
12 | 18 | speech_key, service_region = "c87da06e1dfe4dd3b6e58fa41ec19c95", "eastus"
|
13 | 19 | speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
|
| 20 | +app.config['SECRET_KEY'] = "4cf9c9881c554ef032f3a12c7f225dea" |
| 21 | +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' |
| 22 | +db = SQLAlchemy(app) |
| 23 | + |
| 24 | +class Speech(db.Model): |
| 25 | + id = db.Column(db.Integer, primary_key=True) |
| 26 | + correct_text = db.Column(db.String()) |
| 27 | + correct_audio_filename = db.Column(db.String()) |
| 28 | + # correct_audio = db.Column(None) |
| 29 | + # user_audio = db.Column(None) |
| 30 | + user_audio_location = db.Column(db.String()) |
| 31 | + user_text = db.Column(db.String()) |
| 32 | + def __repr__(self): |
| 33 | + return f"Speech('{self.id}', '{self.correct_text}', '{self.correct_audio_filename}')" |
| 34 | + |
14 | 35 | data = {
|
15 |
| - "correct_text": "Hello! I love my cat and my dog", |
16 |
| - "path": "D:/Haverford/LocalHack/speech_analysis/reconnect_app/static/Sounds/", |
17 |
| - "correct_audio_filename": "correct_sound.wav" |
| 36 | + "correct_text": "This app doesn't work", |
| 37 | + "path": "D:/Haverford/LocalHack/speech_analysis/reconnect_app/static/CorrectSounds/", |
| 38 | + "correct_audio_filename": "correct_sound", |
| 39 | + "user_audio_filename" : "user_sound" |
| 40 | + |
18 | 41 | }
|
19 | 42 |
|
| 43 | +def get_file_name(filename): |
| 44 | + if filename[0]=="1": |
| 45 | + return filename[1:] |
| 46 | + else: |
| 47 | + return filename.split(".")[-1]+ "1" + filename.split(".")[-1] |
| 48 | + |
| 49 | +def generate_user_text(user_audio_filename): |
| 50 | + new_file_name = str(time.time()) + data["user_audio_filename"] + str(time.time()) + ".wav" |
| 51 | + path_to_file = os.path.join(app.root_path, "static/Sounds", new_file_name) |
| 52 | + return get_text_from_input(path_to_file, speech_config) |
| 53 | + |
20 | 54 | def generate_correct_sound(correct_text):
|
21 |
| - correct_text = correct_text |
22 |
| - path_to_file = data["path"] + data["correct_audio_filename"] |
| 55 | + new_file_name = data["correct_audio_filename"] + str(time.time()) + ".wav" |
| 56 | + print(new_file_name) |
| 57 | + path_to_file = os.path.join(app.root_path, "static/Sounds", new_file_name) |
| 58 | + if path.exists("static/Sounds/*"): |
| 59 | + os.remove("static/Sounds/*") |
23 | 60 | correct_audio = get_correct_sound(path_to_file, correct_text, speech_config)
|
24 |
| - return data["correct_audio_filename"] |
| 61 | + return "static/Sounds/" + new_file_name |
25 | 62 |
|
26 | 63 | @app.route("/")
|
27 |
| -@app.route("/learn") |
28 | 64 | def home():
|
29 |
| - correct_sound_address = generate_correct_sound(data["correct_text"]) |
| 65 | + return render_template('index.html', title="Reconnect - Main") |
30 | 66 |
|
31 |
| - return render_template('index.html', correct_sound_address=correct_sound_address, cor_str=data["correct_text"]) |
| 67 | +@app.route("/learn", methods=["GET", "POST"]) |
| 68 | +def learn(): |
| 69 | + correct_form = CorrectSpeechForm() |
| 70 | + if correct_form.submitc.data and correct_form.validate_on_submit(): |
| 71 | + user_form = UserSpeechForm() |
| 72 | + print(correct_form.correct_text.data) |
| 73 | + correct_sound_address = generate_correct_sound(correct_form.correct_text.data) |
| 74 | + correct_audio = open(correct_sound_address) |
| 75 | + speech = Speech(correct_text=correct_form.correct_text.data, correct_audio_filename=correct_sound_address) |
| 76 | + print("from learn -> ", speech) |
| 77 | + db.session.add(speech) |
| 78 | + db.session.commit() |
| 79 | + return redirect(url_for('practice')) |
32 | 80 |
|
| 81 | + return render_template('learn.html', correct_form=correct_form, title="Learn - Reconnect") |
33 | 82 |
|
| 83 | +@app.route("/practice", methods=["GET", "POST"]) |
| 84 | +def practice(): |
| 85 | + user_form = UserSpeechForm() |
| 86 | + correct_form = CorrectSpeechForm() |
| 87 | + speech = Speech.query.order_by(-Speech.id).first() |
| 88 | + |
| 89 | + print("we got to practice route") |
| 90 | + print(speech) |
| 91 | + #doesn't validate the form?? doesn't get into next lines |
| 92 | + if user_form.submitu.data and user_form.validate_on_submit(): |
| 93 | + print("Got the sound!") |
| 94 | + speech.user_audio_location = user_form.user_speech.data.name |
| 95 | + speech.user_text = generate_user_text(user_form.user_speech.data.name) |
| 96 | + db.session.commit() |
| 97 | + return url_for('feedback', speech=speech) |
| 98 | + return render_template('practice.html', correct_text=speech.correct_text, correct_form=correct_form, user_form=user_form, correct_sound_address=str(speech.correct_audio_filename), title="Practice - Reconnect") |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +def learn2(): |
| 104 | + if speech: |
| 105 | + user_form = UserSpeechForm() |
| 106 | + if user_form.validate_on_submit(): |
| 107 | + print("Got the sound!") |
| 108 | + user_text = generate_user_text(user_form.user_speech.data.name) |
| 109 | + speech.user_audio = user_form.user_speech |
| 110 | + speech.user_text = user_text |
| 111 | + db.session.commit() |
| 112 | + return render_template('learn.html', correct_form=correct_form, title="Learn - Reconnect", correct_text = correct_form.correct_text.data, speech=speech, correct_sound_address=correct_sound_address) |
34 | 113 |
|
35 | 114 |
|
36 | 115 | @app.route("/feedback")
|
37 |
| -def start(): |
38 |
| - |
39 |
| - # |
40 |
| - #correct_audio_filename = "/static/correct_sound.wav" |
41 |
| - #correct_text = data["correct_text"] |
42 |
| - # |
43 |
| - # correct_audio = get_correct_sound(correct_audio_filename, correct_text, speech_config) |
44 |
| - # correct_length = get_audio_length(correct_audio_filename) |
45 |
| - # |
46 |
| - # input_audio_filename = "input_sound.wav" |
47 |
| - # input_text = get_text_from_input(input_audio_filename, speech_config) |
48 |
| - # |
49 |
| - # differences = compare(correct_text, input_text) |
50 |
| - # difs_og = keys(differences) |
51 |
| - # difs_new = values(differences) |
52 |
| - # |
53 |
| - # spl_og = correct_text.split(" ") |
54 |
| - # spl_new = input_text.split(" ") |
55 |
| - # if(is_empty(differences)): |
56 |
| - # difs_og = None |
57 |
| - # difs_new = None |
58 |
| - # return render_template('index2.html', cor_str=data["correct_text"], input_text=input_text, difs_og=difs_og, difs_new=difs_new, spl_og=spl_og, spl_new=spl_new) |
59 |
| - return render_template('index2.html') |
| 116 | +def feedback(): |
| 117 | + |
| 118 | + return render_template('feedback.html') |
60 | 119 |
|
61 | 120 |
|
62 | 121 | if __name__ == '__main__':
|
|
0 commit comments