tonic commited on
Commit
fa0faa4
1 Parent(s): fb8b6b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -10
app.py CHANGED
@@ -3,20 +3,31 @@ import gradio as gr
3
  import requests
4
  import os
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Define the API parameters
7
- API_URL = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model"
8
- API_TOKEN = os.getenv("HF_AUTH_TOKEN")
9
- if not API_TOKEN:
10
- raise ValueError("Please set the HF_AUTH_TOKEN environment variable.")
11
 
12
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
13
 
14
  # Function to query the API
15
  def query(payload):
16
- response = requests.post(API_URL, headers=headers, json=payload)
17
  return response.json()
18
 
19
- # Function to be called by the Gradio interface
 
20
  def evaluate_hallucination(input1, input2):
21
  # Combine the inputs
22
  combined_input = f"{input1}. {input2}"
@@ -27,11 +38,117 @@ def evaluate_hallucination(input1, input2):
27
  # Extract the score from the output
28
  score = output[0][0]['score']
29
 
30
- # Return a red or green circle based on the score
31
  if score < 0.5:
32
- return "🔴", "The score is less than 0.5"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  else:
34
- return "🟢", "The score is greater than 0.5"
35
 
36
  # Create the Gradio interface
37
  iface = gr.Interface(
 
3
  import requests
4
  import os
5
 
6
+ def check_hallucination(assertion, citation):
7
+ api_url = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model"
8
+ header = {"Authorization": f"Bearer {hf_token}"}
9
+ payload = {"inputs": f"{assertion} [SEP] {citation}"}
10
+
11
+ response = requests.post(api_url, headers=header, json=payload, timeout=120)
12
+ output = response.json()
13
+ output = output[0][0]["score"]
14
+
15
+ return f"**hallucination score:** {output}"
16
+
17
+
18
  # Define the API parameters
19
+ vapi_url = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model"
20
+
21
+ headers = {"Authorization": f"Bearer {hf_token}"}
 
22
 
 
23
 
24
  # Function to query the API
25
  def query(payload):
26
+ response = requests.post(vapi_url, headers=headers, json=payload)
27
  return response.json()
28
 
29
+
30
+ # Function to evaluate hallucination
31
  def evaluate_hallucination(input1, input2):
32
  # Combine the inputs
33
  combined_input = f"{input1}. {input2}"
 
38
  # Extract the score from the output
39
  score = output[0][0]['score']
40
 
41
+ # Generate a label based on the score
42
  if score < 0.5:
43
+ label = f"🔴 High risk. Score: {score:.2f}"
44
+ else:
45
+ label = f"🟢 Low risk. Score: {score:.2f}"
46
+
47
+ return label
48
+
49
+ def query_vectara(text):
50
+ user_message = text
51
+
52
+ # Read authentication parameters from the .env file
53
+ customer_id = os.getenv('CUSTOMER_ID')
54
+ corpus_id = os.getenv('CORPUS_ID')
55
+ api_key = os.getenv('API_KEY')
56
+
57
+ # Define the headers
58
+ api_key_header = {
59
+ "customer-id": customer_id,
60
+ "x-api-key": api_key
61
+ }
62
+
63
+ # Define the request body in the structure provided in the example
64
+ request_body = {
65
+ "query": [
66
+ {
67
+ "query": user_message,
68
+ "queryContext": "",
69
+ "start": 1,
70
+ "numResults": 25,
71
+ "contextConfig": {
72
+ "charsBefore": 0,
73
+ "charsAfter": 0,
74
+ "sentencesBefore": 2,
75
+ "sentencesAfter": 2,
76
+ "startTag": "%START_SNIPPET%",
77
+ "endTag": "%END_SNIPPET%",
78
+ },
79
+ "rerankingConfig": {
80
+ "rerankerId": 272725718,
81
+ "mmrConfig": {
82
+ "diversityBias": 0.35
83
+ }
84
+ },
85
+ "corpusKey": [
86
+ {
87
+ "customerId": customer_id,
88
+ "corpusId": corpus_id,
89
+ "semantics": 0,
90
+ "metadataFilter": "",
91
+ "lexicalInterpolationConfig": {
92
+ "lambda": 0
93
+ },
94
+ "dim": []
95
+ }
96
+ ],
97
+ "summary": [
98
+ {
99
+ "maxSummarizedResults": 5,
100
+ "responseLang": "auto",
101
+ "summarizerPromptName": "vectara-summary-ext-v1.2.0"
102
+ }
103
+ ]
104
+ }
105
+ ]
106
+ }
107
+
108
+ # Make the API request using Gradio
109
+ response = requests.post(
110
+ "https://api.vectara.io/v1/query",
111
+ json=request_body, # Use json to automatically serialize the request body
112
+ verify=True,
113
+ headers=api_key_header
114
+ )
115
+
116
+ if response.status_code == 200:
117
+ query_data = response.json()
118
+ if query_data:
119
+ sources_info = []
120
+
121
+ # Extract the summary.
122
+ summary = query_data['responseSet'][0]['summary'][0]['text']
123
+
124
+ # Iterate over all response sets
125
+ for response_set in query_data.get('responseSet', []):
126
+ # Extract sources
127
+ # Limit to top 5 sources.
128
+ for source in response_set.get('response', [])[:5]:
129
+ source_metadata = source.get('metadata', [])
130
+ source_info = {}
131
+
132
+ for metadata in source_metadata:
133
+ metadata_name = metadata.get('name', '')
134
+ metadata_value = metadata.get('value', '')
135
+
136
+ if metadata_name == 'title':
137
+ source_info['title'] = metadata_value
138
+ elif metadata_name == 'author':
139
+ source_info['author'] = metadata_value
140
+ elif metadata_name == 'pageNumber':
141
+ source_info['page number'] = metadata_value
142
+
143
+ if source_info:
144
+ sources_info.append(source_info)
145
+
146
+ result = {"summary": summary, "sources": sources_info}
147
+ return f"{json.dumps(result, indent=2)}"
148
+ else:
149
+ return "No data found in the response."
150
  else:
151
+ return f"Error: {response.status_code}"
152
 
153
  # Create the Gradio interface
154
  iface = gr.Interface(