MaxSSD / OpenAI-GUI

Graphical user interface (GUI) for interacting with various OpenAI models. With this GUI, you can easily run various language and machine learning models, such as GPT-3 and DALL-E, without needing to use the command line.
MIT License
31 stars 8 forks source link

A few simple changes that you may find helpful? #2

Closed PySimpleGUI closed 1 year ago

PySimpleGUI commented 1 year ago

Hi!

I really image what you've made. Really like that you included screenshots.

I quickly made a few tweaks that you may find helpful. for this and other PySimpleGUI programs. They include:

Here's an example session with this GUI

python_JinDTbWTgr

This is the code.

import logging
import PySimpleGUI as sg
import openai
import os
import sys
import requests
import urllib.request
from PIL import Image
from io import BytesIO
from api_key import key

# @https://beta.openai.com/docs/engines/gpt-3

logger = logging.getLogger()
logging.basicConfig(filename='answers.txt', level=logging.INFO)

max_tokens_list = (256, 2000, 8000)
models = ("text-davinci-003", "text-davinci-002", "text-curie-001", "text-babbage-001", "text-ada-001")

# Defines the modules() and openAi() functions which are used to select the engine and generate a response.
def modules(engines):
    return engines if engines in models else None

def select_max_tokens(max_tokens):
    return max_tokens if max_tokens in max_tokens_list else None

def picture_size(size):
    sizing = None
    if size == "256x256":
        sizing = "256x256"
    elif size == "512x512":
        sizing = "512x512"
    elif size == "1024x1024":
        sizing = "1024x1024"
    return sizing

def openAi(prompt_in, engines, max_tokens):
    sg.popup_quick_message('Responding...')
    max_tokens = select_max_tokens(256)
    completion = openai.Completion.create(engine=modules(engines), prompt=prompt_in, temperature=0, max_tokens=select_max_tokens(max_tokens), top_p=1.0)
    result = completion.choices[0].text
    if len(result) < 150:
        print(result)
        logger.info(result)
    else:
        sg.popup_quick_message('Responding to answers.txt')
        print(result)
        with open('answers.txt', 'a+') as f:
            f.write(result)

def dalle(prompt_ins, size):
    response = openai.Image.create(
        prompt=prompt_ins,
        n=1,
        size=picture_size(size)
    )
    image_url = response['data'][0]['url']
    webUrl = urllib.request.urlopen(image_url)
    img = Image.open(webUrl)
    sg.Popup('Displaying and saving image...', keep_on_top=True)
    file_name = os.path.basename(prompt_ins)[:255] + '.png'
    img.show()
    img.save(file_name)

def make_window(theme):
    sg.theme(theme)
    max_tokens = select_max_tokens(256)
    # GUI layout.
    layout = [
        [sg.Text("OpenAIGUI", size=(63, 1), expand_x=True, expand_y=True, justification="center", font=(
            "Helvetica", 13), relief=sg.RELIEF_RIDGE, key="-TEXT HEADING-", enable_events=True)],
        [sg.TabGroup([
            [sg.Tab("OpenAi", [
                [sg.Radio("Choose model", "RADIO1", default=True, key="modules"), sg.Combo(models, default_value=models[0], key="engines", readonly=True)],
                [sg.Radio("Choose max token", "RADIO1", key="select_max_tokens"), sg.Combo(max_tokens_list, key="max_tokens")],
                [sg.Text("Enter your question or statement below:",
                         font=("Arial", 9, 'bold'))],
                [sg.Pane([sg.Column([[sg.Multiline(key="prompt", size=(77, 20), expand_x=True, expand_y=True, enter_submits=True, focus=True)]]),
                sg.Column([[sg.Multiline(size=(60, 15), font=("Arial", 9), expand_x=True, expand_y=True, write_only=True,
                              reroute_stdout=True, reroute_stderr=True, echo_stdout_stderr=True, autoscroll=True, auto_refresh=True)]])], expand_x=True, expand_y=True)],
                [sg.Button("Answer", bind_return_key=True), sg.Button('Open file'),
                 sg.Button("Clear"), sg.Button("Quit")]
            ]),
                sg.Tab("Dall-E", [
                    [sg.Text("Suggest impression:", font=("Arial", 9, 'bold'))],
                    [sg.Radio("Choose picture size", "RADIO1", key="picture_size"), sg.Combo(
                        ["256x256", "512x512", "1024x1024"], key="size")],
                    [sg.Multiline(key="promptdalle", size=(
                        77, 20), expand_x=True, expand_y=True)],
                    [sg.Button("Create image"), sg.Button(
                     "Clear"), sg.Button("Quit")]
                ]),
                sg.Tab("Theme", [
                    [sg.Text("Choose theme:")],
                    [sg.Listbox(values=sg.theme_list(), size=(20, 12),
                                key="-THEME LISTBOX-", enable_events=True)],
                    [sg.Button("Set Theme")]
                ]),
                sg.Tab("About", [
                    [sg.Text(
                        "text-davinci-003 - Upgraded davinci-002. GPT3 chatbot model.")],
                    [sg.Text(
                        "text-davinci-002 - Code review, complex intent, cause and effect, summarization for audience")],
                    [sg.Text(
                        "code-davinci-edit-001 - Edit endpoint is particularly useful for editing code.")],
                    [sg.Text(
                        "text-curie-001 - Language translation, complex classification, text sentiment, summarization")],
                    [sg.Text(
                        "text-babbage-001 - Moderate classification, semantic search classification")],
                    [sg.Text(
                        "text-ada-001 - Parsing text, simple classification, address correction, keywords")]
                ])
            ]], key="-TAB GROUP-", expand_x=True, expand_y=True),
         sg.Sizegrip()]]
    # Gui window and layout sizing.
    # icon='C:/OpenAI-GUI/icon.ico'
    window = sg.Window('OpenAI GUI', layout, resizable=True, return_keyboard_events=True, finalize=True)
    # window.bind(bind_string="<Enter>", key="Answer", propagate=True)
    # window.bind('<Configure>', "Configure")
    return window

    # Set keyboard shortcuts for the buttons [ADD KEY BINDS]
    # window.bind(bind_string="<Enter>", key="Answer", propagate=True)
    # window['Answer'].bind('<Return>', 'Answer')
    # window['Clear'].bind('<Delete>', 'Clear')
    # window['Quit'].bind('<Escape>', close_me)
    #

