Dagfinn1962 commited on
Commit
b46b66a
1 Parent(s): 6c6c022

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +232 -38
app.py CHANGED
@@ -1,44 +1,238 @@
1
- import os
2
- import openai
3
  import gradio as gr
 
 
 
 
4
 
5
- openai.ChatCompletion.create(
6
- model="gpt-3.5-turbo0301",
7
- )
8
- # Set OpenAI API key from environment variable
9
- openai.api_key = os.environ.get("OPENAI_API_KEY")
10
 
11
- # Define GPT-3.5 Turbo model
12
- model = "gpt-3.5-turbo-0301"
13
 
14
- # Define function for generating responses
15
- def generate_response(prompt):
16
- try:
17
- response = openai.Completion.create(
18
- engine=model,
19
- prompt=prompt,
20
- max_tokens=1024,
21
- n=1,
22
- stop=None,
23
- temperature=0.7,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
- return response.choices[0].text.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  except Exception as e:
27
- return f"Error: {e}"
28
-
29
- # Define input and output interfaces
30
- input_text = gr.inputs.Textbox(label="Enter your message:")
31
- output_text = gr.outputs.Textbox(label="AI response:")
32
-
33
- # Define Gradio app
34
- gradio_app = gr.Interface(
35
- fn=generate_response,
36
- inputs=input_text,
37
- outputs=output_text,
38
- title="GPT-3.5 Turbo Chatbot",
39
- description="Enter a message and get a response from an AI chatbot powered by GPT-3.5 Turbo.",
40
- theme="compact",
41
- )
42
-
43
- # Launch Gradio app
44
- gradio_app.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import openai
3
+ import os
4
+ import time
5
+ import logging
6
 
 
 
 
 
 
7
 
 
 
8
 
9
+
10
+
11
+
12
+
13
+
14
+
15
+ logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
16
+ logging.warning("READY. App started...")
17
+
18
+
19
+ class Chat:
20
+ default_system_prompt = "A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."
21
+ system_format = "<|im_start|>system\n{}<|im_end|>\n"
22
+
23
+ def __init__(
24
+ self, system: str = None, user: str = None, assistant: str = None
25
+ ) -> None:
26
+ if system is not None:
27
+ self.set_system_prompt(system)
28
+ else:
29
+ self.reset_system_prompt()
30
+ self.user = user if user else "<|im_start|>user\n{}<|im_end|>\n"
31
+ self.assistant = (
32
+ assistant if assistant else "<|im_start|>assistant\n{}<|im_end|>\n"
33
  )
34
+ self.response_prefix = self.assistant.split("{}")[0]
35
+
36
+ def set_system_prompt(self, system_prompt):
37
+ # self.system = self.system_format.format(system_prompt)
38
+ return system_prompt
39
+
40
+ def reset_system_prompt(self):
41
+ return self.set_system_prompt(self.default_system_prompt)
42
+
43
+ def history_as_formatted_str(self, system, history) -> str:
44
+ system = self.system_format.format(system)
45
+ text = system + "".join(
46
+ [
47
+ "\n".join(
48
+ [
49
+ self.user.format(item[0]),
50
+ self.assistant.format(item[1]),
51
+ ]
52
+ )
53
+ for item in history[:-1]
54
+ ]
55
+ )
56
+ text += self.user.format(history[-1][0])
57
+ text += self.response_prefix
58
+ # stopgap solution to too long sequences
59
+ if len(text) > 4500:
60
+ # delete from the middle between <|im_start|> and <|im_end|>
61
+ # find the middle ones, then expand out
62
+ start = text.find("<|im_start|>", 139)
63
+ end = text.find("<|im_end|>", 139)
64
+ while end < len(text) and len(text) > 4500:
65
+ end = text.find("<|im_end|>", end + 1)
66
+ text = text[:start] + text[end + 1 :]
67
+ if len(text) > 4500:
68
+ # the nice way didn't work, just truncate
69
+ # deleting the beginning
70
+ text = text[-4500:]
71
+
72
+ return text
73
+
74
+ def clear_history(self, history):
75
+ return []
76
+
77
+ def turn(self, user_input: str):
78
+ self.user_turn(user_input)
79
+ return self.bot_turn()
80
+
81
+ def user_turn(self, user_input: str, history):
82
+ history.append([user_input, ""])
83
+ return user_input, history
84
+
85
+ def bot_turn(self, system, history, openai_key):
86
+ conversation = self.history_as_formatted_str(system, history)
87
+ assistant_response = call_inf_server(conversation, openai_key)
88
+ # history[-1][-1] = assistant_response
89
+ # return history
90
+ history[-1][1] = ""
91
+ for chunk in assistant_response:
92
+ try:
93
+ decoded_output = chunk["choices"][0]["delta"]["content"]
94
+ history[-1][1] += decoded_output
95
+ yield history
96
+ except KeyError:
97
+ pass
98
+
99
+
100
+ def call_inf_server(prompt, openai_key):
101
+ model_id = "gpt-3.5-turbo" # "gpt-3.5-turbo-16k",
102
+ model = llm_boiler(model_id, openai_key)
103
+ logging.warning(f'Inf via "{model_id}"" for prompt "{prompt}"')
104
+
105
+ try:
106
+ # run text generation
107
+ response = model.run(prompt, temperature=1.0)
108
+ logging.warning(f"Result of text generation: {response}")
109
+ return response
110
+
111
  except Exception as e:
112
+ # assume it is our error
113
+ # just wait and try one more time
114
+ print(e)
115
+ time.sleep(2)
116
+ response = model.run(prompt, temperature=1.0)
117
+ logging.warning(f"Result of text generation: {response}")
118
+ return response
119
+
120
+
121
+ with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
122
+ # org :
123
+ #theme=gr.themes.Glass(
124
+ #primary_hue="lime",
125
+ #secondary_hue="emerald",
126
+ #neutral_hue="zinc",
127
+
128
+
129
+
130
+ gr.Markdown(
131
+ """
132
+ <br><h1><center>Chat with gpt-3.5-turbo</center></h1>
133
+ This is a lightweight gpt-3.5-turbo conversation
134
+ completion.
135
+ """
136
+ )
137
+ conversation = Chat()
138
+ with gr.Row():
139
+ with gr.Column():
140
+ # to do: change to openaikey input for public release
141
+ openai_key = gr.Textbox(
142
+ label="OpenAI Key",
143
+ value="",
144
+ type="password",
145
+ placeholder="os.environ.get('openai_key')",
146
+ info="You have to provide your own OpenAI API key.",
147
+ )
148
+
149
+ chatbot = gr.Chatbot().style(height=400)
150
+ with gr.Row():
151
+ with gr.Column():
152
+ msg = gr.Textbox(
153
+ label="Chat Message Box",
154
+ placeholder="Chat Message Box",
155
+ show_label=False,
156
+ ).style(container=False)
157
+ with gr.Column():
158
+ with gr.Row():
159
+ submit = gr.Button("Submit")
160
+ stop = gr.Button("Stop")
161
+ clear = gr.Button("Clear")
162
+ with gr.Row():
163
+ with gr.Accordion("Advanced Options:", open=False):
164
+ with gr.Row():
165
+ with gr.Column(scale=2):
166
+ system = gr.Textbox(
167
+ label="System Prompt",
168
+ value=Chat.default_system_prompt,
169
+ show_label=False,
170
+ ).style(container=False)
171
+ with gr.Column():
172
+ with gr.Row():
173
+ change = gr.Button("Change System Prompt")
174
+ reset = gr.Button("Reset System Prompt")
175
+ with gr.Row():
176
+ gr.Markdown(
177
+ "Disclaimer: The gpt-3.5-turbo model can produce factually incorrect output, and should not be solely relied on to produce "
178
+ "factually accurate information. The gpt-3.5-turbo model was trained on various public datasets; while great efforts "
179
+ "have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
180
+ "biased, or otherwise offensive outputs.",
181
+ elem_classes=["disclaimer"],
182
+ )
183
+ with gr.Row():
184
+ gr.Markdown(
185
+ "[Privacy policy](https://gist.github.com/samhavens/c29c68cdcd420a9aa0202d0839876dac)",
186
+ elem_classes=["disclaimer"],
187
+ )
188
+
189
+ submit_event = msg.submit(
190
+ fn=conversation.user_turn,
191
+ inputs=[msg, chatbot],
192
+ outputs=[msg, chatbot],
193
+ queue=False,
194
+ ).then(
195
+ fn=conversation.bot_turn,
196
+ inputs=[system, chatbot, openai_key],
197
+ outputs=[chatbot],
198
+ queue=True,
199
+ )
200
+ submit_click_event = submit.click(
201
+ fn=conversation.user_turn,
202
+ inputs=[msg, chatbot],
203
+ outputs=[msg, chatbot],
204
+ queue=False,
205
+ ).then(
206
+ fn=conversation.bot_turn,
207
+ inputs=[system, chatbot, openai_key],
208
+ outputs=[chatbot],
209
+ queue=True,
210
+ )
211
+ stop.click(
212
+ fn=None,
213
+ inputs=None,
214
+ outputs=None,
215
+ cancels=[submit_event, submit_click_event],
216
+ queue=False,
217
+ )
218
+ clear.click(lambda: None, None, chatbot, queue=False).then(
219
+ fn=conversation.clear_history,
220
+ inputs=[chatbot],
221
+ outputs=[chatbot],
222
+ queue=False,
223
+ )
224
+ change.click(
225
+ fn=conversation.set_system_prompt,
226
+ inputs=[system],
227
+ outputs=[system],
228
+ queue=False,
229
+ )
230
+ reset.click(
231
+ fn=conversation.reset_system_prompt,
232
+ inputs=[],
233
+ outputs=[system],
234
+ queue=False,
235
+ )
236
+
237
+
238
+ demo.queue(max_size=36, concurrency_count=14).launch(debug=True)