Iker commited on
Commit
2e94ecb
·
verified ·
1 Parent(s): dde2508

Upload factchecking_front.py

Browse files
Files changed (1) hide show
  1. factchecking_front.py +59 -8
factchecking_front.py CHANGED
@@ -1,14 +1,55 @@
1
  import gradio as gr
2
- from gradio_client import Client
3
- import os
 
4
 
5
- client = Client("Iker/FactChecking-Backend")
 
6
 
 
7
 
8
- def fact_checking(
9
- article_topic: str,
10
- ):
11
- result = client.predict(article_topic=article_topic, api_name="/partial")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  return result
14
 
@@ -53,6 +94,16 @@ if __name__ == "__main__":
53
  placeholder="Los coches electricos contaminan más que los coches de gasolina",
54
  )
55
 
 
 
 
 
 
 
 
 
 
 
56
  gr_play = gr.Button("Fact-Checking")
57
 
58
  gr_output = gr.Markdown(
@@ -93,7 +144,7 @@ if __name__ == "__main__":
93
 
94
  gr_play.click(
95
  fact_checking,
96
- inputs=[gr_text],
97
  outputs=[gr_output, gr_ft, gr_qa, gr_citations, gr_image],
98
  )
99
 
 
1
  import gradio as gr
2
+ import os
3
+ import requests
4
+ import time
5
 
6
+ API_URL = "https://Iker-FactChecking-Backend.hf.space:7860" # Replace with your server's URL if different
7
+ API_KEY = os.getenv("API_KEY")
8
 
9
+ headers = {"X-API-Key": API_KEY, "Content-Type": "application/json"}
10
 
11
+
12
+ # Function to submit a fact-checking request
13
+ def submit_fact_check(article_topic, config):
14
+ endpoint = f"{API_URL}/fact-check"
15
+ payload = {"article_topic": article_topic, "config": config}
16
+
17
+ response = requests.post(endpoint, json=payload, headers=headers)
18
+ response.raise_for_status() # Raise an exception for HTTP errors
19
+ return response.json()["job_id"]
20
+
21
+
22
+ # Function to get the result of a fact-checking job
23
+ def get_fact_check_result(job_id):
24
+ endpoint = f"{API_URL}/result/{job_id}"
25
+
26
+ response = requests.get(endpoint, headers=headers)
27
+ response.raise_for_status() # Raise an exception for HTTP errors
28
+ return response.json()
29
+
30
+
31
+ def fact_checking(article_topic: str, config="pro"):
32
+ try:
33
+ job_id = submit_fact_check(article_topic, config.lower())
34
+ print(f"Fact-checking job submitted. Job ID: {job_id}")
35
+
36
+ # Poll for results
37
+ while True:
38
+ result = get_fact_check_result(job_id)
39
+ if result["status"] == "completed":
40
+ print("Fact-checking completed:")
41
+ print(result["result"])
42
+ break
43
+ elif result["status"] == "failed":
44
+ print("Fact-checking failed:")
45
+ print(result["error"])
46
+ break
47
+ else:
48
+ print("Fact-checking in progress. Waiting...")
49
+ time.sleep(2) # Wait for 2 seconds before checking again
50
+
51
+ except requests.exceptions.RequestException as e:
52
+ print(f"An error occurred: {e}")
53
 
54
  return result
55
 
 
94
  placeholder="Los coches electricos contaminan más que los coches de gasolina",
95
  )
96
 
97
+ gr_mode = gr.Radio(
98
+ label="Fact-Checking Mode",
99
+ info="Choose the fact-checking mode. The \"Turbo\" mode is faster and cheaper. The \"Pro\" mode is more accurate but more expensive.",
100
+ choices=["Pro", "Turbo"],
101
+ type="value",
102
+ default="Pro",
103
+ show_label=False,
104
+ interactive=True,
105
+ )
106
+
107
  gr_play = gr.Button("Fact-Checking")
108
 
109
  gr_output = gr.Markdown(
 
144
 
145
  gr_play.click(
146
  fact_checking,
147
+ inputs=[gr_text,gr_mode],
148
  outputs=[gr_output, gr_ft, gr_qa, gr_citations, gr_image],
149
  )
150