Skip to content

Commit e1ad302

Browse files
fix: fix linter warnings
1 parent 30f9d02 commit e1ad302

File tree

8 files changed

+137
-123
lines changed

8 files changed

+137
-123
lines changed

Dockerfile

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,60 @@
1-
FROM python:3.10 AS base
1+
FROM python:3.10 AS base
22

3-
ARG TELEGRAM_TOKEN
3+
ARG TELEGRAM_TOKEN
44

5-
ENV RUVERSION 0.22
6-
ENV TZ 'Europe/Moscow'
5+
ENV RUVERSION 0.22
6+
ENV TZ 'Europe/Moscow'
77

8-
RUN apt-get update && \
9-
DEBIAN_FRONTEND=noninteractive apt-get install -y \
10-
ffmpeg="7:5.1.6-0+deb12u1" \
11-
espeak="1.48.15+dfsg-3" \
12-
libsm6="2:1.2.3-1" \
13-
libxext6="2:1.3.4-1+b1" \
14-
git="1:2.39.5-0+deb12u2" \
15-
tzdata="2024b-0+deb12u1" && \
16-
apt-get clean
8+
RUN apt-get update && \
9+
DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-downgrades\
10+
ffmpeg="7:5.1.6-0+deb12u1" \
11+
espeak="1.48.15+dfsg-3" \
12+
libsm6="2:1.2.3-1" \
13+
libxext6="2:1.3.4-1+b1" \
14+
git="1:2.39.5-0+deb12u2" \
15+
tzdata="2025a-0+deb12u1" && \
16+
apt-get clean
1717

18-
RUN echo $TZ > /etc/timezone && \
19-
rm /etc/localtime && \
20-
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
21-
dpkg-reconfigure -f noninteractive tzdata
18+
RUN echo $TZ > /etc/timezone && \
19+
rm /etc/localtime && \
20+
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
21+
dpkg-reconfigure -f noninteractive tzdata
2222

2323

24-
FROM base AS emotion_recognition
24+
FROM base AS emotion_recognition
2525

26-
RUN pip3 install --no-cache-dir \
27-
torch==1.13.1+cu117 \
28-
torchvision>=0.13.1+cu117 \
29-
torchaudio>=0.13.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
26+
RUN pip3 install --no-cache-dir \
27+
torch==1.13.1+cu117 \
28+
torchvision>=0.13.1+cu117 \
29+
torchaudio>=0.13.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
3030

31-
WORKDIR /bot/emotion_recognition
31+
WORKDIR /bot/emotion_recognition
3232

33-
RUN git clone https://github.com/elizaveta-andreeva/Video-Audio-Face-Emotion-Recognition.git . && \
34-
git checkout 63f8b64b456fa3cdfd89379660a171145ea256cb
33+
RUN git clone https://github.com/elizaveta-andreeva/Video-Audio-Face-Emotion-Recognition.git . && \
34+
git checkout 9b3368ccdce1d571a362543633ade81eb0cbd622
3535

36-
RUN git clone https://github.com/rishiswethan/pytorch_utils.git source/pytorch_utils && \
37-
cd source/pytorch_utils && \
38-
git checkout v1.0.3 && \
39-
cd ../.. && \
40-
python3 setup.py
36+
RUN git clone https://github.com/rishiswethan/pytorch_utils.git source/pytorch_utils && \
37+
cd source/pytorch_utils && \
38+
git checkout v1.0.3 && \
39+
cd ../.. && \
40+
python3 setup.py
4141

42-
RUN pip3 install --no-cache-dir -r requirements.txt
43-
RUN python3 -m spacy download en_core_web_lg
42+
RUN pip3 install --no-cache-dir -r requirements.txt
43+
RUN python3 -m spacy download en_core_web_lg
4444

4545

46-
FROM emotion_recognition AS phsycho_bot
46+
FROM emotion_recognition AS phsycho_bot
4747

48-
WORKDIR /bot
48+
WORKDIR /bot
4949

50-
COPY src/requirements.txt src/
51-
RUN pip3 install --no-cache-dir -r src/requirements.txt
50+
COPY src/requirements.txt src/
51+
RUN pip3 install --no-cache-dir -r src/requirements.txt
5252

