baruga commited on
Commit
2eb2277
1 Parent(s): d079567

Add app.py and requirements

Browse files
Files changed (2) hide show
  1. app.py +80 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+
4
+ openai.api_key = "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"
5
+
6
+ default_system_message = {"role": "system", "content": "You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user."}
7
+
8
+ def get_completion(model, user_message, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty):
9
+ new_history_row = {"role": "user", "content": user_message}
10
+ updated_message_history = message_history + [new_history_row]
11
+ completion = openai.ChatCompletion.create(
12
+ model=model,
13
+ messages=updated_message_history,
14
+ temperature=temperature,
15
+ max_tokens=maximum_length,
16
+ top_p=top_p,
17
+ frequency_penalty=frequency_penalty,
18
+ presence_penalty=presence_penalty,
19
+ )
20
+ assistant_message = completion["choices"][0]["message"]["content"]
21
+ new_history_row = {"role": "assistant", "content": assistant_message}
22
+ updated_message_history = updated_message_history + [new_history_row]
23
+ updated_chatlog_history = chatlog_history + [(user_message, assistant_message)]
24
+ token_count = completion["usage"]["total_tokens"]
25
+ return "", updated_message_history, updated_chatlog_history, updated_chatlog_history, token_count
26
+
27
+ def retry_completion(model, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty):
28
+ # get latest user message
29
+ user_message = chatlog_history[-1][0]
30
+ # delete latest entries from chatlog history
31
+ updated_chatlog_history = chatlog_history[:-1]
32
+ # delete latest assistant message from message_history
33
+ updated_message_history = message_history[:-1]
34
+ completion = openai.ChatCompletion.create(
35
+ model=model,
36
+ messages=updated_message_history,
37
+ temperature=temperature,
38
+ max_tokens=maximum_length,
39
+ top_p=top_p,
40
+ frequency_penalty=frequency_penalty,
41
+ presence_penalty=presence_penalty,
42
+ )
43
+ assistant_message = completion["choices"][0]["message"]["content"]
44
+ new_history_row = {"role": "assistant", "content": assistant_message}
45
+ updated_message_history = updated_message_history + [new_history_row]
46
+ updated_chatlog_history = updated_chatlog_history + [(user_message, assistant_message)]
47
+ token_count = completion["usage"]["total_tokens"]
48
+ return "", updated_message_history, updated_chatlog_history, updated_chatlog_history, token_count
49
+
50
+ def reset_chat():
51
+ return "", [default_system_message], [], [], 0
52
+
53
+ theme = gr.themes.Monochrome()
54
+ with gr.Blocks(theme=theme) as app:
55
+ message_history = gr.State([default_system_message])
56
+ chatlog_history = gr.State([])
57
+ with gr.Row():
58
+ with gr.Column(scale=4):
59
+ chatbot = gr.Chatbot(label="Chat").style(height=600)
60
+ with gr.Column(scale=1):
61
+ # model = gr.Textbox(lines=3, value="You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user.", interactive=True, label="System")
62
+ model = gr.Dropdown(choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"], value="gpt-3.5-turbo", interactive=True, label="Model")
63
+ temperature = gr.Slider(minimum=0, maximum=1, step=0.05, value=0.5, interactive=True, label="Temperature")
64
+ maximum_length = gr.Slider(minimum=0, maximum=2048, step=64, value=256, interactive=True, label="Maximum length")
65
+ top_p = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, interactive=True, label="Top P")
66
+ frequency_penalty = gr.Slider(minimum=0, maximum=2, step=0.01, value=0, interactive=True, label="Frequency penalty")
67
+ presence_penalty = gr.Slider(minimum=0, maximum=2, step=0.01, value=0, interactive=True, label="Presence penalty")
68
+ token_count = gr.Number(interactive=False, label="Token Count")
69
+ with gr.Row():
70
+ user_message = gr.Textbox(label="Message")
71
+ with gr.Row():
72
+ reset_button = gr.Button("Reset Chat")
73
+ retry_button = gr.Button("Retry")
74
+
75
+ user_message.submit(get_completion, inputs=[model, user_message, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
76
+ retry_button.click(retry_completion, inputs=[model, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
77
+ reset_button.click(reset_chat, inputs=[], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
78
+
79
+ app.launch()
80
+ # app.launch(auth=("admin", "C%nc6mrn8*BCwQF9HhH4CX35d7Q**eQY"))
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ openai