From 6c484fc0a3ae19b354074347b6fd4dbad79292ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Grie=C3=9Fhaber?= <griesshaber@hdm-stuttgart.de>
Date: Mon, 19 Aug 2024 13:52:31 +0200
Subject: [PATCH] add textual editor

---
 evoprompt/optimization.py | 22 ++++++++++++++++++++++
 requirements.txt          |  1 +
 2 files changed, 23 insertions(+)

diff --git a/evoprompt/optimization.py b/evoprompt/optimization.py
index f7b730f..648325e 100644
--- a/evoprompt/optimization.py
+++ b/evoprompt/optimization.py
@@ -5,6 +5,9 @@ from itertools import zip_longest
 from pathlib import Path
 from typing import Any, Optional
 
+from textual.app import App, ComposeResult
+from textual.binding import Binding
+from textual.widgets import Footer, TextArea
 from tqdm import tqdm, trange
 
 from evoprompt.models import LLMModel
@@ -18,6 +21,20 @@ logger = logging.getLogger(__name__)
 PARAPHRASE_PROMPT = """You are given an instruction that describes a task. Write a response that paraphrases the instruction. Only output the paraphrased instruction bracketed in <prompt> and </prompt>."""
 
 
+class EditText(App):
+    BINDINGS = [
+        Binding(key="q", action="quit", description="Quit the app"),
+    ]
+
+    def __init__(self, text):
+        self.text = text + "asdasdasd"
+        super().__init__()
+
+    def compose(self) -> ComposeResult:
+        self.text_area = TextArea.code_editor(self.text)
+        yield Footer(self.text_area)
+
+
 @log_calls("Paraphrasing prompts")
 def paraphrase_prompts(
     model: LLMModel,
@@ -266,6 +283,11 @@ class PromptOptimization:
 
         logger.info(f"Prompt judged as bad. Letting User change the prompt.")
 
+        editor = EditText(response)
+        editor.run()
+        response = editor.text_area.text
+        print(response)
+
 
 def load_snapshot(path: Path):
     import json
diff --git a/requirements.txt b/requirements.txt
index 4568fd6..e996d6f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -10,3 +10,4 @@ py7zr
 rouge-score
 sacrebleu
 sacremoses
+textual
-- 
GitLab