53-
# COPY tests/requirements.txt tests/
54-
# RUN pip3 install -r tests/requirements.txt
53+
# COPY tests/requirements.txt tests/
54+
# RUN pip3 install -r tests/requirements.txt
5555

56-
COPY . /bot/
56+
COPY . /bot/
5757

58-
CMD python3 src/bot.py ${TELEGRAM_TOKEN} ${MONGO_INITDB_ROOT_USERNAME} ${MONGO_INITDB_ROOT_PASSWORD}
58+
ENV PYTHONPATH="/bot:/bot/emotion_recognition:${PYTHONPATH}"
59+
60+
CMD python3 src/bot.py ${TELEGRAM_TOKEN} ${MONGO_INITDB_ROOT_USERNAME} ${MONGO_INITDB_ROOT_PASSWORD}

src/databases/db.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import datetime
22
import logging
33
from typing import List, Optional
4-
from string import punctuation
5-
from collections import Counter
6-
import nltk
7-
from nltk.corpus import stopwords
8-
from pymystem3 import Mystem
9-
104
import pytz
115
from pymodm import connect, fields, MongoModel
126
from pymodm.connection import _get_db
@@ -202,7 +196,6 @@ def init_survey_progress(
202196
focus,
203197
id_=0,
204198
survey_step=0,
205-
next_step=1,
206199
need_answer=False,
207200
user_answer="INIT PROGRESS",
208201
stats="",

src/emotion_analysis.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import os
2-
import sys
31
from string import punctuation
42
from collections import Counter
53
import nltk
64
from nltk.corpus import stopwords
75
from pymystem3 import Mystem
8-
9-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
10-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'emotion_recognition')))
11-
12-
from emotion_recognition.source.audio_analysis_utils.predict import predict
6+
from emotion_recognition.source.audio_analysis_utils.predict import predict # pylint: disable=import-error
137

148

159
def get_words_statistics(text: str):
@@ -24,7 +18,7 @@ def get_words_statistics(text: str):
2418

2519

2620
def predict_emotion(filename):
27-
emotion, predicted_label, prediction_string = predict(filename)
21+
emotion, _, _ = predict(filename)
2822
return emotion
2923

3024

src/kafka/kafka_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33

44
def load_conf(conf_file):
5-
with open(conf_file) as f:
6-
conf_val = json.load(f)
5+
with open(conf_file) as file:
6+
conf_val = json.load(file)
77
return conf_val

src/modules/stt_module/voice_module.py

Lines changed: 85 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import os
33
import time
44
import subprocess
5-
from collections import Counter
6-
7-
import requests
8-
95
from noisereduce import reduce_noise
106
from scipy.io import wavfile
117
from telegram import Update
@@ -20,52 +16,65 @@
2016
from utilities.wrapper import send_text
2117
from databases.db import push_user_survey_progress, init_user, get_user_audio
2218

23-
from env_config import (DEBUG_MODE,
24-
DEBUG_ON, DEBUG_OFF, TOKEN)
19+
from env_config import (DEBUG_MODE, DEBUG_ON)
2520
from kafka.kafka_producer import produce_message
2621

2722

28-
def split_audio(wav_filename, unique_file_id, min_chunk_length=30000, max_chunk_length=40000, silence_thresh=-40, min_silence_len=500):
29-
audio = AudioSegment.from_wav(wav_filename)
30-
chunk_dir_name = os.path.join('emotion_recognition', 'input_files')
31-
if not os.path.exists(chunk_dir_name):
32-
os.makedirs(chunk_dir_name)
33-
chunk_filenames = []
34-
chunk_start_times = []
35-
36-
if len(audio) <= min_chunk_length:
37-
chunk_filename = os.path.join(chunk_dir_name, unique_file_id + "_chunk_0.wav")
38-
audio.export(chunk_filename, format="wav")
39-
return [chunk_filename], [0]
40-
23+
def get_silence_points(audio, min_silence_len, silence_thresh):
4124
silence_ranges = detect_silence(audio, min_silence_len=min_silence_len, silence_thresh=silence_thresh)
42-
silence_points = [(start + end) / 2 for start, end in silence_ranges]
25+
return [(start + end) / 2 for start, end in silence_ranges]
4326

