Spaces:
Running
Running
| import gradio as gr | |
| import requests | |
| import os | |
| # Define the API parameters | |
| API_URL = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model" | |
| API_TOKEN = os.getenv("HF_AUTH_TOKEN") | |
| if not API_TOKEN: | |
| raise ValueError("Please set the HF_AUTH_TOKEN environment variable.") | |
| headers = {"Authorization": f"Bearer {API_TOKEN}"} | |
| # Function to query the API | |
| def query(payload): | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| return response.json() | |
| # Function to be called by the Gradio interface | |
| def evaluate_hallucination(input1, input2): | |
| # Combine the inputs | |
| combined_input = f"{input1}. {input2}" | |
| # Make the API call | |
| output = query({"inputs": combined_input}) | |
| # Extract the score from the output | |
| score = output[0][0]['score'] | |
| # Return a red or green circle based on the score | |
| if score < 0.5: | |
| return "π΄", "The score is less than 0.5" | |
| else: | |
| return "π’", "The score is greater than 0.5" | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=evaluate_hallucination, | |
| inputs=[gr.inputs.Textbox(label="Input 1"), gr.inputs.Textbox(label="Input 2")], | |
| outputs=[gr.outputs.Label(), gr.outputs.Textbox(label="Explanation")], | |
| live=False | |
| ) | |
| # Launch the interface | |
| iface.launch() |