ysharma HF staff commited on
Commit
efe4a11
1 Parent(s): 49e5889

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -6,9 +6,8 @@ import requests
6
  hf_token = os.getenv('HF_TOKEN')
7
  api_url = os.getenv('API_URL')
8
  api_url_nostream = os.getenv('API_URL_NOSTREAM')
9
- headers = {
10
- 'Content-Type': 'application/json',
11
- }
12
 
13
  system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
14
  title = "Llama2 70B Chatbot"
@@ -103,7 +102,6 @@ def predict(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=
103
 
104
  # No Stream
105
  def predict_batch(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.6, repetition_penalty=1.0,):
106
-
107
  if system_prompt != "":
108
  input_prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n "
109
  else:
@@ -118,6 +116,7 @@ def predict_batch(message, chatbot, system_prompt="", temperature=0.9, max_new_t
118
  input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s>[INST] "
119
 
120
  input_prompt = input_prompt + str(message) + " [/INST] "
 
121
 
122
  data = {
123
  "inputs": input_prompt,
@@ -130,17 +129,17 @@ def predict_batch(message, chatbot, system_prompt="", temperature=0.9, max_new_t
130
  },
131
  }
132
 
133
- response = requests.post(api_url_nostream, headers=headers, data=json.dumps(data), auth=('hf', hf_token))
134
 
135
  if response.status_code == 200: # check if the request was successful
136
  try:
137
  json_obj = response.json()
138
- if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
139
- return json_obj['generated_text']
140
- elif 'error' in json_obj:
141
- return json_obj['error'] + ' Please refresh and try again with smaller input prompt'
142
  else:
143
- print(f"Unexpected response: {json_obj}")
144
  except json.JSONDecodeError:
145
  print(f"Failed to decode response as JSON: {response.text}")
146
  else:
@@ -203,7 +202,7 @@ chat_interface_stream = gr.ChatInterface(predict,
203
  chatbot=chatbot_stream,
204
  css=css,
205
  examples=examples,
206
- cache_examples=True,
207
  additional_inputs=additional_inputs,)
208
  chat_interface_batch=gr.ChatInterface(predict_batch,
209
  title=title,
@@ -212,7 +211,7 @@ chat_interface_batch=gr.ChatInterface(predict_batch,
212
  chatbot=chatbot_batch,
213
  css=css,
214
  examples=examples,
215
- cache_examples=True,
216
  additional_inputs=additional_inputs,)
217
 
218
  # Gradio Demo
 
6
  hf_token = os.getenv('HF_TOKEN')
7
  api_url = os.getenv('API_URL')
8
  api_url_nostream = os.getenv('API_URL_NOSTREAM')
9
+ #headers = {'Content-Type': 'application/json',}
10
+ headers = {"Authorization": f"Bearer {hf_token}"}
 
11
 
12
  system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
13
  title = "Llama2 70B Chatbot"
 
102
 
103
  # No Stream
104
  def predict_batch(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.6, repetition_penalty=1.0,):
 
105
  if system_prompt != "":
106
  input_prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n "
107
  else:
 
116
  input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s>[INST] "
117
 
118
  input_prompt = input_prompt + str(message) + " [/INST] "
119
+ print(f"input_prompt - {input_prompt}")
120
 
121
  data = {
122
  "inputs": input_prompt,
 
129
  },
130
  }
131
 
132
+ response = requests.post(api_url_nostream, headers=headers, json=data )
133
 
134
  if response.status_code == 200: # check if the request was successful
135
  try:
136
  json_obj = response.json()
137
+ if 'generated_text' in json_obj[0] and len(json_obj[0]['generated_text']) > 0:
138
+ return json_obj[0]['generated_text']
139
+ elif 'error' in json_obj[0]:
140
+ return json_obj[0]['error'] + ' Please refresh and try again with smaller input prompt'
141
  else:
142
+ print(f"Unexpected response: {json_obj[0]}")
143
  except json.JSONDecodeError:
144
  print(f"Failed to decode response as JSON: {response.text}")
145
  else:
 
202
  chatbot=chatbot_stream,
203
  css=css,
204
  examples=examples,
205
+ #cache_examples=True,
206
  additional_inputs=additional_inputs,)
207
  chat_interface_batch=gr.ChatInterface(predict_batch,
208
  title=title,
 
211
  chatbot=chatbot_batch,
212
  css=css,
213
  examples=examples,
214
+ #cache_examples=True,
215
  additional_inputs=additional_inputs,)
216
 
217
  # Gradio Demo