File size: 2,353 Bytes
770f5f7
 
 
 
20d9410
770f5f7
20d9410
770f5f7
93675e1
 
1125ceb
770f5f7
 
2a9567a
3b847a5
2a9567a
a157f63
 
 
 
93675e1
3b847a5
a157f63
3b847a5
 
 
a157f63
 
3b847a5
a157f63
93675e1
a157f63
770f5f7
2a9567a
3b847a5
a157f63
 
 
 
 
8819714
 
 
a157f63
8819714
a157f63
 
 
 
f6fe9f4
3b847a5
a157f63
 
3b847a5
2a9567a
93675e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import gradio as gr
# import openai
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
key = os.getenv('OPENAI_API_KEY')
uname = os.getenv('LOGNAME')
pwd = os.getenv('PASSWORD')

client = OpenAI(api_key = key)

def clear():
    return [None, [], None]

def updatePassword(txt):
    return [txt, None]

def chat(prompt, user_window, pwd_window, past, response, gptModel):
    if user_window==uname and pwd_window==pwd:
        past.append({"role":"user", "content":prompt})
        completion = client.chat.completions.create(model=gptModel,
                                            messages=past)
        reply = completion.choices[0].message.content
        response += "\n\nYOU: " + prompt + "\nGPT: " + reply
        if len(response) > 40000:
            response += "\n\nTHIS DIALOG IS GETTING TOO LONG. PLEASE CLEAR IT."
        past.append({"role":"assistant", "content": reply})
        return [past, response, None, None]
    else:
        return [[], "User name and/or password are incorrect", prompt, pwd_window]

with gr.Blocks() as demo:
    history = gr.State([])
    password = gr.State("")
    model = gr.State("gpt-3.5-turbo")
    gr.Markdown('# GPT Chat')
    gr.Markdown('Enter user name & password then enter prompt and click submit button.  GPT 3.5 is cheaper but GPT 4o may perform better.')
    # heading = gr.Label(value="GPT Chat", scale=2, color="Crimson" )
    with gr.Row():
        user_window = gr.Textbox(label = "User Name")
        pwd_window = gr.Textbox(label = "Password")
        pwd_window.blur(updatePassword, pwd_window, [password, pwd_window])
    with gr.Row():
        clear_button = gr.Button(value="Clear Dialog")
        gpt_chooser=gr.Radio(choices=[("GPT-3.5","gpt-3.5-turbo"),("GPT-4o","gpt-4o-mini")],
                             value="gpt-3.5-turbo", label="GPT Model", interactive=True)
        submit_window = gr.Button(value="Submit Prompt/Question")
    prompt_window = gr.Textbox(label = "Prompt or Question")
    output_window = gr.Textbox(label = "Dialog")
    submit_window.click(chat, inputs=[prompt_window, user_window, password, history, output_window, model],
                         outputs=[history, output_window, prompt_window, pwd_window])
    clear_button.click(clear, inputs=[], outputs=[prompt_window, history, output_window])
    
demo.launch()