move file logic to it's own class in handlers dir
This commit is contained in:
31
app.py
31
app.py
@@ -1,14 +1,19 @@
|
|||||||
import gradio as gr
|
import gradio as gr
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
# Подгрузка сервисов
|
# Подгрузка сервисов
|
||||||
from services.llm import Llm
|
from services.llm import Llm
|
||||||
from services.fasterWhisper import FasterWhisper
|
from services.fasterWhisper import FasterWhisper
|
||||||
from services.convertMdToPdf import ConvertMdToPdf
|
from services.convertMdToPdf import ConvertMdToPdf
|
||||||
|
|
||||||
|
# Загрузка доп. модулей
|
||||||
|
from handlers.fileHandlers import FileHandlers
|
||||||
|
|
||||||
# Загрузка параметров конфигурации
|
# Загрузка параметров конфигурации
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
|
# Объект для работы с файлами
|
||||||
|
fh = FileHandlers()
|
||||||
|
|
||||||
# Функция транскрибации
|
# Функция транскрибации
|
||||||
def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile, filename, filenamePdf):
|
def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile, filename, filenamePdf):
|
||||||
llm = Llm(api_key)
|
llm = Llm(api_key)
|
||||||
@@ -21,8 +26,8 @@ def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_
|
|||||||
pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md)
|
pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md)
|
||||||
|
|
||||||
if isSaveFile:
|
if isSaveFile:
|
||||||
savePdf(filenamePdf, pdf)
|
fh.saveFile(filenamePdf, pdf, OUTPUT_PATH)
|
||||||
saveFile(filename, result)
|
fh.saveFile(filename, result, OUTPUT_PATH)
|
||||||
|
|
||||||
return result, unicodeText
|
return result, unicodeText
|
||||||
|
|
||||||
@@ -34,30 +39,14 @@ def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_
|
|||||||
pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md)
|
pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md)
|
||||||
|
|
||||||
if isSaveFile:
|
if isSaveFile:
|
||||||
savePdf(filenamePdf, pdf)
|
fh.saveFile(filenamePdf, pdf, OUTPUT_PATH)
|
||||||
saveFile(filename, result)
|
fh.saveFile(filename, result, OUTPUT_PATH)
|
||||||
|
|
||||||
return result, unicodeText
|
return result, unicodeText
|
||||||
|
|
||||||
# если нет чекбокса и было событие change
|
# если нет чекбокса и было событие change
|
||||||
return gr.skip(), gr.skip()
|
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)
|
|
||||||
filePath = directory / filename
|
|
||||||
filePath.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
filePath.write_text(text, encoding='utf-8')
|
|
||||||
|
|
||||||
# ConvertMdToPdf().convert(text)
|
|
||||||
|
|
||||||
# Функция для динамического обновления кнопки в зависимости от состояния checkbox
|
# Функция для динамического обновления кнопки в зависимости от состояния checkbox
|
||||||
def updateButton(isChecked):
|
def updateButton(isChecked):
|
||||||
if not isChecked:
|
if not isChecked:
|
||||||
|
|||||||
0
handlers/__init__.py
Normal file
0
handlers/__init__.py
Normal file
23
handlers/fileHandlers.py
Normal file
23
handlers/fileHandlers.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from markdown_pdf import MarkdownPdf # Для аннотации типов
|
||||||
|
|
||||||
|
class FileHandlers:
|
||||||
|
# Функция сохранения файла
|
||||||
|
def saveFile(self, filename, content, output_path):
|
||||||
|
'''
|
||||||
|
Сохраняет текст или pdf из markdown_pdf в файл с указанным названием и директорией.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
:param filename: название файла;
|
||||||
|
:param content: содержание файла;
|
||||||
|
:param output_path: выходная диретория файла.
|
||||||
|
'''
|
||||||
|
# Создание объекта директории
|
||||||
|
directory = Path(output_path)
|
||||||
|
filePath = directory / filename # Добавление пути директории
|
||||||
|
filePath.parent.mkdir(parents=True, exist_ok=True) # Создание директории если не существует
|
||||||
|
# Сохранение для разных типов
|
||||||
|
if type(content) == MarkdownPdf:
|
||||||
|
content.save(filePath)
|
||||||
|
elif type(content) == str:
|
||||||
|
filePath.write_text(content, encoding='utf-8')
|
||||||
Reference in New Issue
Block a user