kelvinyasu commited on
Commit
6f32094
1 Parent(s): bf8b6c7

Update app.py

Browse files

Fix json return error

Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -40,7 +40,7 @@ def predict(message, chatbot):
40
  "temperature":0.9,}
41
  }
42
 
43
- response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token), stream=True)
44
 
45
  partial_message = ""
46
  for line in response.iter_lines():
@@ -53,13 +53,14 @@ def predict(message, chatbot):
53
  json_line = decoded_line[5:] # Exclude the first 5 characters ('data:')
54
  else:
55
  gr.Warning(f"This line does not start with 'data:': {decoded_line}")
 
56
  continue
57
 
58
  # Load as JSON
59
  try:
60
- json_obj = json.loads(json_line)
61
- if 'token' in json_obj:
62
- partial_message = partial_message + json_obj['token']['text']
63
  yield partial_message
64
  elif 'error' in json_obj:
65
  yield json_obj['error'] + '. Please refresh and try again with an appropriate smaller input prompt.'
@@ -73,4 +74,4 @@ def predict(message, chatbot):
73
  gr.Warning(f"KeyError: {e} occurred for JSON object: {json_obj}")
74
  continue
75
 
76
- gr.ChatInterface(predict, title=title, description=description, css=css, examples=examples, cache_examples=True).queue(concurrency_count=75).launch(debug=True)
 
40
  "temperature":0.9,}
41
  }
42
 
43
+ response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('Bearer', hf_token), stream=True)
44
 
45
  partial_message = ""
46
  for line in response.iter_lines():
 
53
  json_line = decoded_line[5:] # Exclude the first 5 characters ('data:')
54
  else:
55
  gr.Warning(f"This line does not start with 'data:': {decoded_line}")
56
+ json_line = decoded_line
57
  continue
58
 
59
  # Load as JSON
60
  try:
61
+ json_obj = json.loads(json_line)[0]
62
+ if 'generated_text' in json_obj:
63
+ partial_message = partial_message + json_obj['generated_text']#['token']['text']
64
  yield partial_message
65
  elif 'error' in json_obj:
66
  yield json_obj['error'] + '. Please refresh and try again with an appropriate smaller input prompt.'
 
74
  gr.Warning(f"KeyError: {e} occurred for JSON object: {json_obj}")
75
  continue
76
 
77
+ gr.ChatInterface(predict, title=title, description=description, css=css, examples=examples, cache_examples=True).queue(concurrency_count=75).launch()