IlyasMoutawwakil HF staff commited on
Commit
5becf31
1 Parent(s): f82711d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -12
app.py CHANGED
@@ -23,20 +23,44 @@ HF_TOKEN = os.getenv("HF_TOKEN")
23
  RANKER_URL = os.getenv("RANKER_URL")
24
  RETRIEVER_URL = os.getenv("RETRIEVER_URL")
25
 
26
- # RETRIEVER_IE = get_inference_endpoint(
27
- # "fastrag-retriever", namespace="optimum-intel", token=HF_TOKEN
28
- # )
29
- # RANKER_IE = get_inference_endpoint(
30
- # "fastrag-ranker", namespace="optimum-intel", token=HF_TOKEN
31
- # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # if RETRIEVER_IE.status != "running":
34
- # RETRIEVER_IE.resume()
35
- # RETRIEVER_IE.wait()
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # if RANKER_IE.status != "running":
38
- # RANKER_IE.resume()
39
- # RANKER_IE.wait()
40
 
41
 
42
  def post(url, payload):
@@ -189,6 +213,14 @@ pipe.add_node(component=ranker, name="Ranker", inputs=["Retriever"])
189
 
190
 
191
  def run(query: str) -> dict:
 
 
 
 
 
 
 
 
192
  pipe_output = pipe.run(query=query)
193
 
194
  output = f"""<h2>Top {TOP_K} Documents</h2>"""
 
23
  RANKER_URL = os.getenv("RANKER_URL")
24
  RETRIEVER_URL = os.getenv("RETRIEVER_URL")
25
 
26
+ RETRIEVER_IE = get_inference_endpoint(
27
+ "fastrag-retriever", namespace="optimum-intel", token=HF_TOKEN
28
+ )
29
+ RANKER_IE = get_inference_endpoint(
30
+ "fastrag-ranker", namespace="optimum-intel", token=HF_TOKEN
31
+ )
32
+
33
+ def check_inference_endpoints():
34
+ RETRIEVER_IE.update()
35
+ RANKER_IE.update()
36
+
37
+ messages = []
38
+
39
+ if RETRIEVER_IE.status in ["initializing", "pending"]:
40
+ messages += [
41
+ f"Retriever Inference Endpoint is {RETRIEVER_IE.status}. Please wait a few seconds and try again."
42
+ ]
43
+ elif RETRIEVER_IE.status in ["paused", "scaledToZero"]:
44
+ messages += [
45
+ f"Retriever Inference Endpoint is {RETRIEVER_IE.status}. Resuming it. Please wait a few seconds and try again."
46
+ ]
47
+ RETRIEVER_IE.resume()
48
 
49
+ if RANKER_IE.status in ["initializing", "pending"]:
50
+ messages += [
51
+ f"Ranker Inference Endpoint is {RANKER_IE.status}. Please wait a few seconds and try again."
52
+ ]
53
+ elif RANKER_IE.status in ["paused", "scaledToZero"]:
54
+ messages += [
55
+ f"Ranker Inference Endpoint is {RANKER_IE.status}. Resuming it. Please wait a few seconds and try again."
56
+ ]
57
+ RANKER_IE.resume()
58
+
59
+ if len(messages) > 0:
60
+ return "<br>".join(messages)
61
+ else:
62
+ return None
63
 
 
 
 
64
 
65
 
66
  def post(url, payload):
 
213
 
214
 
215
  def run(query: str) -> dict:
216
+ message = check_inference_endpoints()
217
+
218
+ if message is not None:
219
+ return f"""
220
+ <h2>Service Unavailable</h2>
221
+ <p>{message}</p>
222
+ """
223
+
224
  pipe_output = pipe.run(query=query)
225
 
226
  output = f"""<h2>Top {TOP_K} Documents</h2>"""