# GUI window that runs the main() function to interact with the user.
def main():
    window = make_window(sg.theme())
    # Event loop.
    while True:
        event, values = window.read(timeout=100)
        max_tokens = select_max_tokens(377)
        if values is not None:
            engines = values['engines'] if values['engines'] == 'Choose model' else values['engines']
        if values is not None:
            max_tokens = values['max_tokens'] if values['max_tokens'] == 'Choose max token' else values['max_tokens']
        if values is not None:
            size = values['size'] if values['size'] == 'Choose picture size' else values['size']
        if event == 'Answer':
            prompt_in = values['prompt']
            openAi(prompt_in, engines, max_tokens)
        elif event == 'Create image':
            prompt_ins = values['promptdalle']
            dalle(prompt_ins, size)
        elif event == 'Open file':
            os.startfile('answers.txt', 'open')
        elif event == 'Clear':
            window['prompt'].update('')
        elif event == "Set Theme":
            theme_chosen = values['-THEME LISTBOX-'][0]
            window.close()
            window = make_window(theme_chosen)
            sg.popup(f"Chosen Theme: {str(theme_chosen)}", keep_on_top=True)
        elif event == sg.WINDOW_CLOSED or event == 'Quit':
            break

    window.close()
    sys.exit(0)

if __name__ == '__main__':
    sg.theme('black')
    sg.theme('dark red')
    sg.theme('dark green 7')
    main()
MaxSSD commented 1 year ago

Hey hey! Very cool lines.

Thank you for taking the time to make these suggestions! I appreciate the feedback and I'm glad you found the screenshots helpful. I just pushed the updated code to the repository.

Please let me know if you have ideas for the same key bound to two buttons in one window without calling Tkinter.

PySimpleGUI commented 1 year ago

Please let me know if you have ideas for the same key bound to two buttons in one window without calling Tkinter.

If you've not opened an issue in the PySimpleGUI GitHub about this then please do. I'm not sure exactly what you mean by the same key bound to two buttons. Too many definitions for "key".

PySimpleGUI commented 1 year ago

This was another change that I made... simplified by taking out size, key, events enabled, etc. Looked like a feature that wasn't completed.

        [sg.Text("OpenAIGUI",  expand_x=True, justification="center", font=("Helvetica", 13), relief=sg.RELIEF_RIDGE)],

I also added my normal editing / version info featute by adding this standard right click menu:

    window = sg.Window('OpenAI GUI', layout, resizable=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT)

And then these 4 lines that I copy to all of my projects' event loop.

        if event == 'Edit Me':
            sg.execute_editor(__file__)
        elif event == 'Version':
            sg.popup_scrolled(__file__, sg.get_versions(), location=window.current_location(), keep_on_top=True, non_blocking=True)

This enables me to quickly make changes to programs I'm working on from time to time...

image

Right-click, choose "Edit Me" and I'm editing the program in PyCharm without having to remember where the program is located on my system.