Dagfinn1962 commited on
Commit
38880eb
1 Parent(s): c005425

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -66
app.py CHANGED
@@ -1,10 +1,9 @@
1
- import openai
2
  import time
3
  import logging
4
  import gradio as gr
5
- import os
6
  from src.llm_boilers import llm_boiler
7
- import configparser
8
 
9
  logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
10
  logging.warning("READY. App started...")
@@ -12,24 +11,29 @@ logging.warning("READY. App started...")
12
 
13
  class Chat:
14
  default_system_prompt = "A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."
15
- system_format = "system\n{}\n"
16
 
17
- def __init__(self, system: str = None, user: str = None, assistant: str = None):
 
 
18
  if system is not None:
19
  self.set_system_prompt(system)
20
  else:
21
  self.reset_system_prompt()
22
- self.user = user if user else "user\n{}\n"
23
- self.assistant = assistant if assistant else "assistant\n{}\n"
 
 
24
  self.response_prefix = self.assistant.split("{}")[0]
25
 
26
  def set_system_prompt(self, system_prompt):
 
27
  return system_prompt
28
 
29
  def reset_system_prompt(self):
30
  return self.set_system_prompt(self.default_system_prompt)
31
 
32
- def history_as_formatted_str(self, system, history):
33
  system = self.system_format.format(system)