27+
28+
def generate_chunks(audio, silence_points, min_len, max_len):
4429
chunks = []
30+
starts = []
4531
start = 0
4632

4733
for silence in silence_points:
4834
chunk_length = silence - start
49-
if min_chunk_length <= chunk_length <= max_chunk_length:
50-
chunks.append(audio[start:silence])
51-
chunk_start_times.append(start)
52-
start = silence
53-
elif chunk_length > max_chunk_length:
54-
split_point = start + max_chunk_length
35+
if min_len <= chunk_length <= max_len:
36+
chunks.append(audio[start:int(silence)])
37+
starts.append(start)
38+
start = int(silence)
39+
elif chunk_length > max_len:
40+
split_point = start + max_len
5541
chunks.append(audio[start:split_point])
56-
chunk_start_times.append(start)
42+
starts.append(start)
5743
start = split_point
5844

5945
if start < len(audio):
6046
chunks.append(audio[start:])
61-
chunk_start_times.append(start)
47+
starts.append(start)
48+
49+
return chunks, starts
50+
6251

52+
def export_chunks(chunks, base_dir, file_id):
53+
filenames = []
6354
for i, chunk in enumerate(chunks):
64-
chunk_filename = os.path.join(chunk_dir_name, unique_file_id + f"_chunk_{i}.wav")
65-
chunk.export(chunk_filename, format="wav")
66-
chunk_filenames.append(chunk_filename)
55+
filename = os.path.join(base_dir, f"{file_id}_chunk_{i}.wav")
56+
chunk.export(filename, format="wav")
57+
filenames.append(filename)
58+
return filenames
6759

68-
return chunk_filenames, chunk_start_times
60+
61+
def split_audio(wav_filename, unique_file_id, min_chunk_length=30000, max_chunk_length=40000, silence_thresh=-40,
62+
min_silence_len=500):
63+
audio = AudioSegment.from_wav(wav_filename)
64+
chunk_dir = os.path.join('emotion_recognition', 'input_files')
65+
if not os.path.exists(chunk_dir):
66+
os.makedirs(chunk_dir)
67+
68+
if len(audio) <= min_chunk_length:
69+
filename = os.path.join(chunk_dir, f"{unique_file_id}_chunk_0.wav")
70+
audio.export(filename, format="wav")
71+
return [filename], [0]
72+
73+
silence_points = get_silence_points(audio, min_silence_len, silence_thresh)
74+
chunks, start_times = generate_chunks(audio, silence_points, min_chunk_length, max_chunk_length)
75+
filenames = export_chunks(chunks, chunk_dir, unique_file_id)
76+
77+
return filenames, start_times
6978

7079

7180
def download_voice(update: Update):
@@ -121,46 +130,63 @@ def work_with_audio(update: Update, context: CallbackContext):
121130
produce_message('stt', json.dumps(message))
122131

123132

124-
def audio_to_text(filename, ogg_filename, chunk_filenames, chunk_start_times, update_id, user):
125-
processing_start_time = time.time()
126-
input_sentence, stats_sentence = "", ""
127-
emotions = Counter()
128-
audio_emotions_statistics = []
133+
def process_chunk(chunk_filename, start_time):
134+
response = get_att_whisper(chunk_filename)
129135

130-
for chunk_filename, start_time in zip(chunk_filenames, chunk_start_times):
131-
response = get_att_whisper(chunk_filename)
136+
if response.status_code != 200:
137+
return None
132138

133-
if response.status_code == 200:
134-
chunk_input_sentence = RecognizedSentence(response.json())
135-
else:
136-
return
139+
sentence = RecognizedSentence(response.json())
140+
word, emotion = associate_words_with_emotions(os.path.basename(chunk_filename), sentence.get_text())
137141

