Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import requests | |
import time | |
API_URL = "https://Iker-FactChecking-Backend.hf.space:7860" # Replace with your server's URL if different | |
API_KEY = os.getenv("API_KEY") | |
headers = {"X-API-Key": API_KEY, "Content-Type": "application/json"} | |
# Function to submit a fact-checking request | |
def submit_fact_check(article_topic, config): | |
endpoint = f"{API_URL}/fact-check" | |
payload = {"article_topic": article_topic, "config": config} | |
response = requests.post(endpoint, json=payload, headers=headers) | |
response.raise_for_status() # Raise an exception for HTTP errors | |
return response.json()["job_id"] | |
# Function to get the result of a fact-checking job | |
def get_fact_check_result(job_id): | |
endpoint = f"{API_URL}/result/{job_id}" | |
response = requests.get(endpoint, headers=headers) | |
response.raise_for_status() # Raise an exception for HTTP errors | |
return response.json() | |
def fact_checking(article_topic: str, config="pro"): | |
try: | |
job_id = submit_fact_check(article_topic, config.lower()) | |
print(f"Fact-checking job submitted. Job ID: {job_id}") | |
# Poll for results | |
while True: | |
result = get_fact_check_result(job_id) | |
if result["status"] == "completed": | |
print("Fact-checking completed:") | |
print(result["result"]) | |
break | |
elif result["status"] == "failed": | |
print("Fact-checking failed:") | |
print(result["error"]) | |
break | |
else: | |
print("Fact-checking in progress. Waiting...") | |
time.sleep(2) # Wait for 2 seconds before checking again | |
except requests.exceptions.RequestException as e: | |
print(f"An error occurred: {e}") | |
return result | |
if __name__ == "__main__": | |
theme = gr.themes.Soft( | |
primary_hue="green", | |
secondary_hue="gray", | |
neutral_hue="neutral", | |
font=[ | |
gr.themes.GoogleFont("Poppins"), | |
gr.themes.GoogleFont("Poppins"), | |
gr.themes.GoogleFont("Poppins"), | |
gr.themes.GoogleFont("Poppins"), | |
], | |
font_mono=[ | |
gr.themes.GoogleFont("Poppins"), | |
gr.themes.GoogleFont("Poppins"), | |
gr.themes.GoogleFont("Poppins"), | |
gr.themes.GoogleFont("Poppins"), | |
], | |
).set( | |
body_text_color="*secondary_600", | |
button_border_width="*block_label_border_width", | |
button_primary_background_fill="*primary_600", | |
button_secondary_background_fill="*primary_500", | |
button_secondary_background_fill_hover="*primary_400", | |
button_secondary_border_color="*primary_500", | |
) | |
with gr.Blocks( | |
theme=theme, | |
title="🤖 Automated Fact-Checking Engine", | |
analytics_enabled=False, | |
) as demo: | |
gr_text = gr.Textbox( | |
label="Fact-Checking Statement", | |
# info="Write here the statement you want to fact-check", | |
show_label=False, | |
lines=1, | |
interactive=True, | |
placeholder="Los coches electricos contaminan más que los coches de gasolina", | |
) | |
gr_mode = gr.Radio( | |
label="Fact-Checking Mode", | |
info="Choose the fact-checking mode. The \"Turbo\" mode is faster and cheaper. The \"Pro\" mode is more accurate but more expensive.", | |
choices=["Pro", "Turbo"], | |
type="value", | |
default="Pro", | |
show_label=False, | |
interactive=True, | |
) | |
gr_play = gr.Button("Fact-Checking") | |
gr_output = gr.Markdown( | |
label="Fact-Checking Results", | |
visible=True, | |
) | |
gr_ft = gr.Textbox( | |
label="fact_checking", | |
info="String with fact checking text", | |
lines=1, | |
interactive=False, | |
visible=False, | |
) | |
gr_qa = gr.Textbox( | |
label="qa", | |
info="Questions and answers, first line is the question, second line is the answer, and so on", | |
lines=1, | |
interactive=False, | |
visible=False, | |
) | |
gr_citations = gr.Textbox( | |
label="citations", | |
info="Here you will see the citations, first line is the citation id, second line is the url, and so on", | |
lines=1, | |
interactive=False, | |
visible=False, | |
) | |
gr_image = gr.Textbox( | |
label="image", | |
info="Contains the image url", | |
interactive=False, | |
visible=False, | |
) | |
gr_play.click( | |
fact_checking, | |
inputs=[gr_text,gr_mode], | |
outputs=[gr_output, gr_ft, gr_qa, gr_citations, gr_image], | |
) | |
demo.queue(default_concurrency_limit=1) | |
demo.launch(auth=(os.getenv("GRADIO_USERNAME"), os.getenv("GRADIO_PASSWORD"))) | |