34
  text = system + "".join(
35
  [
@@ -44,18 +48,27 @@ class Chat:
44
  )
45
  text += self.user.format(history[-1][0])
46
  text += self.response_prefix
47
-
48
- # Truncate text if it exceeds the limit
49
- if len(text) > 4096:
50
- text = text[-4096:]
 
 
 
 
 
 
 
 
 
51
 
52
  return text
53
 
54
  def clear_history(self, history):
55
  return []
56
 
57
- def turn(self, user_input: str, history):
58
- self.user_turn(user_input, history)
59
  return self.bot_turn()
60
 
61
  def user_turn(self, user_input: str, history):
@@ -65,6 +78,8 @@ class Chat:
65
  def bot_turn(self, system, history, openai_key):
66
  conversation = self.history_as_formatted_str(system, history)
67
  assistant_response = call_inf_server(conversation, openai_key)
 
 
68
  history[-1][1] = ""
69
  for chunk in assistant_response:
70
  try:
@@ -76,18 +91,19 @@ class Chat:
76
 
77
 
78
  def call_inf_server(prompt, openai_key):
79
- model_id = "gpt-3.5-turbo"
80
  model = llm_boiler(model_id, openai_key)
81
  logging.warning(f'Inf via "{model_id}"" for prompt "{prompt}"')
82
 
83
  try:
84
- # Run text generation
85
  response = model.run(prompt, temperature=1.0)
86
  logging.warning(f"Result of text generation: {response}")
87
  return response
88
 
89
  except Exception as e:
90
- # Wait and try one more time
 
91
  print(e)
92
  time.sleep(2)
93
  response = model.run(prompt, temperature=1.0)
@@ -95,21 +111,24 @@ def call_inf_server(prompt, openai_key):
95
  return response
96
 
97
 
98
-
99
-
100
- # Get the OpenAI key from the environment variable
101
- openai_key = os.getenv("OPENAI_API_KEY")
102
-
103
-
104
-
105
- with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
106
  gr.Markdown(
107
- """
108
- <br><h1><center>Chat with gpt-3.5-turbo</center></h1>
109
- This is a lightweight gpt-3.5-turbo conversation completion.
110
- """
111
  )
112
  conversation = Chat()
 
 
 
 
 
 
 
 
 
 
 
113
  chatbot = gr.Chatbot().style(height=400)
114
  with gr.Row():
115
  with gr.Column():
@@ -138,48 +157,42 @@ with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
138
  reset = gr.Button("Reset System Prompt")
139
  with gr.Row():
140
  gr.Markdown(
141
- "Disclaimer: The gpt-3.5-turbo model can produce factually incorrect output and should not be solely relied on to produce "
142
  "factually accurate information. The gpt-3.5-turbo model was trained on various public datasets; while great efforts "
143
  "have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
144
  "biased, or otherwise offensive outputs.",
145
  elem_classes=["disclaimer"],
146
  )
147
- with gr.Row():
148
- gr.Markdown(
149
- "[Privacy policy](https://gist.github.com/samhavens/c29c68cdcd420a9aa0202d0839876dac)",
150
- elem_classes=["disclaimer"],
151
- )submit_event = msg.submit(
152
- fn=conversation.user_turn,
153
- inputs=[msg],
154
- outputs=[msg, chatbot],
155
- queue=False,
156
- ).then(
157
- fn=conversation.bot_turn,
158
- inputs=[system, chatbot, openai_key],
159
- outputs=[chatbot],
160
- queue=True, # Change `queue=True` to `keep_in_queue=True`
161
- )
162
-
163
- submit_click_event = submit.click(
164
- fn=conversation.user_turn,
165
- inputs=[msg],
166
- outputs=[msg, chatbot],
167
- queue=False,
168
- ).then(
169
- fn=conversation.bot_turn,
170
- inputs=[system, chatbot, openai_key],
171
- outputs=[chatbot],
172
- queue=True, # Change `queue=True` to `keep_in_queue=True`
173
- )
174
-
175
- stop.click(
176
- fn=None,
177
- inputs=None,
178
- outputs=None,
179
- cancels=[submit_event, submit_click_event],
180
- queue=False,
181
- )
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  clear.click(lambda: None, None, chatbot, queue=False).then(
184
  fn=conversation.clear_history,
185
  inputs=[chatbot],
@@ -200,4 +213,4 @@ stop.click(
200
  )
201
 
202
 
203
- demo.queue(max_size=36, concurrency_count=14).launch(debug=True)
 
 
1
  import time
2
  import logging
3
  import gradio as gr
4
+
5
  from src.llm_boilers import llm_boiler
6
+
7
 
8
  logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
9
  logging.warning("READY. App started...")
 
11
 
12
  class Chat:
13
  default_system_prompt = "A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."
14
+ system_format = "<|im_start|>system\n{}<|im_end|>\n"
15
 
16
+ def __init__(
17
+ self, system: str = None, user: str = None, assistant: str = None
18
+ ) -> None:
19
  if system is not None:
20
  self.set_system_prompt(system)
21
  else:
22
  self.reset_system_prompt()
23
+ self.user = user if user else "<|im_start|>user\n{}<|im_end|>\n"
24
+ self.assistant = (
25
+ assistant if assistant else "<|im_start|>assistant\n{}<|im_end|>\n"
26
+ )
27
  self.response_prefix = self.assistant.split("{}")[0]
28
 
29
  def set_system_prompt(self, system_prompt):
30
+ # self.system = self.system_format.format(system_prompt)
31
  return system_prompt
32
 
33
  def reset_system_prompt(self):
34
  return self.set_system_prompt(self.default_system_prompt)
35
 
36
+ def history_as_formatted_str(self, system, history) -> str:
37
  system = self.system_format.format(system)
38
  text = system + "".join(
39
  [
 
48
  )
49
  text += self.user.format(history[-1][0])
50
  text += self.response_prefix
51
+ # stopgap solution to too long sequences
52
+ if len(text) > 4500:
53
+ # delete from the middle between <|im_start|> and <|im_end|>
54
+ # find the middle ones, then expand out
55
+ start = text.find("<|im_start|>", 139)
56
+ end = text.find("<|im_end|>", 139)
57
+ while end < len(text) and len(text) > 4500:
58
+ end = text.find("<|im_end|>", end + 1)
59
+ text = text[:start] + text[end + 1 :]
60
+ if len(text) > 4500:
61
+ # the nice way didn't work, just truncate
62
+ # deleting the beginning
63
+ text = text[-4500:]
64
 
65
  return text
66
 
67
  def clear_history(self, history):
68
  return []
69
 
70
+ def turn(self, user_input: str):
71
+ self.user_turn(user_input)
72
  return self.bot_turn()
73
 
74
  def user_turn(self, user_input: str, history):
 
78
  def bot_turn(self, system, history, openai_key):
79
  conversation = self.history_as_formatted_str(system, history)
80
  assistant_response = call_inf_server(conversation, openai_key)
81
+ # history[-1][-1] = assistant_response
82
+ # return history
83
  history[-1][1] = ""
84
  for chunk in assistant_response:
85
  try:
 
91
 
92
 
93
  def call_inf_server(prompt, openai_key):
94
+ model_id = "eolionross/gpt-3.5-turbo-demo" # "gpt-3.5-turbo-16k",
95
  model = llm_boiler(model_id, openai_key)
96
  logging.warning(f'Inf via "{model_id}"" for prompt "{prompt}"')
97
 
98
  try:
99
+ # run text generation
100
  response = model.run(prompt, temperature=1.0)
101
  logging.warning(f"Result of text generation: {response}")
102
  return response
103
 
104
  except Exception as e:
105
+ # assume it is our error
106
+ # just wait and try one more time
107
  print(e)
108
  time.sleep(2)
109
  response = model.run(prompt, temperature=1.0)
 
111
  return response
112
 
113
 
114
+ with gr.Blocks(theme='ParityError/Anime') as demo:
 
 
 
 
 
 
 
115
  gr.Markdown(
116
+ """<h3><center>Chat with gpt-3.5-turbo</center></h3>
117
+
118
+ """
 
119
  )
120
  conversation = Chat()
121
+ with gr.Row():
122
+ with gr.Column():
123
+ # to do: change to openaikey input for public release
124
+ openai_key = gr.Textbox(
125
+ label="Open Ai key",
126
+ value="",
127
+ type="password",
128
+ placeholder="Enter your Open ai API KEY here ",
129
+ info="",
130
+
131
+ )
132
  chatbot = gr.Chatbot().style(height=400)
133
  with gr.Row():
134
  with gr.Column():
 
157
  reset = gr.Button("Reset System Prompt")
158
  with gr.Row():
159
  gr.Markdown(
160
+ "Disclaimer: The gpt-3.5-turbo model can produce factually incorrect output, and should not be solely relied on to produce "
161
  "factually accurate information. The gpt-3.5-turbo model was trained on various public datasets; while great efforts "
162
  "have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
163
  "biased, or otherwise offensive outputs.",
164
  elem_classes=["disclaimer"],
165
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
+ submit_event = msg.submit(
168
+ fn=conversation.user_turn,
169
+ inputs=[msg, chatbot],
170
+ outputs=[msg, chatbot],
171
+ queue=False,
172
+ ).then(
173
+ fn=conversation.bot_turn,
174
+ inputs=[system, chatbot, openai_key],
175
+ outputs=[chatbot],
176
+ queue=True,
177
+ )
178
+ submit_click_event = submit.click(
179
+ fn=conversation.user_turn,
180
+ inputs=[msg, chatbot],
181
+ outputs=[msg, chatbot],
182
+ queue=False,
183
+ ).then(
184
+ fn=conversation.bot_turn,
185
+ inputs=[system, chatbot, openai_key],
186
+ outputs=[chatbot],
187
+ queue=True,
188
+ )
189
+ stop.click(
190
+ fn=None,
191
+ inputs=None,
192
+ outputs=None,
193
+ cancels=[submit_event, submit_click_event],
194
+ queue=False,
195
+ )
196
  clear.click(lambda: None, None, chatbot, queue=False).then(
197
  fn=conversation.clear_history,
198
  inputs=[chatbot],
 
213
  )
214
 
215
 
216
+ demo.queue(max_size=36, concurrency_count=14).launch(debug=True)