Dagfinn1962 commited on
Commit
418e706
1 Parent(s): 262db44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -66
app.py CHANGED
@@ -6,46 +6,30 @@ import os
6
  from src.llm_boilers import llm_boiler
7
  import configparser
8
 
9
- # Read the configuration file
10
- config = configparser.ConfigParser()
11
- config.read('config.ini')
12
-
13
- # Get the OpenAI key from the configuration file
14
- openai_key = config.get('Credentials', 'openai_key')
15
-
16
- # Use openai_key in your code
17
-
18
-
19
-
20
  logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
21
  logging.warning("READY. App started...")
22
 
23
 
24
  class Chat:
25
  default_system_prompt = "A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."
26
- system_format = "<|im_start|>system\n{}<|im_end|>\n"
27
 
28
- def __init__(
29
- self, system: str = None, user: str = None, assistant: str = None
30
- ) -> None:
31
  if system is not None:
32
  self.set_system_prompt(system)
33
  else:
34
  self.reset_system_prompt()
35
- self.user = user if user else "<|im_start|>user\n{}<|im_end|>\n"
36
- self.assistant = (
37
- assistant if assistant else "<|im_start|>assistant\n{}<|im_end|>\n"
38
- )
39
  self.response_prefix = self.assistant.split("{}")[0]
40
 
41
  def set_system_prompt(self, system_prompt):
42
- # self.system = self.system_format.format(system_prompt)
43
  return system_prompt
44
 
45
  def reset_system_prompt(self):
46
  return self.set_system_prompt(self.default_system_prompt)
47
 
48
- def history_as_formatted_str(self, system, history) -> str:
49
  system = self.system_format.format(system)
50
  text = system + "".join(
51
  [
@@ -60,27 +44,18 @@ class Chat:
60
  )
61
  text += self.user.format(history[-1][0])
62
  text += self.response_prefix
63
- # stopgap solution to too long sequences
64
- if len(text) > 4500:
65
- # delete from the middle between <|im_start|> and <|im_end|>
66
- # find the middle ones, then expand out
67
- start = text.find("<|im_start|>", 139)
68
- end = text.find("<|im_end|>", 139)
69
- while end < len(text) and len(text) > 4500:
70
- end = text.find("<|im_end|>", end + 1)
71
- text = text[:start] + text[end + 1 :]
72
- if len(text) > 4500:
73
- # the nice way didn't work, just truncate
74
- # deleting the beginning
75
- text = text[-4500:]
76
 
77
  return text
78
 
79
  def clear_history(self, history):
80
  return []
81
 
82
- def turn(self, user_input: str):
83
- self.user_turn(user_input)
84
  return self.bot_turn()
85
 
86
  def user_turn(self, user_input: str, history):
@@ -90,8 +65,6 @@ class Chat:
90
  def bot_turn(self, system, history, openai_key):
91
  conversation = self.history_as_formatted_str(system, history)
92
  assistant_response = call_inf_server(conversation, openai_key)
93
- # history[-1][-1] = assistant_response
94
- # return history
95
  history[-1][1] = ""
96
  for chunk in assistant_response:
97
  try:
@@ -103,19 +76,18 @@ class Chat:
103
 
104
 
105
  def call_inf_server(prompt, openai_key):
106
- model_id = "gpt-3.5-turbo" # "gpt-3.5-turbo-16k",
107
  model = llm_boiler(model_id, openai_key)
108
  logging.warning(f'Inf via "{model_id}"" for prompt "{prompt}"')
109
 
110
  try:
111
- # run text generation
112
  response = model.run(prompt, temperature=1.0)
113
  logging.warning(f"Result of text generation: {response}")
114
  return response
115
 
116
  except Exception as e:
117
- # assume it is our error
118
- # just wait and try one more time
119
  print(e)
120
  time.sleep(2)
121
  response = model.run(prompt, temperature=1.0)
@@ -123,34 +95,22 @@ def call_inf_server(prompt, openai_key):
123
  return response
124
 
125
 
126
- with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
127
- # org :
128
- #theme=gr.themes.Glass(
129
- #primary_hue="lime",
130
- #secondary_hue="emerald",
131
- #neutral_hue="zinc",
132
-
133
 
134
 
 
135
  gr.Markdown(
136
  """
137
  <br><h1><center>Chat with gpt-3.5-turbo</center></h1>
138
- This is a lightweight gpt-3.5-turbo conversation
139
- completion.
140
- """
141
- )
142
  conversation = Chat()
143
- with gr.Row():
144
- with gr.Column():
145
- # to do: change to openaikey input for public release
146
- openai_key = gr.Textbox(
147
- label="OpenAI Key",
148
- value="",
149
- type="password",
150
- placeholder="os.environ.get('openai_key')",
151
- info="You have to provide your own OpenAI API key.",
152
- )
153
-
154
  chatbot = gr.Chatbot().style(height=400)
155
  with gr.Row():
156
  with gr.Column():
@@ -179,7 +139,7 @@ with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
179
  reset = gr.Button("Reset System Prompt")
180
  with gr.Row():
181
  gr.Markdown(
182
- "Disclaimer: The gpt-3.5-turbo model can produce factually incorrect output, and should not be solely relied on to produce "
183
  "factually accurate information. The gpt-3.5-turbo model was trained on various public datasets; while great efforts "
184
  "have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
185
  "biased, or otherwise offensive outputs.",
@@ -240,4 +200,4 @@ with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
240
  )
241
 
242
 
243
- demo.queue(max_size=36, concurrency_count=14).launch(debug=True)
 
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...")
11
 
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
  )
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
  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
 
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
  return response
96
 
97
 
98
+ # Read the configuration file
99
+ config = configparser.ConfigParser()
100
+ config.read('config.ini')
101
+
102
+ # Get the OpenAI key from the configuration file
103
+ openai_key = config.get('Credentials', 'openai_key')
 
104
 
105
 
106
+ with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
107
  gr.Markdown(
108
  """
109
  <br><h1><center>Chat with gpt-3.5-turbo</center></h1>
110
+ This is a lightweight gpt-3.5-turbo conversation completion.
111
+ """
112
+ )
 
113
  conversation = Chat()
 
 
 
 
 
 
 
 
 
 
 
114
  chatbot = gr.Chatbot().style(height=400)
115
  with gr.Row():
116
  with gr.Column():
 
139
  reset = gr.Button("Reset System Prompt")
140
  with gr.Row():
141
  gr.Markdown(
142
+ "Disclaimer: The gpt-3.5-turbo model can produce factually incorrect output and should not be solely relied on to produce "
143
  "factually accurate information. The gpt-3.5-turbo model was trained on various public datasets; while great efforts "
144
  "have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
145
  "biased, or otherwise offensive outputs.",
 
200
  )
201
 
202
 
203
+ demo.queue(max_size=36, concurrency_count=14).launch(debug=True)