move all functions at it's own class in handlers
This commit is contained in:
78
app.py
78
app.py
@@ -1,64 +1,18 @@
|
|||||||
import gradio as gr
|
import gradio as gr
|
||||||
|
|
||||||
# Подгрузка сервисов
|
|
||||||
from services.llm import Llm
|
|
||||||
from services.fasterWhisper import FasterWhisper
|
|
||||||
from services.convertMdToPdf import ConvertMdToPdf
|
|
||||||
|
|
||||||
# Загрузка доп. модулей
|
|
||||||
from handlers.fileHandlers import FileHandlers
|
|
||||||
|
|
||||||
# Загрузка параметров конфигурации
|
# Загрузка параметров конфигурации
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
# Объект для работы с файлами
|
# Подгрузка сервисов
|
||||||
fh = FileHandlers()
|
from services.llm import Llm
|
||||||
|
from services.convertMdToPdf import ConvertMdToPdf
|
||||||
|
|
||||||
# Функция транскрибации
|
# Загрузка доп. модулей
|
||||||
def generateByCondition(api_key, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile, filename, filenamePdf):
|
from handlers.gradioHandler import GradioHandlers
|
||||||
llm = Llm(api_key)
|
from handlers.fileHandlers import FileHandlers
|
||||||
|
from services.fasterWhisper import FasterWhisper
|
||||||
|
|
||||||
# если чекбокс включен и событие было change → обрабатываем
|
gh = GradioHandlers(gr, Llm, ConvertMdToPdf, FileHandlers, FasterWhisper)
|
||||||
if is_pipeline_enabled and trigger == "change":
|
|
||||||
result, md = llm.generate(llm_model, system_prompt, recognized_text, llm_temperature)
|
|
||||||
|
|
||||||
# Конвертируем текст с латексом в юникод
|
|
||||||
pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md)
|
|
||||||
|
|
||||||
if isSaveFile:
|
|
||||||
fh.saveFile(filenamePdf, pdf, OUTPUT_PATH)
|
|
||||||
fh.saveFile(filename, result, OUTPUT_PATH)
|
|
||||||
|
|
||||||
return result, unicodeText
|
|
||||||
|
|
||||||
# если чекбокс выключен и событие было click → обрабатываем
|
|
||||||
if not is_pipeline_enabled and trigger == "click":
|
|
||||||
result, md = llm.generate(llm_model, system_prompt, recognized_text, llm_temperature)
|
|
||||||
|
|
||||||
# Конвертируем текст с латексом в юникод
|
|
||||||
pdf, unicodeText = ConvertMdToPdf().convertLatexToText(md)
|
|
||||||
|
|
||||||
if isSaveFile:
|
|
||||||
fh.saveFile(filenamePdf, pdf, OUTPUT_PATH)
|
|
||||||
fh.saveFile(filename, result, OUTPUT_PATH)
|
|
||||||
|
|
||||||
return result, unicodeText
|
|
||||||
|
|
||||||
# если нет чекбокса и было событие change
|
|
||||||
return gr.skip(), gr.skip()
|
|
||||||
|
|
||||||
# Функция для динамического обновления кнопки в зависимости от состояния checkbox
|
|
||||||
def updateButton(isChecked):
|
|
||||||
if not isChecked:
|
|
||||||
variant = 'primary'
|
|
||||||
else:
|
|
||||||
variant = 'secondary'
|
|
||||||
|
|
||||||
return gr.update(interactive=not isChecked, variant=variant)
|
|
||||||
|
|
||||||
|
|
||||||
def updateTextbox(isChecked):
|
|
||||||
return gr.update(visible=isChecked)
|
|
||||||
|
|
||||||
###################################################
|
###################################################
|
||||||
# ____ ___.___ ___. .__ #
|
# ____ ___.___ ___. .__ #
|
||||||
@@ -158,24 +112,24 @@ with gr.Blocks() as demo:
|
|||||||
# \/ /_____/ \/ \/ \/ #
|
# \/ /_____/ \/ \/ \/ #
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
isPipelineEnabledCheckbox.change(updateButton, inputs=[isPipelineEnabledCheckbox], outputs=refineTextBtn)
|
isPipelineEnabledCheckbox.change(gh.updateButton, inputs=[isPipelineEnabledCheckbox], outputs=refineTextBtn)
|
||||||
saveFileCheckbox.change(updateTextbox, inputs=saveFileCheckbox, outputs=filename)
|
saveFileCheckbox.change(gh.updateTextbox, inputs=saveFileCheckbox, outputs=filename)
|
||||||
saveFileCheckbox.change(updateTextbox, inputs=saveFileCheckbox, outputs=filenamePdf)
|
saveFileCheckbox.change(gh.updateTextbox, inputs=saveFileCheckbox, outputs=filenamePdf)
|
||||||
|
|
||||||
recognizeBtn.click(FasterWhisper().recognize, outputs=[recognizedText], inputs=[fastWhisperModel, device, compute_type, audioFile, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText])
|
recognizeBtn.click(gh.FasterWhisper.recognize, outputs=[recognizedText], inputs=[fastWhisperModel, device, compute_type, audioFile, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText])
|
||||||
|
|
||||||
# Если пайплайн включен то тогда делаем автоматически
|
# Если пайплайн включен то тогда делаем автоматически
|
||||||
# автоматический пайплайн
|
# автоматический пайплайн
|
||||||
recognizedText.change(
|
recognizedText.change(
|
||||||
generateByCondition,
|
gh.generateByCondition,
|
||||||
inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("change"), saveFileCheckbox, filename, filenamePdf],
|
inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("change"), saveFileCheckbox, filename, filenamePdf, gr.State(OUTPUT_PATH)],
|
||||||
outputs=[refinedText, refinedTextMD]
|
outputs=[refinedText, refinedTextMD]
|
||||||
)
|
)
|
||||||
|
|
||||||
# ручной запуск по кнопке
|
# ручной запуск по кнопке
|
||||||
refineTextBtn.click(
|
refineTextBtn.click(
|
||||||
generateByCondition,
|
gh.generateByCondition,
|
||||||
inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("click"), saveFileCheckbox, filename, filenamePdf],
|
inputs=[apiKey, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("click"), saveFileCheckbox, filename, filenamePdf, gr.State(OUTPUT_PATH)],
|
||||||
outputs=[refinedText, refinedTextMD]
|
outputs=[refinedText, refinedTextMD]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ LLM_MODELS = ['openai/gpt-oss-120b', 'Qwen/Qwen3-235B-A22B-Thinking-2507', 'deep
|
|||||||
DEVICES = ['cpu', 'cuda']
|
DEVICES = ['cpu', 'cuda']
|
||||||
COMPUTE_TYPE = ['auto', 'int8', 'float16', 'float32']
|
COMPUTE_TYPE = ['auto', 'int8', 'float16', 'float32']
|
||||||
|
|
||||||
|
|
||||||
# Стандартный API ключ
|
# Стандартный API ключ
|
||||||
DEFAULT_API_KEY=os.getenv('API_KEY')
|
DEFAULT_API_KEY=os.getenv('API_KEY')
|
||||||
# Задаем выходную директорию
|
# Задаем выходную директорию
|
||||||
@@ -46,5 +45,6 @@ Final Output: A cohesive, detailed, and well-structured lecture summary, suitabl
|
|||||||
Use only russian language!
|
Use only russian language!
|
||||||
USE LATEX IN DOLLAR SIGN ($)!
|
USE LATEX IN DOLLAR SIGN ($)!
|
||||||
EXTRA BIG LENTH OF CONSPECT!
|
EXTRA BIG LENTH OF CONSPECT!
|
||||||
|
MAKE AS LONG AS POSIBLE AND AS BE GOOD!
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|||||||
55
handlers/gradioHandler.py
Normal file
55
handlers/gradioHandler.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
class GradioHandlers:
|
||||||
|
def __init__(self, gr, Llm, ConvertMdToPdf, FileHandlers, FasterWhisper):
|
||||||
|
# Объект для работы с файлами
|
||||||
|
self.fh = FileHandlers()
|
||||||
|
self.ConvertMdToPdf = ConvertMdToPdf()
|
||||||
|
self.FasterWhisper = FasterWhisper()
|
||||||
|
self.Llm = Llm
|
||||||
|
self.gr = gr
|
||||||
|
|
||||||
|
# Функция улучшения текста
|
||||||
|
def generateByCondition(self, api_key, llm_model, system_prompt, recognized_text, llm_temperature, is_pipeline_enabled, trigger, isSaveFile, filename, filenamePdf, output_path):
|
||||||
|
llm = self.Llm(api_key)
|
||||||
|
|
||||||
|
# если чекбокс включен и событие было change → обрабатываем
|
||||||
|
if is_pipeline_enabled and trigger == "change":
|
||||||
|
result, md = llm.generate(llm_model, system_prompt, recognized_text, llm_temperature)
|
||||||
|
|
||||||
|
# Конвертируем текст с латексом в юникод
|
||||||
|
pdf, unicodeText = self.ConvertMdToPdf.convertLatexToText(md)
|
||||||
|
|
||||||
|
if isSaveFile:
|
||||||
|
self.fh.saveFile(filenamePdf, pdf, output_path)
|
||||||
|
self.fh.saveFile(filename, result, output_path)
|
||||||
|
|
||||||
|
return result, unicodeText
|
||||||
|
|
||||||
|
# если чекбокс выключен и событие было click → обрабатываем
|
||||||
|
if not is_pipeline_enabled and trigger == "click":
|
||||||
|
result, md = llm.generate(llm_model, system_prompt, recognized_text, llm_temperature)
|
||||||
|
|
||||||
|
# Конвертируем текст с латексом в юникод
|
||||||
|
pdf, unicodeText = self.ConvertMdToPdf.convertLatexToText(md)
|
||||||
|
|
||||||
|
if isSaveFile:
|
||||||
|
self.fh.saveFile(filenamePdf, pdf, output_path)
|
||||||
|
self.fh.saveFile(filename, result, output_path)
|
||||||
|
|
||||||
|
return result, unicodeText
|
||||||
|
|
||||||
|
# если нет чекбокса и было событие change
|
||||||
|
return self.gr.skip(), self.gr.skip()
|
||||||
|
|
||||||
|
# Функция для динамического обновления кнопки в зависимости от состояния checkbox
|
||||||
|
def updateButton(self, isChecked):
|
||||||
|
if not isChecked:
|
||||||
|
variant = 'primary'
|
||||||
|
else:
|
||||||
|
variant = 'secondary'
|
||||||
|
|
||||||
|
return self.gr.update(interactive=not isChecked, variant=variant)
|
||||||
|
|
||||||
|
|
||||||
|
def updateTextbox(self, isChecked):
|
||||||
|
return self.gr.update(visible=isChecked)
|
||||||
|
|
||||||
Reference in New Issue
Block a user