-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
110 lines (87 loc) · 3.63 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
from flask import Flask, request, render_template, render_template_string, send_file
import pandas as pd
import numpy as np
import os
import re
import nltk
from nltk.corpus import stopwords
from gensim.models import Word2Vec
nltk.download('punkt')
nltk.download('stopwords')
stop_words = stopwords.words('english')
# function to preprocess text
def preprocess_text(text):
text = text.lower()
text = re.sub(r'\W+', ' ', text)
# tokenize text
text = nltk.word_tokenize(text)
return text
df = pd.read_csv('./static/UrbanSound8K.csv')
class_column = df['class']
audio_names = class_column.tolist()
class_to_filename = dict(zip(df['class'], df['slice_file_name']))
#print(audio_names[:5])
extended_audio_names = []
for name in audio_names:
extended_audio_names.append(name.split('_'))
processed_audio_names = [preprocess_text(name) for name in audio_names] + extended_audio_names
# Train a Word2Vec model
model = Word2Vec(sentences=processed_audio_names, min_count=1, vector_size=100, window=5, workers=4)
# display model vocabulary
words = list(model.wv.key_to_index)
def get_similar_words(input_text, topn=3):
print("Running get_similar_words")
print(str(input_text))
input_words = preprocess_text(input_text)
class_scores = {class_name: 0 for class_name in set(df['class'])}
for word in input_words:
if word in model.wv.key_to_index:
for class_name in class_scores.keys():
similarity = model.wv.similarity(word, class_name)
class_scores[class_name] = max(class_scores[class_name], similarity)
sorted_scores = sorted(class_scores.items(), key=lambda x: x[1], reverse=True)[:topn]
similar_classes = [class_name for class_name, _ in sorted_scores]
if similar_classes:
# Select only rows with the similar classes
similar_rows = df[df['class'].isin(similar_classes)][['slice_file_name', 'fold']]
# Replace any remaining backslashes with forward slashes
similar_rows['slice_file_name'] = similar_rows['slice_file_name'].str.replace('\\', '/')
# Generate full file path of the audio files
audio_files = []
for file_name, fold in similar_rows.itertuples(index=False):
audio_file_path = os.path.join('audio_files', 'static', f'fold{fold}', file_name)
audio_files.append((file_name, audio_file_path))
return audio_files[:topn]
else:
return []
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
input_text = request.form['input_text']
similar_files = get_similar_words(input_text, topn=3)
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
results_template = '''
{% if results %}
<h2>Results:</h2>
{% for file_name, file_path in results %}
<h3>{{ file_name }}</h3>
<audio controls>
<source src="{{ file_path }}" type="audio/wav">
Your browser does not support the audio tag.
</audio>
{% endfor %}
{% endif %}
'''
return render_template_string(results_template, results=similar_files)
else:
return render_template('index.html', results=similar_files)
else:
return render_template('index.html')
# Define route for serving audio files
@app.route('/audio_files/<path:file_path>')
def get_audio(file_path):
formatted_file_path = file_path.replace('\\', '/')
return send_file(formatted_file_path, mimetype='audio/wav')
if __name__ == '__main__':
app.run(debug=True)