yuntian-deng commited on
Commit
bfcdf7c
1 Parent(s): 749e38e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -44
app.py CHANGED
@@ -35,47 +35,47 @@ def parse_codeblock(text):
35
  def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
36
 
37
  payload = {
38
- "model": MODEL,
39
- "messages": [{"role": "user", "content": f"{inputs}"}],
40
- "temperature" : 1.0,
41
- "top_p":1.0,
42
- "n" : 1,
43
- "stream": True,
44
- "presence_penalty":0,
45
- "frequency_penalty":0,
46
  }
47
 
48
  headers = {
49
- "Content-Type": "application/json",
50
- "Authorization": f"Bearer {OPENAI_API_KEY}"
51
  }
52
 
53
  # print(f"chat_counter - {chat_counter}")
54
  if chat_counter != 0 :
55
  messages = []
56
  for i, data in enumerate(history):
57
- if i % 2 == 0:
58
- role = 'user'
59
- else:
60
- role = 'assistant'
61
- temp = {}
62
- temp["role"] = role
63
- temp["content"] = data
64
- messages.append(temp)
65
 
66
  temp3 = {}
67
  temp3["role"] = "user"
68
  temp3["content"] = inputs
69
  messages.append(temp3)
70
  payload = {
71
- "model": MODEL,
72
- "messages": messages,
73
- "temperature" : temperature, #1.0,
74
- "top_p": top_p, #1.0,
75
- "n" : 1,
76
- "stream": True,
77
- "presence_penalty":0,
78
- "frequency_penalty":0,
79
  }
80
 
81
  chat_counter+=1
@@ -85,39 +85,37 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
85
  response = requests.post(API_URL, headers=headers, json=payload, stream=True)
86
  response_code = f"{response}"
87
  if response_code.strip() != "<Response [200]>":
88
- #print(f"response code - {response}")
89
  raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
90
  token_counter = 0
91
  partial_words = ""
92
- counter=0
93
  for chunk in response.iter_lines():
94
  #Skipping first chunk
95
  if counter == 0:
96
  counter+=1
97
  continue
98
- #counter+=1
99
  # check whether each line is non-empty
100
  if chunk.decode() :
101
- chunk = chunk.decode()
102
- # decode each line as response data is in bytes
103
- if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
104
- #if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
105
- # break
106
- partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
107
- if token_counter == 0:
108
- history.append(" " + partial_words)
109
- else:
110
- history[-1] = partial_words
111
- chat = [(parse_codeblock(history[i]), parse_codeblock(history[i + 1])) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
112
- token_counter+=1
113
- yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
114
  print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
115
 
116
 
117
  def reset_textbox():
118
  return gr.update(value='')
119
 
120
- title = """<h1 align="center">GPT3.5 Chatbot</h1>"""
121
  if DISABLED:
122
  title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. We are currently requesting an increase in our quota. Please check back in a few days.</h1>"""
123
  description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
@@ -137,7 +135,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
137
  #chatbot {height: 520px; overflow: auto;}""",
138
  theme=theme) as demo:
139
  gr.HTML(title)
140
- gr.HTML("""<h3 align="center">This App provides you full access to GPT3.5 API (4096 token limit). You don't need any OPENAI API key</h1>""")
141
  gr.HTML('''<center><a href="https://huggingface.co/spaces/yuntian-deng/ChatGPT?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
142
  with gr.Column(elem_id = "col_container", visible=False) as main_block:
143
  #API Key is provided by OpenAI
 
35
  def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
36
 
37
  payload = {
38
+ "model": MODEL,
39
+ "messages": [{"role": "user", "content": f"{inputs}"}],
40
+ "temperature" : 1.0,
41
+ "top_p":1.0,
42
+ "n" : 1,
43
+ "stream": True,
44
+ "presence_penalty":0,
45
+ "frequency_penalty":0,
46
  }
47
 
48
  headers = {
49
+ "Content-Type": "application/json",
50
+ "Authorization": f"Bearer {OPENAI_API_KEY}"
51
  }
52
 
53
  # print(f"chat_counter - {chat_counter}")
54
  if chat_counter != 0 :
55
  messages = []
56
  for i, data in enumerate(history):
57
+ if i % 2 == 0:
58
+ role = 'user'
59
+ else:
60
+ role = 'assistant'
61
+ temp = {}
62
+ temp["role"] = role
63
+ temp["content"] = data
64
+ messages.append(temp)
65
 
66
  temp3 = {}
67
  temp3["role"] = "user"
68
  temp3["content"] = inputs
69
  messages.append(temp3)
70
  payload = {
71
+ "model": MODEL,
72
+ "messages": messages,
73
+ "temperature" : temperature,
74
+ "top_p": top_p,
75
+ "n" : 1,
76
+ "stream": True,
77
+ "presence_penalty":0,
78
+ "frequency_penalty":0,
79
  }
80
 
81
  chat_counter+=1
 
85
  response = requests.post(API_URL, headers=headers, json=payload, stream=True)
86
  response_code = f"{response}"
87
  if response_code.strip() != "<Response [200]>":
 
88
  raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
89
  token_counter = 0
90
  partial_words = ""
91
+ counter = 0
92
  for chunk in response.iter_lines():
93
  #Skipping first chunk
94
  if counter == 0:
95
  counter+=1
96
  continue
 
97
  # check whether each line is non-empty
98
  if chunk.decode() :
99
+ chunk = chunk.decode()
100
+ # decode each line as response data is in bytes
101
+ if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
102
+ #if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
103
+ # break
104
+ partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
105
+ if token_counter == 0:
106
+ history.append(" " + partial_words)
107
+ else:
108
+ history[-1] = partial_words
109
+ chat = [(parse_codeblock(history[i]), parse_codeblock(history[i + 1])) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
110
+ token_counter+=1
111
+ yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
112
  print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
113
 
114
 
115
  def reset_textbox():
116
  return gr.update(value='')
117
 
118
+ title = """<h1 align="center">GPT-3.5 Chatbot</h1>"""
119
  if DISABLED:
120
  title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. We are currently requesting an increase in our quota. Please check back in a few days.</h1>"""
121
  description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
 
135
  #chatbot {height: 520px; overflow: auto;}""",
136
  theme=theme) as demo:
137
  gr.HTML(title)
138
+ gr.HTML("""<h3 align="center">This App provides you full access to GPT-3.5 (4096 token limit). You don't need any OPENAI API key.</h1>""")
139
  gr.HTML('''<center><a href="https://huggingface.co/spaces/yuntian-deng/ChatGPT?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
140
  with gr.Column(elem_id = "col_container", visible=False) as main_block:
141
  #API Key is provided by OpenAI