Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,43 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
# Define the API parameters
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model"
|
| 6 |
+
API_TOKEN = os.getenv("HF_AUTH_TOKEN")
|
| 7 |
+
if not API_TOKEN:
|
| 8 |
+
raise ValueError("Please set the HF_AUTH_TOKEN environment variable.")
|
| 9 |
+
|
| 10 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 11 |
+
|
| 12 |
+
# Function to query the API
|
| 13 |
+
def query(payload):
|
| 14 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 15 |
+
return response.json()
|
| 16 |
+
|
| 17 |
+
# Function to be called by the Gradio interface
|
| 18 |
+
def evaluate_hallucination(input1, input2):
|
| 19 |
+
# Combine the inputs
|
| 20 |
+
combined_input = f"{input1}. {input2}"
|
| 21 |
+
|
| 22 |
+
# Make the API call
|
| 23 |
+
output = query({"inputs": combined_input})
|
| 24 |
+
|
| 25 |
+
# Extract the score from the output
|
| 26 |
+
score = output[0][0]['score']
|
| 27 |
+
|
| 28 |
+
# Return a red or green circle based on the score
|
| 29 |
+
if score < 0.5:
|
| 30 |
+
return "🔴", "The score is less than 0.5"
|
| 31 |
+
else:
|
| 32 |
+
return "🟢", "The score is greater than 0.5"
|
| 33 |
+
|
| 34 |
+
# Create the Gradio interface
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=evaluate_hallucination,
|
| 37 |
+
inputs=[gr.inputs.Textbox(label="Input 1"), gr.inputs.Textbox(label="Input 2")],
|
| 38 |
+
outputs=[gr.outputs.Label(), gr.outputs.Textbox(label="Explanation")],
|
| 39 |
+
live=False
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the interface
|
| 43 |
+
iface.launch()
|