From 83e19c471a30152c085e44f5ee3250c055b82b74 Mon Sep 17 00:00:00 2001 From: swrneko Date: Tue, 9 Sep 2025 18:24:18 +0300 Subject: [PATCH] add changes in pdf logic changes: 1. Remove `/` file (just missed click when create it); 2. Add filename input for pdf; 3. Now pdf file saves. --- "\\" | 24 ------------------------ app.py | 25 +++++++++++++++++-------- config.py | 4 +--- services/convertMdToPdf.py | 4 ++-- services/fasterWhisper.py | 2 +- 5 files changed, 21 insertions(+), 38 deletions(-) delete mode 100644 "\\" diff --git "a/\\" "b/\\" deleted file mode 100644 index 7809987..0000000 --- "a/\\" +++ /dev/null @@ -1,24 +0,0 @@ -import re -from pylatexenc.latex2text import LatexNodes2Text - -class ConvertMdToPdf: - # Конвертирует md в pdf - def convertLatexToText(self, text:str): - # Обрабатываем только математические выражения - text = re.sub( - r'\$\$(.*?)\$\$|\$(.*?)\$', - self.replace_math, - text, - flags=re.DOTALL - ) - return text - - def replace_math(self, match): - math_content = match.group(1) or match.group(2) # $$...$$ или $...$ - try: - # Преобразуем только математическое выражение - converted = LatexNodes2Text().latex_to_text(math_content) - return converted - except: - return math_content # В случае ошибки оставляем как есть - diff --git a/app.py b/app.py index 18c6d70..95ae48b 100644 --- a/app.py +++ b/app.py @@ -11,7 +11,7 @@ from services.convertMdToPdf import ConvertMdToPdf from config import * # Функция транскрибации -def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile): +def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile, filename, filenamePdf): llm = Llm(api_key) # если чекбокс включен и событие было change → обрабатываем @@ -19,10 +19,11 @@ def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_ result, md = llm.generate(llm_model, system_prompt, recognized_text, llm_temperature) # Конвертируем текст с латексом в юникод - unicodeText = ConvertMdToPdf().convertLatexToText(md) + pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md) if isSaveFile: - saveFile("output.txt", result) + savePdf(filenamePdf, pdf) + saveFile(filename, result) return result, unicodeText @@ -31,10 +32,11 @@ def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_ result, md = llm.generate(llm_model, system_prompt, recognized_text, llm_temperature) # Конвертируем текст с латексом в юникод - unicodeText = ConvertMdToPdf().convertLatexToText(md) + pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md) if isSaveFile: - saveFile("output.txt", result) + savePdf(filenamePdf, pdf) + saveFile(filename, result) return result, unicodeText @@ -42,6 +44,12 @@ def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_ return gr.skip(), gr.skip() +def savePdf(filename, pdf): + directory = Path(OUTPUT_PATH) + filePath = directory / filename + filePath.parent.mkdir(parents=True, exist_ok=True) + pdf.save(filePath) + # Функция сохранеhния файла def saveFile(filename, text): directory = Path(OUTPUT_PATH) @@ -93,6 +101,7 @@ with gr.Blocks() as demo: with gr.Group(): saveFileCheckbox = gr.Checkbox(label='save file', value=True, interactive=True) filename = gr.Textbox(label='Output filename', value='output.txt', interactive=True) + filenamePdf = gr.Textbox(label='Output filename for pdf', value='output.pdf', interactive=True) # Акордион настроек faster whisper @@ -127,7 +136,7 @@ with gr.Blocks() as demo: systemPrompt = gr.Textbox(label='', value=DEFAULT_SYSTEM_PROMPT, interactive=True) with gr.Row(): - llmModel = gr.Dropdown(label='models', choices=LLM_MODELS, value=LLM_MODELS[0], interactive=True) + llmModel = gr.Dropdown(label='models', choices=LLM_MODELS, value=LLM_MODELS[1], interactive=True) llmTemperature = gr.Number(label='Temperature', value=0.8, interactive=True ) with gr.Column(): @@ -151,14 +160,14 @@ with gr.Blocks() as demo: # автоматический пайплайн recognizedText.change( generateByCondition, - inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("change"), saveFileCheckbox], + inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("change"), saveFileCheckbox, filename, filenamePdf], outputs=[refinedText, refinedTextMD] ) # ручной запуск по кнопке refineTextBtn.click( generateByCondition, - inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("click"), saveFileCheckbox], + inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("click"), saveFileCheckbox, filename, filenamePdf], outputs=[refinedText, refinedTextMD] ) diff --git a/config.py b/config.py index 2e18146..cf9cac1 100644 --- a/config.py +++ b/config.py @@ -11,9 +11,6 @@ DEFAULT_API_KEY=os.getenv('API_KEY') # Задаем выходную директорию OUTPUT_PATH='outputs' -# Стандартный системный промпт -OUTPUT_PATH='outputs' - DEFAULT_SYSTEM_PROMPT='''You are a diligent university student who has recorded a lecture as an audio file and later transcribed it into raw text. Your task is to rewrite this unstructured transcript into a clear, logically organized, and detailed lecture summary (lecture notes). @@ -45,5 +42,6 @@ Guidelines: Final Output: A cohesive, detailed, and well-structured lecture summary, suitable for later studying and revision. Use only russian language! USE LATEX IN DOLLAR SIGN ($)! +EXTRA BIG LENTH OF CONSPECT! ''' diff --git a/services/convertMdToPdf.py b/services/convertMdToPdf.py index 0d34953..a7135b2 100644 --- a/services/convertMdToPdf.py +++ b/services/convertMdToPdf.py @@ -13,9 +13,9 @@ class ConvertMdToPdf: text, flags=re.DOTALL ) - pdf = MarkdownPdf(toc_level=2, optimize=True) + pdf = MarkdownPdf(toc_level=0, optimize=True) pdf.add_section(Section(text)) - return text + return pdf, text def replace_math(self, match): math_content = match.group(1) or match.group(2) # $$...$$ или $...$ diff --git a/services/fasterWhisper.py b/services/fasterWhisper.py index 8ac1e48..2c7c24b 100644 --- a/services/fasterWhisper.py +++ b/services/fasterWhisper.py @@ -2,7 +2,7 @@ from faster_whisper import WhisperModel class FasterWhisper: def recognize(self, model, audioFile, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText): - model = WhisperModel(model, device='auto', compute_type='auto') # Задаем модель + model = WhisperModel(model, device='cuda', compute_type='auto') # Задаем модель segments, _ = model.transcribe( # Распознаем текст audioFile,