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.
This commit is contained in:
swrneko
2025-09-09 18:24:18 +03:00
parent 240c39cab2
commit 83e19c471a
5 changed files with 21 additions and 38 deletions

24
\
View File

@@ -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 # В случае ошибки оставляем как есть

25
app.py
View File

@@ -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]
)

View File

@@ -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!
'''

View File

@@ -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) # $$...$$ или $...$

View File

@@ -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,