Tonic commited on
Commit
bb6e5e5
1 Parent(s): 3b73227

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import requests
3
  import os
4
  import json
 
5
  import transformers
6
  from transformers import AutoTokenizer, AutoModelForCausalLM
7
 
@@ -30,11 +31,33 @@ def check_hallucination(assertion, citation):
30
  header = {"Authorization": f"Bearer {hf_token}"}
31
  payload = {"inputs": f"{assertion} [SEP] {citation}"}
32
 
33
- response = requests.post(api_url, headers=header, json=payload, timeout=120)
34
- output = response.json()
35
- output = output[0][0]["score"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- return f"**hallucination score:** {output}"
38
 
39
 
40
  def query_vectara(text):
 
2
  import requests
3
  import os
4
  import json
5
+ import time
6
  import transformers
7
  from transformers import AutoTokenizer, AutoModelForCausalLM
8
 
 
31
  header = {"Authorization": f"Bearer {hf_token}"}
32
  payload = {"inputs": f"{assertion} [SEP] {citation}"}
33
 
34
+ attempts = 0
35
+ max_attempts = 3
36
+ wait_time = 180 # 3 minutes
37
+
38
+ while attempts < max_attempts:
39
+ try:
40
+ response = requests.post(api_url, headers=header, json=payload, timeout=120)
41
+ response.raise_for_status() # This will raise an exception for HTTP error codes
42
+ output = response.json()
43
+ output = output[0][0]["score"]
44
+ return f"**hallucination score:** {output}"
45
+ except requests.exceptions.HTTPError as http_err:
46
+ print(f"HTTP error occurred: {http_err}") # Python 3.6
47
+ except requests.exceptions.RequestException as err:
48
+ print(f"Other error occurred: {err}") # Python 3.6
49
+ except KeyError:
50
+ print("KeyError: The expected key was not found in the response. The endpoint might be waking up.")
51
+
52
+ attempts += 1
53
+ if attempts < max_attempts:
54
+ print(f"Attempt {attempts} failed. Waiting for {wait_time} seconds before retrying...")
55
+ time.sleep(wait_time)
56
+ else:
57
+ print("Maximum attempts reached. Please try again later.")
58
+ return "Error: Unable to retrieve hallucination score after multiple attempts."
59
 
60
+ return "Error: Unable to process the hallucination check."
61
 
62
 
63
  def query_vectara(text):