DevangMahesh commited on
Commit
1ffbb97
·
1 Parent(s): 524fbfe

Added code to wait for Hope to wake up if it is sleeping

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -27,7 +27,15 @@ hope_client = Client("HopeLLM/HopeV1.0", hf_token=os.getenv("HF_READ_TKN"))
27
  Lock = threading.Lock()
28
  TIMESTAMP_LAST_CALL_TO_HOPE = None
29
 
30
- def call_hope_api(input_txt):
 
 
 
 
 
 
 
 
31
  global hope_client
32
  global TIMESTAMP_LAST_CALL_TO_HOPE
33
 
@@ -35,6 +43,14 @@ def call_hope_api(input_txt):
35
  with Lock:
36
  if hope_client == None:
37
  hope_client = Client("HopeLLM/HopeV1.0", hf_token=os.getenv("HF_READ_TKN"))
 
 
 
 
 
 
 
 
38
  return hope_client.predict(inp = input_txt, api_name = "/answer_question")
39
 
40
  def reset_hope_client():
@@ -142,13 +158,13 @@ def backgnd_process_message(message):
142
  response_text = "Awesome, got your story. Now, let me ask you some questions."
143
  ## Generate question from llama
144
  llama_input = llama_input_template.format(story_to_use = message_text, fixed_prompt=FIXED_PROMPT)
145
- llama_output = call_hope_api(llama_input)
146
  logger.info(f"LLama Ouput: {llama_output}")
147
  response_text = response_text + "My question for you is \n \n" + llama_output
148
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
149
  else:
150
  llama_input = ONGOING_CONVO[sender_id]['Llama_conv'] + message_text + "[/INST]"
151
- llama_output = call_hope_api(llama_input)
152
  logger.info(f"LLama Ouput: {llama_output}")
153
  response_text = llama_output
154
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
@@ -162,13 +178,13 @@ def backgnd_process_message(message):
162
  response_text = "Awesome, got your story. Now, let me ask you some questions."
163
  ## Generate question from llama
164
  llama_input = llama_input_template.format(story_to_use = transcription, fixed_prompt=FIXED_PROMPT)
165
- llama_output = call_hope_api(llama_input)
166
  logger.info(f"LLama Ouput: {llama_output}")
167
  response_text = response_text + "My question for you is \n \n" + llama_output
168
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
169
  else:
170
  llama_input = ONGOING_CONVO[sender_id]['Llama_conv'] + transcription + "[/INST]"
171
- llama_output = call_hope_api(llama_input)
172
  logger.info(f"LLama Ouput: {llama_output}")
173
  response_text = llama_output
174
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
@@ -176,13 +192,13 @@ def backgnd_process_message(message):
176
  if message["interactive"]["button_reply"]["id"] == "random_story":
177
  ONGOING_CONVO[sender_id]['Story'] = "random_story"
178
  #send_whatsapp_message(sender_id, message_id, "Perfect, let me find a nice story for today.")
179
- story_for_chat = call_hope_api(llama_input_gen_random_story)
180
  ONGOING_CONVO[sender_id]['Story'] = story_for_chat
181
  logger.info(f"Llama gave story {story_for_chat}")
182
 
183
  ## Generate question from llama
184
  llama_input = llama_input_template.format(story_to_use = story_for_chat, fixed_prompt=FIXED_PROMPT)
185
- llama_output = call_hope_api(llama_input)
186
  logger.info(f"LLama Ouput: {llama_output}")
187
  response_text = story_for_chat + "\n \n" + llama_output
188
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
 
27
  Lock = threading.Lock()
28
  TIMESTAMP_LAST_CALL_TO_HOPE = None
29
 
30
+ def is_space_sleeping():
31
+ headers = {"Authorization": f"Bearer {os.getenv("HF_READ_TKN")}"}
32
+ response = requests.get("https://huggingface.co/api/spaces/HopeLLM/HopeV1.0", headers=headers)
33
+ if response.status_code == 200:
34
+ space_info = response.json()
35
+ return space_info.get("runtime", {}).get("stage", "") == "sleeping"
36
+ return "unknown" # If API call fails, assume the space is sleeping
37
+
38
+ def call_hope_api(input_txt, sender_id):
39
  global hope_client
40
  global TIMESTAMP_LAST_CALL_TO_HOPE
41
 
 
43
  with Lock:
44
  if hope_client == None:
45
  hope_client = Client("HopeLLM/HopeV1.0", hf_token=os.getenv("HF_READ_TKN"))
46
+ if(is_space_sleeping()==True):
47
+ logger.info("Hope is sleeping, trying to wake it up")
48
+ send_whatsapp_message(sender_id, None, "Hope is sleeping, waking it up ;)\nKindly wait for some time", text=True)
49
+ while(is_space_sleeping()):
50
+ #Sleep for 30sec
51
+ time.sleep(30)
52
+ elif (is_space_sleeping() == "unknown"):
53
+ logger.error("Something wrong with Hope Check")
54
  return hope_client.predict(inp = input_txt, api_name = "/answer_question")
55
 
56
  def reset_hope_client():
 
158
  response_text = "Awesome, got your story. Now, let me ask you some questions."
159
  ## Generate question from llama
160
  llama_input = llama_input_template.format(story_to_use = message_text, fixed_prompt=FIXED_PROMPT)
161
+ llama_output = call_hope_api(llama_input,sender_id)
162
  logger.info(f"LLama Ouput: {llama_output}")
163
  response_text = response_text + "My question for you is \n \n" + llama_output
164
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
165
  else:
166
  llama_input = ONGOING_CONVO[sender_id]['Llama_conv'] + message_text + "[/INST]"
167
+ llama_output = call_hope_api(llama_input,sender_id)
168
  logger.info(f"LLama Ouput: {llama_output}")
169
  response_text = llama_output
170
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
 
178
  response_text = "Awesome, got your story. Now, let me ask you some questions."
179
  ## Generate question from llama
180
  llama_input = llama_input_template.format(story_to_use = transcription, fixed_prompt=FIXED_PROMPT)
181
+ llama_output = call_hope_api(llama_input,sender_id)
182
  logger.info(f"LLama Ouput: {llama_output}")
183
  response_text = response_text + "My question for you is \n \n" + llama_output
184
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
185
  else:
186
  llama_input = ONGOING_CONVO[sender_id]['Llama_conv'] + transcription + "[/INST]"
187
+ llama_output = call_hope_api(llama_input,sender_id)
188
  logger.info(f"LLama Ouput: {llama_output}")
189
  response_text = llama_output
190
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "
 
192
  if message["interactive"]["button_reply"]["id"] == "random_story":
193
  ONGOING_CONVO[sender_id]['Story'] = "random_story"
194
  #send_whatsapp_message(sender_id, message_id, "Perfect, let me find a nice story for today.")
195
+ story_for_chat = call_hope_api(llama_input_gen_random_story,sender_id)
196
  ONGOING_CONVO[sender_id]['Story'] = story_for_chat
197
  logger.info(f"Llama gave story {story_for_chat}")
198
 
199
  ## Generate question from llama
200
  llama_input = llama_input_template.format(story_to_use = story_for_chat, fixed_prompt=FIXED_PROMPT)
201
+ llama_output = call_hope_api(llama_input,sender_id)
202
  logger.info(f"LLama Ouput: {llama_output}")
203
  response_text = story_for_chat + "\n \n" + llama_output
204
  ONGOING_CONVO[sender_id]['Llama_conv'] = llama_input + llama_output + "</s> <s> [INST] "