-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
80 lines (59 loc) · 2.36 KB
/
main.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
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, RegexHandler, CallbackQueryHandler, Filters
from pydub import AudioSegment
from sklearn.preprocessing import Normalizer
from sklearn.decomposition import PCA
import logging
import numpy as np
import pickle
import subprocess
import os
# get token from token.conf
TOKEN = open("token.conf", "r").read().strip()
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
# load model
svm = pickle.load(open("models_trained/svm.sav", 'rb'))
lr = pickle.load(open("models_trained/lr.sav", 'rb'))
# svm = pickle.load(open("models_trained/normed_svm.sav", 'rb'))
# pca = pickle.load(open("models_trained/pca.sav", 'rb'))
# norm = Normalizer(norm='l2')
def start(bot, update):
update.message.reply_text('Hi! Send me a vocal message and I tell you if you are "male" or "female"!')
def predict(bot, update):
file_id = update.message.voice.file_id
new_file = bot.get_file(file_id)
new_file.download('voice.ogg')
sound = AudioSegment.from_ogg("voice.ogg")
sound.export("voice.wav", format="wav")
FNULL = open(os.devnull, 'w')
subprocess.call(('Rscript', "R/extract_feature.r"), stdout=FNULL, stderr=subprocess.STDOUT)
# read second line of csv file (so exclude header)
sample = open("my_voice.csv", "r").read().split("\n")[1].split(",")
sample = [sample]
# sample = norm.transform(np.float64(sample))
# sample = pca.transform(np.float64(sample))
text = ""
if int(svm.predict(sample)[0]) == 0:
# update.message.reply_text("You are male!")
text += "for <b>SVM</b> you are <b>male</b>"
else:
# update.message.reply_text("You are female!")
text += "for <b>SVM</b> you are <b>female</b>"
text += "\n"
if int(lr.predict(np.float64(sample))[0]) == 0:
text += "for <b>LR</b> you are <b>male</b>"
else:
text += "for <b>LR</b> you are <b>female</b>"
update.message.reply_text("We apply two algorithms:\n"+text, parse_mode='HTML')
def main():
updater = Updater(TOKEN)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.voice, predict))
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('help', start))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()