From 8ba0f89f895ba3fab39a1f95d97463a3e53a9a7d Mon Sep 17 00:00:00 2001 From: Freestyle-Play Date: Wed, 8 Oct 2025 16:07:30 +0300 Subject: [PATCH] intermediate commit --- app.py | 6 +- handlers/glueAudio.py | 18 ++++-- handlers/gradioHandler.py | 21 +++++-- requirements.txt | 115 ++++++++++++++++++++++++++++++++++++++ test.py | 19 +++++++ 5 files changed, 167 insertions(+), 12 deletions(-) create mode 100644 test.py diff --git a/app.py b/app.py index d4a29e2..827029c 100644 --- a/app.py +++ b/app.py @@ -113,7 +113,11 @@ def main(): saveFileCheckbox.change(gh.updateTextbox, inputs=saveFileCheckbox, outputs=filename) saveFileCheckbox.change(gh.updateTextbox, inputs=saveFileCheckbox, outputs=filenamePdf) - recognizeBtn.click(gh.handleRecognizeBtn, outputs=[recognizedText], inputs=[audioFiles, fastWhisperModel, device, compute_type, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText, gr.State(GLUED_AUDIO_FILENAME), gr.State(OUTPUT_PATH)]) + recognizeBtn.click( + gh.handleRecognizeBtn, + inputs=[audioFiles, fastWhisperModel, device, compute_type, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText, gr.State(GLUED_AUDIO_FILENAME), gr.State(OUTPUT_PATH)] + outputs=[recognizedText], + ) # Если пайплайн включен то тогда делаем автоматически # автоматический пайплайн diff --git a/handlers/glueAudio.py b/handlers/glueAudio.py index 55e10c1..5ff4d78 100644 --- a/handlers/glueAudio.py +++ b/handlers/glueAudio.py @@ -1,11 +1,17 @@ from pydub import AudioSegment class GlueAudio(): - def glue(self, audioFiles): - glued = AudioSegment.empty() + def glue(self, audio_files: list, output_path: str, output_filename: str) -> Path: + """ + Склеивает аудиофайлы с помощью FFmpeg, используя промежуточный список файлов. + Этот метод чрезвычайно эффективен по памяти и скорости. - for audioFile in audioFiles: - audio = AudioSegment.from_file(audioFile) - glued += audio + Args: + audio_files (list): Список путей к исходным аудиофайлам. + output_path (str): Директория для сохранения итогового файла. + output_filename (str): Имя итогового склеенного файла. + + Returns: + Path: Путь к созданному склеенному файлу. + """ - return glued diff --git a/handlers/gradioHandler.py b/handlers/gradioHandler.py index aa5120c..25d91c3 100644 --- a/handlers/gradioHandler.py +++ b/handlers/gradioHandler.py @@ -9,13 +9,24 @@ class GradioHandlers: self.ga = GlueAudio() self.ConvertMdToPdf = ConvertMdToPdf() self.FasterWhisper = FasterWhisper() - self.llm_factory = llm_factory # Сохраняем фабрику + self.llm_factory = llm_factory - def handleRecognizeBtn(self, audioFiles, model, device, compute_type, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText, filename, outPath): - audioFile = self.ga.glue(audioFiles) - file = self.fh.saveFile(filename, audioFile, outPath) + def handleRecognizeBtn( + self, + audioFiles, model, device, compute_type, beamSize, vadFilter, + minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, + noSpeechThreshold, conditionOnPreviousText, filename, outPath + ): + try: + audioFile = self.ga.glue(audioFiles, output) + file = self.fh.saveFile(filename, audioFile, outPath) - return self.FasterWhisper.recognize(model, device, compute_type, file, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText) + return self.FasterWhisper.recognize(model, device, compute_type, file, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText) + + except (FileNotFoundError, RuntimeError) as e: + # Если FFmpeg не найден или произошла ошибка, сообщаем пользователю + gr.Warning(str(e)) + return "" # Возвращаем пустую строку в текстовое поле # Функция улучшения текста def generateByCondition(self, api_key, llm_provider, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile, filename, filenamePdf, output_path): diff --git a/requirements.txt b/requirements.txt index f077318..7e6d37c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -100,3 +100,118 @@ uvicorn==0.35.0 webencodings==0.5.1 websockets==15.0.1 zopfli==0.2.3.post1 +aiofiles==24.1.0 +aiohappyeyeballs==2.6.1 +aiohttp==3.13.0 +aiosignal==1.4.0 +annotated-types==0.7.0 +anyio==4.10.0 +attrs==25.4.0 +audioop-lts==0.2.2 +av==15.1.0 +beautifulsoup4==4.13.5 +Brotli==1.1.0 +bs4==0.0.2 +certifi==2025.8.3 +cffi==2.0.0 +charset-normalizer==3.4.3 +click==8.2.1 +coloredlogs==15.0.1 +colour==0.1.5 +cssselect2==0.8.0 +ctranslate2==4.6.0 +distro==1.9.0 +dotenv==0.9.9 +exceptiongroup==1.3.0 +fastapi==0.116.1 +faster-whisper==1.2.0 +ffmpeg-python==0.2.0 +ffmpy==0.6.1 +filelock==3.19.1 +flatbuffers==25.2.10 +flatlatex==0.15 +fonttools==4.59.2 +frozenlist==1.8.0 +fsspec==2025.9.0 +future==1.0.0 +g4f==0.6.3.5 +gradio==5.44.1 +gradio_client==1.12.1 +groovy==0.1.2 +h11==0.16.0 +hf-xet==1.1.9 +httpcore==1.0.9 +httpx==0.28.1 +huggingface-hub==0.34.4 +humanfriendly==10.0 +idna==3.10 +iso639-lang==2.6.3 +Jinja2==3.1.6 +jiter==0.10.0 +joblib==1.5.2 +langdetect==1.0.9 +littleutils==0.2.4 +markdown-it-py==3.0.0 +markdown_pdf==1.9 +MarkupSafe==3.0.2 +mdurl==0.1.2 +mpmath==1.3.0 +multidict==6.7.0 +nest-asyncio==1.6.0 +nltk==3.9.1 +numpy==2.2.6 +onnxruntime==1.22.1 +openai==1.106.1 +orjson==3.11.3 +outdated==0.2.2 +packaging==25.0 +pandas==2.3.2 +pillow==11.3.0 +propcache==0.4.0 +protobuf==6.32.0 +pycparser==2.22 +pycryptodome==3.23.0 +pydantic==2.11.7 +pydantic_core==2.33.2 +pydub==0.25.1 +pydyf==0.11.0 +Pygments==2.19.2 +pylatexenc==2.10 +pymultidictionary==1.3.2 +PyMuPDF==1.26.4 +pyperclip==1.9.0 +pyphen==0.17.2 +python-dateutil==2.9.0.post0 +python-dotenv==1.1.1 +python-multipart==0.0.20 +pytz==2025.2 +PyYAML==6.0.2 +regex==2025.9.1 +requests==2.32.5 +rich==14.1.0 +ruff==0.12.12 +safehttpx==0.1.6 +semantic-version==2.10.0 +setuptools==80.9.0 +shellingham==1.5.4 +six==1.17.0 +sniffio==1.3.1 +soupsieve==2.8 +starlette==0.47.3 +sympy==1.14.0 +tinycss2==1.4.0 +tinyhtml5==2.0.0 +tkmacosx==1.0.5 +tokenizers==0.22.0 +tomlkit==0.13.3 +tqdm==4.67.1 +typer==0.17.4 +typing-inspection==0.4.1 +typing_extensions==4.15.0 +tzdata==2025.2 +urllib3==2.5.0 +uvicorn==0.35.0 +webencodings==0.5.1 +websockets==15.0.1 +yarl==1.22.0 +zopfli==0.2.3.post1 diff --git a/test.py b/test.py new file mode 100644 index 0000000..7cb2044 --- /dev/null +++ b/test.py @@ -0,0 +1,19 @@ +from handlers.convertMdToPdf import ConvertMdToPdf +import re +# Создаем экземпляр класса +converter = ConvertMdToPdf() + +# Тестовые примеры дробей +test_cases = [ + r"$\frac{1}{2}$", # простая дробь + r"$\dfrac{3}{4}$", # дробь с displaystyle + r"$\frac{a}{b} + \frac{c}{d}$", # сложение дробей + r"$$\frac{x^2}{y^3}$$", # блочная дробь + r"$\frac{\partial f}{\partial x}$" # частная производная +] + +for latex in test_cases: + result = converter.replace_math(re.search(r'\$\$(.*?)\$\$|\$(.*?)\$', latex)) + print(f"Input: {latex}") + print(f"Output: {result}") + print("---") \ No newline at end of file