import gradio as gr

class LexicalTransformer:
    def __init__(self):
        self.TEXT_VALUES = {
            "antonyms": {
                "-2": {"-2": "abysmal", "-1": "horrible", "0": "terrible", "1": "bad", "2": "poor"},
                "-1": {"-2": "awful", "-1": "mediocre", "0": "fair", "1": "decent", "2": "okay"},
                "0": {"-2": "average", "-1": "fine", "0": "⭐ good ⭐", "1": "great", "2": "excellent"},
                "1": {"-2": "superb", "-1": "fantastic", "0": "amazing", "1": "wonderful", "2": "outstanding"},
                "2": {"-2": "phenomenal", "-1": "incredible", "0": "extraordinary", "1": "exceptional", "2": "perfect"}
            },
            "synonyms": {"-2": "negative2", "-1": "negative1", "0": "neutral", "1": "positive1", "2": "positive2"},
            "underplay": {"-2": "understate", "-1": "downplay", "0": "moderate", "1": "highlight", "2": "emphasize"},
            "hyperbole": {"-2": "exaggerate", "-1": "overstate", "0": "amplify", "1": "magnify", "2": "intensify"}
        }

        self.BUTTON_LABELS = {
            "top_button_left": "⬆️ (-1, 1)",
            "top_button_middle": "⬆️ up ⬆️ (0, 1)",
            "top_button_right": "⬆️ (1, 1)",
            "left_button_middle": "⬅️ (-1, 0)",
            "left_button": "⬅️ left ⬅️ (-1, 0)",
            "right_button": "➡️ right ➡️ (1, 0)",
            "right_button_middle": "➡️ (1, 0)",
            "left_button_bottom": "⬅️ (-1, -1)",
            "right_button_bottom": "➡️ (1, -1)",
            "bottom_button_left": "⬇️ (-1, -1)",
            "bottom_button_middle": "⬇️ down ⬇️ (0, -1)",
            "bottom_button_right": "⬇️ (1, -1)"
        }

        self.TEXTBOX_LABELS = {
            "top_text_left": "Top Left (-1, 1)",
            "top_text_middle": "Top Middle (0, 1)",
            "top_text_right": "Top Right (1, 1)",
            "middle_text_left": "Middle Left (-1, 0)",
            "center_text": "✏️ (0, 0)",
            "middle_text_right": "Middle Right (1, 0)",
            "bottom_text_left": "Bottom Left (-1, -1)",
            "bottom_text_middle": "Bottom Middle (0, -1)",
            "bottom_text_right": "Bottom Right (1, -1)"
        }

    def shift_grid(self, d):
        n = {"antonyms": {}}
        for i in range(-2, 3):
            n["antonyms"][str(i)] = {}
            for j in range(-2, 3):
                if d == "up": new_j = j - 1 if j - 1 >= -2 else 2
                elif d == "down": new_j = j + 1 if j + 1 <= 2 else -2
                elif d == "left": new_i = i + 1 if i + 1 <= 2 else -2
                elif d == "right": new_i = i - 1 if i - 1 >= -2 else 2
                n["antonyms"][str(i)][str(j)] = self.TEXT_VALUES["antonyms"][str(i)][str(new_j)] if d in ["up", "down"] else self.TEXT_VALUES["antonyms"][str(new_i)][str(j)]
        return n

    def handle_button_click(self, d):
        self.TEXT_VALUES = self.shift_grid(d)
        return (
            self.TEXT_VALUES["antonyms"]["0"]["0"],
            self.TEXT_VALUES["antonyms"]["-1"]["1"],
            self.TEXT_VALUES["antonyms"]["0"]["1"],
            self.TEXT_VALUES["antonyms"]["1"]["1"],
            self.TEXT_VALUES["antonyms"]["-1"]["0"],
            self.TEXT_VALUES["antonyms"]["1"]["0"],
            self.TEXT_VALUES["antonyms"]["-1"]["-1"],
            self.TEXT_VALUES["antonyms"]["0"]["-1"],
            self.TEXT_VALUES["antonyms"]["1"]["-1"]
        )
    def update_text_values(self, new_text):
        self.TEXT_VALUES["antonyms"]["0"]["0"] = new_text
        return new_text
    def create_interface(self):
        with gr.Tab(label="Lexical Transformer"):
            gr.Markdown("# NLTK Lexical Transformer\n### Enter text to transform based on lexical relations")
            with gr.Row(variant="compact"):
                gr.Button("", scale=1, min_width=50)
                top_button_left = gr.Button(self.BUTTON_LABELS["top_button_left"], scale=1, min_width=50)
                top_button_middle = gr.Button(self.BUTTON_LABELS["top_button_middle"], scale=1, min_width=50)
                top_button_right = gr.Button(self.BUTTON_LABELS["top_button_right"], scale=1, min_width=50)
                gr.Button("", scale=1, min_width=50)
            with gr.Row(variant="compact"):
                left_button_middle = gr.Button(self.BUTTON_LABELS["left_button_middle"], scale=1, min_width=50)
                top_text_left = gr.Textbox(label=self.TEXTBOX_LABELS["top_text_left"], value=self.TEXT_VALUES["antonyms"]["-1"]["1"], interactive=False, scale=1, min_width=50)
                top_text_middle = gr.Textbox(label=self.TEXTBOX_LABELS["top_text_middle"], value=self.TEXT_VALUES["antonyms"]["0"]["1"], interactive=False, scale=1, min_width=50)
                top_text_right = gr.Textbox(label=self.TEXTBOX_LABELS["top_text_right"], value=self.TEXT_VALUES["antonyms"]["1"]["1"], interactive=False, scale=1, min_width=50)
                right_button_middle = gr.Button(self.BUTTON_LABELS["right_button_middle"], scale=1, min_width=50)
            with gr.Row(variant="compact"):
                left_button = gr.Button(self.BUTTON_LABELS["left_button"], scale=1, min_width=50)
                middle_text_left = gr.Textbox(label=self.TEXTBOX_LABELS["middle_text_left"], value=self.TEXT_VALUES["antonyms"]["-1"]["0"], interactive=False, scale=1, min_width=50)
                center_text = gr.Textbox(label=self.TEXTBOX_LABELS["center_text"], placeholder="Enter text to process", value=self.TEXT_VALUES["antonyms"]["0"]["0"], interactive=True, scale=1, min_width=50)
                middle_text_right = gr.Textbox(label=self.TEXTBOX_LABELS["middle_text_right"], value=self.TEXT_VALUES["antonyms"]["1"]["0"], interactive=False, scale=1, min_width=50)
                right_button = gr.Button(self.BUTTON_LABELS["right_button"], scale=1, min_width=50)
            with gr.Row(variant="compact"):
                left_button_bottom = gr.Button(self.BUTTON_LABELS["left_button_bottom"], scale=1, min_width=50)
                bottom_text_left = gr.Textbox(label=self.TEXTBOX_LABELS["bottom_text_left"], value=self.TEXT_VALUES["antonyms"]["-1"]["-1"], interactive=False, scale=1, min_width=50)
                bottom_text_middle = gr.Textbox(label=self.TEXTBOX_LABELS["bottom_text_middle"], value=self.TEXT_VALUES["antonyms"]["0"]["-1"], interactive=False, scale=1, min_width=50)
                bottom_text_right = gr.Textbox(label=self.TEXTBOX_LABELS["bottom_text_right"], value=self.TEXT_VALUES["antonyms"]["1"]["-1"], interactive=False, scale=1, min_width=50)
                right_button_bottom = gr.Button(self.BUTTON_LABELS["right_button_bottom"], scale=1, min_width=50)
            with gr.Row(variant="compact"):
                gr.Button("", scale=1, min_width=50)
                bottom_button_left = gr.Button(self.BUTTON_LABELS["bottom_button_left"], scale=1, min_width=50)
                bottom_button_middle = gr.Button(self.BUTTON_LABELS["bottom_button_middle"], scale=1, min_width=50)
                bottom_button_right = gr.Button(self.BUTTON_LABELS["bottom_button_right"], scale=1, min_width=50)
                gr.Button("", scale=1, min_width=50)

            center_text.change(fn=self.update_text_values, inputs=center_text, outputs=center_text)

            for b, d in [
                (top_button_left, "up"), (top_button_middle, "up"), (top_button_right, "up"),
                (left_button, "left"), (left_button_middle, "left"), (left_button_bottom, "left"),
                (right_button, "right"), (right_button_middle, "right"), (right_button_bottom, "right"),
                (bottom_button_left, "down"), (bottom_button_middle, "down"), (bottom_button_right, "down")
            ]:
                b.click(fn=lambda d=d: self.handle_button_click(d), inputs=[], outputs=[
                    center_text, top_text_left, top_text_middle, top_text_right,
                    middle_text_left, middle_text_right, bottom_text_left, bottom_text_middle, bottom_text_right
                ])

with gr.Blocks() as demo:
    transformer = LexicalTransformer()
    transformer.create_interface()
#    demo.launch()

if __name__ == "__main__":
    transformer = LexicalTransformer()
    transformer.create_interface()

--------

what do you imagine the ui looks like?