From 5a571cf9b8580df381d10fc1051d0d64bc6a3ade Mon Sep 17 00:00:00 2001 From: jabberjabberjabber <75267439+jabberjabberjabber@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:11:37 -0500 Subject: [PATCH] Add files via upload Add language to translate in options --- chunkify-gui.py | 49 ++++++++++++++++++++++++++++---------------- chunkify.py | 46 +++++++++++++++++++++++++++-------------- chunkify_config.json | 3 ++- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/chunkify-gui.py b/chunkify-gui.py index 9dfb635..976dbb0 100644 --- a/chunkify-gui.py +++ b/chunkify-gui.py @@ -30,17 +30,15 @@ def __init__(self, config, task, instruction, files, selected_template=None): def run(self): try: - # Override the clear_console function in the processor to emit signals instead - def gui_console_update(text): - self.progress_signal.emit(text) - - processor = LLMProcessor(self.config, self.task) - # Monkey patch the monitor function to use our GUI update + # Create a single processor instance that we can update + self.processor = LLMProcessor(self.config, self.task) + + # Override the monitor function for the GUI def gui_monitor(): generating = False - payload = {'genkey': processor.genkey} - while not processor.generated: - result = processor._call_api("check", payload) + payload = {'genkey': self.processor.genkey} + while not self.processor.generated: + result = self.processor._call_api("check", payload) if not result: time.sleep(2) continue @@ -48,18 +46,18 @@ def gui_monitor(): self.progress_signal.emit(f"{result}") # Replace the monitor function - processor._monitor_generation = gui_monitor + self.processor._monitor_generation = gui_monitor results = [] for file_path in self.files: self.progress_signal.emit(f"Processing {file_path}...") - content, metadata = processor._get_content(file_path) + content, metadata = self.processor._get_content(file_path) if self.task == "custom": - responses = processor.process_in_chunks(self.instruction, content) + responses = self.processor.process_in_chunks(self.instruction, content) else: - responses = processor.route_task(self.task, content) + responses = self.processor.route_task(self.task, content) # Create output filename path = Path(file_path) @@ -80,7 +78,6 @@ def gui_monitor(): except Exception as e: self.progress_signal.emit(f"Error: {str(e)}") - class ChunkerGUI(QMainWindow): def __init__(self): super().__init__() @@ -282,7 +279,7 @@ def processing_finished(self, results): def show_config_dialog(self): dialog = ConfigDialog(self.config, self) if dialog.exec_() == QDialog.Accepted: - # Update config with new values + # Existing config updates... self.config.api_url = dialog.api_url_input.text() self.config.api_password = dialog.api_password_input.text() self.config.temp = dialog.temp_input.value() @@ -292,12 +289,22 @@ def show_config_dialog(self): self.config.min_p = dialog.min_p_input.value() self.selected_template = dialog.template_combo.currentText() - # Save chunk settings - + # Update translation language in config + self.config.translation_language = dialog.translation_language_input.text() # Save config to file self.save_config() + # Immediately check API with new settings + self.api_ready = False + self.check_api() + + # If we have an active processing thread, update its config and refresh instructions + if self.processing_thread and self.processing_thread.isRunning(): + self.processing_thread.config = self.config + if hasattr(self.processing_thread, 'processor'): + self.processing_thread.processor.update_config(self.config) + def load_config(self): """Load configuration from JSON file.""" try: @@ -337,7 +344,8 @@ def save_config(self): 'top_k': self.config.top_k, 'top_p': self.config.top_p, 'min_p': self.config.min_p, - 'selected_template': self.selected_template + 'selected_template': self.selected_template, + 'translation_language': self.translation_language } with open(self.config_file, 'w') as f: @@ -372,6 +380,11 @@ def initUI(self): api_layout.addWidget(QLabel("API Password:")) api_layout.addWidget(self.api_password_input) + self.translation_language_input = QLineEdit(self.config.translation_language) + #self.translation_language_input.setEchoMode(QLineEdit.translation_language) + api_layout.addWidget(QLabel("Translate to language:")) + api_layout.addWidget(self.translation_language_input) + api_group.setLayout(api_layout) # Sampler Settings diff --git a/chunkify.py b/chunkify.py index a439e3e..b6b8274 100644 --- a/chunkify.py +++ b/chunkify.py @@ -20,6 +20,7 @@ class LLMConfig: templates_directory: str api_url: str api_password: str + translation_language: str text_completion: bool = False gen_count: int = 500 #not used temp: float = 0.2 @@ -27,17 +28,7 @@ class LLMConfig: min_p: float = 0.02 top_k: int = 0 top_p: int = 1 - summary_instruction="Extract the key points, themes and actions from the text succinctly without developing any conclusions or commentary." - translate_instruction="Translate the entire document into English. Maintain linguistic flourish and authorial style as much as possible. Write the full contents without condensing the writing or modernizing the language." - #translate_instruction = """Generate a faithful English translation of this text. -#- Translate each sentence completely -#- Keep the original's pacing and paragraph structure -#- Maintain any metaphors or imagery, changing only when necessary for understanding -#- Use the same level of formality and tone as the source -#- Try to emulate the author's style -#""" - distill_instruction="Rewrite the text to be as concise as possible without losing meaning." - correct_instruction="Correct any grammar, spelling, style, or format errors in the text. Do not alter the text or otherwise change the meaning." + @classmethod @@ -64,11 +55,35 @@ def __init__(self, config, task): "model": "/api/v1/model", "generate": "/api/v1/generate", } + self._update_instructions() + self.templates_directory = config.templates_directory + self.api_url = config.api_url + self.headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {config.api_password}", + } + self.genkey = self._create_genkey() + self.templates = self._get_templates() + self.model = self._get_model() + self.max_context = self._get_max_context_length() + self.generated = False + self.system_instruction = "You are a helpful assistant." + self.task = task + self.max_chunk = int((self.max_context // 2) *.9) # give room for template + self.max_length = self.max_context // 2 + + def _update_instructions(self): + """Update instructions based on current config""" + self.summary_instruction = "Extract the key points, themes and actions from the text succinctly without developing any conclusions or commentary." + self.translate_instruction = f"Translate the entire document into {self.config.translation_language}. Maintain linguistic flourish and authorial style as much as possible. Write the full contents without condensing the writing or modernizing the language." + self.distill_instruction = "Rewrite the text to be as concise as possible without losing meaning." + self.correct_instruction = "Correct any grammar, spelling, style, or format errors in the text. Do not alter the text or otherwise change the meaning or style." + + def update_config(self, new_config): + """Update config and refresh instructions""" + self.config = new_config + self._update_instructions() self.templates_directory = config.templates_directory - self.summary_instruction = config.summary_instruction - self.translate_instruction = config.translate_instruction - self.distill_instruction = config.distill_instruction - self.correct_instruction = config.correct_instruction self.api_url = config.api_url self.headers = { "Content-Type": "application/json", @@ -441,6 +456,7 @@ def clear_console(): templates_directory=args.templates, api_url=args.api_url, api_password=args.api_password, + translation_language="English" ) task = args.task.lower() processor = LLMProcessor(config, task) diff --git a/chunkify_config.json b/chunkify_config.json index 18cd0d7..cb69833 100644 --- a/chunkify_config.json +++ b/chunkify_config.json @@ -7,5 +7,6 @@ "top_k": 0, "top_p": 1.0, "min_p": 0.02, - "selected_template": "Auto" + "selected_template": "Auto", + "translation_language": "English" } \ No newline at end of file