Spaces:
Sleeping
Sleeping
Yurii Paniv
commited on
Commit
•
a7a244e
1
Parent(s):
45a2dcd
Add Telegram bot endpoint
Browse files- Dockerfile +2 -0
- main.py +72 -0
- requirements.txt +3 -1
Dockerfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
FROM python:3.7
|
2 |
COPY . /app
|
3 |
WORKDIR /app
|
|
|
|
|
4 |
RUN wget https://github.com/robinhad/voice-recognition-ua/releases/download/v0.3/uk.tflite
|
5 |
RUN wget https://github.com/mozilla/DeepSpeech/releases/download/v0.9.1/deepspeech-0.9.1-models.tflite
|
6 |
RUN pip install -r requirements.txt
|
|
|
1 |
FROM python:3.7
|
2 |
COPY . /app
|
3 |
WORKDIR /app
|
4 |
+
RUN apt-get update
|
5 |
+
RUN apt-get install -y ffmpeg
|
6 |
RUN wget https://github.com/robinhad/voice-recognition-ua/releases/download/v0.3/uk.tflite
|
7 |
RUN wget https://github.com/mozilla/DeepSpeech/releases/download/v0.9.1/deepspeech-0.9.1-models.tflite
|
8 |
RUN pip install -r requirements.txt
|
main.py
CHANGED
@@ -1,7 +1,29 @@
|
|
1 |
from flask import Flask, render_template, request
|
2 |
from io import BytesIO
|
3 |
from client import client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
5 |
app = Flask(__name__,)
|
6 |
app.config['MAX_CONTENT_LENGTH'] = 120 * 1024
|
7 |
|
@@ -22,5 +44,55 @@ def recognize():
|
|
22 |
return result
|
23 |
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
if __name__ == '__main__':
|
26 |
app.run(host='0.0.0.0')
|
|
|
1 |
from flask import Flask, render_template, request
|
2 |
from io import BytesIO
|
3 |
from client import client
|
4 |
+
import telebot
|
5 |
+
import logging
|
6 |
+
import os
|
7 |
+
import warnings
|
8 |
+
from client import client
|
9 |
+
from io import BytesIO
|
10 |
+
import pydub
|
11 |
+
|
12 |
+
warnings.simplefilter('ignore')
|
13 |
+
TOKEN = os.environ['TOKEN']
|
14 |
+
|
15 |
+
if not TOKEN:
|
16 |
+
print('You must set the TOKEN environment variable')
|
17 |
+
exit(1)
|
18 |
+
|
19 |
+
START_MSG = '''Вітання!
|
20 |
+
Цей бот створений для тестування перекладу українських аудіозаписів в текст.
|
21 |
+
Група для обговорення: https://t.me/speech_recognition_uk'''
|
22 |
|
23 |
+
FIRST_STEP = '''Використовувати бота просто: надішліть аудіоповідомлення і чекайте відповіді'''
|
24 |
+
|
25 |
+
|
26 |
+
bot = telebot.TeleBot(TOKEN, parse_mode=None)
|
27 |
app = Flask(__name__,)
|
28 |
app.config['MAX_CONTENT_LENGTH'] = 120 * 1024
|
29 |
|
|
|
44 |
return result
|
45 |
|
46 |
|
47 |
+
@app.route('/bot/' + TOKEN, methods=['POST'])
|
48 |
+
def getMessage():
|
49 |
+
bot.process_new_updates(
|
50 |
+
[telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
|
51 |
+
return "!", 200
|
52 |
+
|
53 |
+
|
54 |
+
@app.route("/bot")
|
55 |
+
def webhook():
|
56 |
+
bot.remove_webhook()
|
57 |
+
bot.set_webhook(
|
58 |
+
url='https://voice-recognition-ua.herokuapp.com/bot/' + TOKEN)
|
59 |
+
return "!", 200
|
60 |
+
|
61 |
+
|
62 |
+
@bot.message_handler(commands=['start', 'help'])
|
63 |
+
def send_welcome(message):
|
64 |
+
bot.reply_to(message, START_MSG)
|
65 |
+
bot.reply_to(message, FIRST_STEP)
|
66 |
+
|
67 |
+
|
68 |
+
@bot.message_handler(content_types=['voice'])
|
69 |
+
def process_voice_message(message):
|
70 |
+
# download the recording
|
71 |
+
file_info = bot.get_file(message.voice.file_id)
|
72 |
+
downloaded_file = bot.download_file(file_info.file_path)
|
73 |
+
# create in-memory representation of files
|
74 |
+
source_audio = BytesIO()
|
75 |
+
source_audio.write(downloaded_file)
|
76 |
+
source_audio.seek(0)
|
77 |
+
output_audio = BytesIO()
|
78 |
+
ogg_file = pydub.AudioSegment.from_ogg(
|
79 |
+
source_audio)
|
80 |
+
|
81 |
+
# convert ogg to wav
|
82 |
+
ogg_file.set_frame_rate(16000).set_channels(
|
83 |
+
1).export(output_audio, "wav", codec="pcm_s16le")
|
84 |
+
output_audio.seek(0)
|
85 |
+
|
86 |
+
# do the recognition
|
87 |
+
# get the recognized text
|
88 |
+
text = client(source_audio)
|
89 |
+
# no results
|
90 |
+
if not text:
|
91 |
+
bot.reply_to(message, 'Я не зміг розпізнати 😢')
|
92 |
+
else:
|
93 |
+
# send the recognized text
|
94 |
+
bot.reply_to(message, text)
|
95 |
+
|
96 |
+
|
97 |
if __name__ == '__main__':
|
98 |
app.run(host='0.0.0.0')
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
Flask==1.1.2
|
2 |
deepspeech-tflite==0.9.1
|
3 |
numpy==1.17.0
|
4 |
-
uwsgi==2.0.19.1
|
|
|
|
|
|
1 |
Flask==1.1.2
|
2 |
deepspeech-tflite==0.9.1
|
3 |
numpy==1.17.0
|
4 |
+
uwsgi==2.0.19.1
|
5 |
+
pytelegrambotapi==3.7.6
|
6 |
+
pydub==0.24.1
|