/
bioxakep
/
VoiceTrans
Обзор
Документация
Войти
/
bioxakep
/
VoiceTrans
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
native_app.py
207 строк
9 KB
bioxakep
refactor
13 авг 2025, 23:54
13 авг 2025, 23:54
6aa60b3
Код
Авторство
О чём код?
import os.path import threading import tkinter as tk from tkinter import ttk, messagebox from tkinterdnd2 import DND_FILES, TkinterDnD from transcribation import smart_transcriber, TranscribeResult from utils_common import rename_sounds, get_phones, humanize_time from voice_engines import VoskRecognizer from config import app_config class Application(TkinterDnD.Tk): def __init__(self): super().__init__() self.title("Транскрибирование аудиофайлов") self.iconbitmap(default=app_config.icon_path) # icon_image = tk.PhotoImage(file=app_config.icon_path) # self.wm_iconphoto(False, icon_image) self.geometry("400x380") self.resizable(False, False) self.sound_path = None self.billing_path = None self._recognizer = None self.sounds_frame = tk.Frame( self, width=300, height=100, bg="lightblue", highlightbackground="gray", highlightthickness=1, ) self.sound_dnd = tk.Label( self.sounds_frame, text=r"Перенесите сюда папку со звуком", bg="lightblue" ) self.billing_frame = tk.Frame( self, width=300, height=100, bg="lightgreen", highlightbackground="gray", highlightthickness=1, ) self.bil_dnd = tk.Label( self.billing_frame, text="Перенесите сюда файл со статистикой \n если необходимо переименовать файлы", bg="lightgreen", ) self.separator = tk.Frame(self, height=1, bg="gray") self.status_label1 = tk.Label(self, text="Директория со звуком не выбрана") self.status_label2 = tk.Label(self, text="Файл со статистикой не выбран") self.separator = tk.Frame(self, height=1, bg="gray") self.start_button = tk.Button( self, text="Начать Транскрибирование", command=self.start_transcribe_task ) self.trans_progress = ttk.Progressbar( self, orient="horizontal", length=200, mode="determinate" ) self.wait_label = tk.Label(self, text="Загрузка транскрибатора") self.wait_label.pack(pady=100) self.wait_progress = ttk.Progressbar( self, orient="horizontal", length=200, mode="indeterminate" ) self.wait_progress.pack(pady=10) self.wait_progress.start() threading.Thread(target=self.init_recognizer).start() def init_recognizer(self): self._recognizer = VoskRecognizer() self.wait_progress.stop() self.wait_progress.pack_forget() self.wait_label.pack_forget() self.load_app_interface() def load_app_interface(self): # Create frames for drag-and-drop self.sounds_frame.pack(pady=10) self.sound_dnd.place(relx=0.5, rely=0.5, anchor=tk.CENTER) self.billing_frame.pack(pady=10) self.bil_dnd.place(relx=0.5, rely=0.5, anchor=tk.CENTER) self.separator.pack(fill="x", pady=10) self.status_label1.pack() self.status_label2.pack() self.separator.pack(fill="x", pady=10) self.start_button.pack() self.wait_progress.pack_forget() self.sounds_frame.dnd_bind("<<Drop>>", self.drop_folder) self.sounds_frame.drop_target_register(DND_FILES) self.billing_frame.dnd_bind("<<Drop>>", self.drop_billing) self.billing_frame.drop_target_register(DND_FILES) self.update() def start_transcribe(self): # Show the progress bar self.trans_progress.pack(pady=10) self.start_button.pack_forget() if self.sound_path is None: return tags_file_path = None dir_files_count = len(os.listdir(self.sound_path)) trans_files_count = 0 total_sound_duration: int = 0 total_speech_duration: int = 0 total_trans_time: int = 0 self.trans_progress["maximum"] = dir_files_count if self.billing_path is None or not os.path.exists(self.billing_path): print("Транскрибация без переименования") for f in os.listdir(self.sound_path): abonent, contact = get_phones(f) file_path: str = os.path.join(self.sound_path, f) trans_result = smart_transcriber( audio_file_path=file_path, recognizer=self._recognizer, tags_file_path=tags_file_path, abonent=abonent, contact=contact, ) if isinstance(trans_result, TranscribeResult): total_trans_time += trans_result.trans_time total_speech_duration += trans_result.speech_duration total_sound_duration += trans_result.sound_duration trans_files_count += 1 self.trans_progress["value"] += 1 self.update_idletasks() else: print("Транскрибация с переименованием") for sound_file_path, abonent, contact, sound_dt in rename_sounds( billing_file_path=self.billing_path, sounds_dir=self.sound_path, ): trans_result = smart_transcriber( audio_file_path=sound_file_path, recognizer=self._recognizer, tags_file_path=tags_file_path, abonent=abonent, contact=contact, sound_dt=sound_dt, ) if isinstance(trans_result, TranscribeResult): total_trans_time += trans_result.trans_time total_speech_duration += trans_result.speech_duration total_sound_duration += trans_result.sound_duration trans_files_count += 1 self.trans_progress["value"] += 1 self.update_idletasks() # Вставить сюда цикл транскрибации файлов из директории # Hide the progress bar after the task is completed self.trans_progress.pack_forget() self.start_button.pack() self.sound_path = None self.billing_path = None self.status_label1.text = "Директория со звуком не выбрана" self.status_label2.text = "Файл со статистикой не выбран" sound_duration_hum = humanize_time(total_sound_duration, clocks=False) speech_duration_hum = humanize_time(total_speech_duration, clocks=False) trans_time_hum = humanize_time(total_trans_time, clocks=False) print( f"Транскрибировано файлов: {trans_files_count}\n" f"Суммарная длительность аудиофайлов: {sound_duration_hum}\n" f"Суммарная длительность выделенной речи: {speech_duration_hum}\n" f"Общее время транскрибации: {trans_time_hum}" ) messagebox.showinfo("Готово!", "Результат в директории со звуком") self.update() def start_transcribe_task(self): if self.sound_path is None: messagebox.showinfo("Ошибка!", "Не указана директория со звуком!") return self.trans_progress["value"] = 0 self.update() # Start the task in a separate thread to avoid blocking the GUI threading.Thread(target=self.start_transcribe).start() def drop_folder(self, event): # Get the dropped file path for Field 1 self.sound_path = event.data.strip("{").strip("}") print(type(event.data)) if self.sound_path: # self.status_label1['text'] = f"Директория со звуком: {os.path.basename(self.sound_path)}" self.sound_dnd["text"] = ( f"Директория со звуком: {os.path.basename(self.sound_path)}" ) else: # self.status_label1['text'] = "Директория со звуком не выбрана" self.sound_dnd["text"] = "Директория со звуком не выбрана" def drop_billing(self, event): # Get the dropped file path for Field 2 self.billing_path = event.data.strip("{").strip("}") if self.billing_path: # self.status_label2['text'] = f"Файл со статистикой: {os.path.basename(self.billing_path)}" self.bil_dnd["text"] = ( f"Файл со статистикой: {os.path.basename(self.billing_path)}" ) else: # self.status_label2['text'] = "Файл со статистикой не выбран" self.bil_dnd["text"] = "Файл со статистикой не выбран" if __name__ == "__main__": app = Application() app.mainloop()