Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Add language to translate in options
  • Loading branch information
jabberjabberjabber authored Dec 13, 2024
1 parent 213cbfd commit 5a571cf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 34 deletions.
49 changes: 31 additions & 18 deletions chunkify-gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,34 @@ 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
time.sleep(1)
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)
Expand All @@ -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__()
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
46 changes: 31 additions & 15 deletions chunkify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,15 @@ 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
rep_pen: float = 1
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
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion chunkify_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"top_k": 0,
"top_p": 1.0,
"min_p": 0.02,
"selected_template": "Auto"
"selected_template": "Auto",
"translation_language": "English"
}

0 comments on commit 5a571cf

Please sign in to comment.