138-
word, emotion = associate_words_with_emotions(chunk_filename.split('/')[-1], chunk_input_sentence.get_text())
139-
emotions.update([emotion])
142+
return {
143+
"text": sentence.get_text(),
144+
"stats": sentence.generate_stats(),
145+
"emotion": emotion,
146+
"word": word,
147+
"start_time": start_time,
148+
"filename": chunk_filename,
149+
}
140150

141-
text = chunk_input_sentence.get_text()
142151

143-
audio_emotions_statistics.append({"filename": chunk_filename, "emotion": emotion, "word": word, "text": text, "start_time": start_time})
152+
def audio_to_text(filename, ogg_filename, chunk_filenames, chunk_start_times, update_id, user):
153+
start_time = time.time()
154+
full_text = []
155+
stats_blocks = []
156+
emotion_stats = []
157+
158+
for chunk_filename, start_time_chunk in zip(chunk_filenames, chunk_start_times):
159+
result = process_chunk(chunk_filename, start_time_chunk)
160+
if not result:
161+
return
144162

145-
input_sentence += text
146-
stats_sentence += chunk_input_sentence.generate_stats() + "\n"
163+
full_text.append(result["text"])
164+
stats_blocks.append(result["stats"])
165+
emotion_stats.append({
166+
"filename": result["filename"],
167+
"emotion": result["emotion"],
168+
"word": result["word"],
169+
"text": result["text"],
170+
"start_time": result["start_time"]
171+
})
147172

148173
if DEBUG_MODE == DEBUG_ON:
149-
processing_end_time = time.time()
150-
processing_time = processing_end_time - processing_start_time
151-
send_text(user.id, f"Processing time: {processing_time:.2f} seconds")
174+
elapsed = time.time() - start_time
175+
send_text(user.id, f"Processing time: {elapsed:.2f} seconds")
152176

153177
push_user_survey_progress(
154-
user,
155-
init_user(user).get_last_focus(),
156-
update_id,
157-
user_answer=input_sentence,
158-
stats=stats_sentence,
178+
user=user,
179+
focus=init_user(user).get_last_focus(),
180+
id_=update_id,
181+
user_answer="".join(full_text),
182+
stats="\n".join(stats_blocks),
159183
audio_file=open(ogg_filename, 'rb'), # pylint: disable=consider-using-with
160-
audio_emotions_statistics=audio_emotions_statistics
184+
audio_emotions_statistics=emotion_stats
161185
)
186+
162187
os.remove(ogg_filename)
163188

164189
if DEBUG_MODE == DEBUG_ON:
165190
print(get_user_audio(user))
166-
send_text(user.id, "ID записи с твоим аудиосообщением в базе данных: " + str(json.loads(json_util.dumps(get_user_audio(user)))))
191+
send_text(user.id, "ID записи с твоим аудиосообщением в базе данных: " +
192+
str(json.loads(json_util.dumps(get_user_audio(user)))))

src/my_cron.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import sys
44
import time
55
from collections import Counter
6-
7-
import pytz
86
from typing import List
97

8+
import pytz
109
import schedule
1110
from telegram.ext import Updater
1211

src/script_engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ def get_next_step(self) -> Step:
153153
self.survey_progress.save()
154154
step_number = self.survey_progress.survey_next
155155
# Генерация нового
156-
new_step_info = Script(Parser(TREE_EXAMPLE_PATH).parse()).get_script(self.last_focus)[
157-
step_number
158-
]
156+
# new_step_info = Script(Parser(TREE_EXAMPLE_PATH).parse()).get_script(self.last_focus)[
157+
# step_number
158+
# ]
159159
new_survey_progress = init_survey_progress(
160-
self.user, self.last_focus, self.update.update_id, step_number, new_step_info['next']
160+
user=self.user, focus=self.last_focus, id_=self.update.update_id, survey_step=step_number
161161
)
162162
next_step = Step(self.update, new_survey_progress, self.last_focus)
163163
return next_step

src/utilities/wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ def send_text(user_id, text):
7474
if response.status_code == 200:
7575
print('Request send successfully')
7676
else:
77-
print(f'Error sending request: {response.json()["description"]}')
77+
print(f'Error sending request: {response.json()["description"]}')

0 commit comments

Comments
 (0)