File size: 4,926 Bytes
74c5b5d
2e94ecb
 
 
74c5b5d
2e94ecb
 
74c5b5d
2e94ecb
74c5b5d
2e94ecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74c5b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dde2508
74c5b5d
 
2e94ecb
 
 
 
 
 
 
 
 
 
74c5b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e94ecb
74c5b5d
 
 
 
6699c4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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")))