Alyafeai commited on
Commit
9575c3b
1 Parent(s): 4aee7a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -22
app.py CHANGED
@@ -2,11 +2,10 @@ import gradio as gr
2
  from dataclasses import dataclass
3
  import os
4
  from uuid import uuid4
5
- import json
6
  import requests
7
 
8
 
9
- DEPLOYED = os.getenv("DEPLOYED", "true").lower() != "true"
10
  MODEL_NAME = os.getenv("MODEL_NAME")
11
  HEADERS = {"Content-Type": "application/json"}
12
  ENDPOINT_URL = os.getenv("ENDPOINT_URL")
@@ -153,7 +152,6 @@ def introduction():
153
  """
154
  )
155
 
156
-
157
  def chat_tab():
158
  def run_chat(
159
  message: str,
@@ -166,7 +164,6 @@ def chat_tab():
166
  session_id: str,
167
  ):
168
  prompt = format_chat_prompt(message, history, instructions, user_name, bot_name)
169
-
170
  payload = {
171
  "endpoint": MODEL_NAME,
172
  "data": {
@@ -182,41 +179,52 @@ def chat_tab():
182
  "session_id": session_id,
183
  },
184
  }
185
-
186
  sess = requests.Session()
187
  full_output = ""
188
- with sess.post(
189
- ENDPOINT_URL, headers=HEADERS, json=payload, stream=True
190
- ) as response:
191
- if response.status_code == 200:
192
- for chunk in response.iter_content(chunk_size=4):
193
- if chunk:
194
- decoded = chunk.decode("utf-8")
195
- full_output += decoded
196
- if full_output.endswith("User:"):
197
- yield full_output[:-5]
198
- break
199
- else:
200
- yield full_output
201
  last_n = 5
 
202
  while full_output == "" and last_n > 0:
203
  payload["data"]["inputs"] = format_chat_prompt(
204
- message, history, instructions, user_name, bot_name, False, last_n
205
  )
206
  with sess.post(
207
  ENDPOINT_URL, headers=HEADERS, json=payload, stream=True
208
  ) as response:
209
- if response.status_code == 200:
210
- for chunk in response.iter_content(chunk_size=4):
211
- if chunk:
 
 
 
 
 
 
 
 
 
 
 
212
  decoded = chunk.decode("utf-8")
 
213
  full_output += decoded
214
  if full_output.endswith("User:"):
215
  yield full_output[:-5]
216
  break
217
  else:
218
  yield full_output
 
 
 
 
 
 
 
 
 
219
  last_n -= 1
 
 
 
220
  return ""
221
 
222
  with gr.Column():
 
2
  from dataclasses import dataclass
3
  import os
4
  from uuid import uuid4
 
5
  import requests
6
 
7
 
8
+ DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"
9
  MODEL_NAME = os.getenv("MODEL_NAME")
10
  HEADERS = {"Content-Type": "application/json"}
11
  ENDPOINT_URL = os.getenv("ENDPOINT_URL")
 
152
  """
153
  )
154
 
 
155
  def chat_tab():
156
  def run_chat(
157
  message: str,
 
164
  session_id: str,
165
  ):
166
  prompt = format_chat_prompt(message, history, instructions, user_name, bot_name)
 
167
  payload = {
168
  "endpoint": MODEL_NAME,
169
  "data": {
 
179
  "session_id": session_id,
180
  },
181
  }
 
182
  sess = requests.Session()
183
  full_output = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  last_n = 5
185
+ include_all_chat_history = True
186
  while full_output == "" and last_n > 0:
187
  payload["data"]["inputs"] = format_chat_prompt(
188
+ message, history, instructions, user_name, bot_name, include_all_chat_history, last_n
189
  )
190
  with sess.post(
191
  ENDPOINT_URL, headers=HEADERS, json=payload, stream=True
192
  ) as response:
193
+ if response.status_code != 200:
194
+ include_all_chat_history = False
195
+ last_n -= 1
196
+ continue
197
+ iterator = response.iter_content(chunk_size = 8)
198
+ try:
199
+ chunk = next(iterator)
200
+ except (StopIteration, StopAsyncIteration):
201
+ include_all_chat_history = False
202
+ last_n -= 1
203
+ continue
204
+ while chunk:
205
+ try:
206
+ try:
207
  decoded = chunk.decode("utf-8")
208
+ chunk = b''
209
  full_output += decoded
210
  if full_output.endswith("User:"):
211
  yield full_output[:-5]
212
  break
213
  else:
214
  yield full_output
215
+ chunk = next(iterator)
216
+ except UnicodeDecodeError as e:
217
+ chunk += next(iterator)
218
+
219
+ except (StopIteration, StopAsyncIteration) as si:
220
+ if chunk:
221
+ yield chunk.decode('UTF-8', 'ignore')
222
+ break
223
+ include_all_chat_history = False
224
  last_n -= 1
225
+
226
+ if full_output:
227
+ return "Sorry, I could not understand. Could you please rephrase your question?"
228
  return ""
229
 
230
  with gr.Column():