Spaces:
Sleeping
Sleeping
create first release
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Define fact_check_statement function here...
|
6 |
+
def fact_check_statement(query, api_key):
|
7 |
+
url = "https://api.wordlift.io/fact-check/score"
|
8 |
+
|
9 |
+
payload = json.dumps({"query": query})
|
10 |
+
headers = {
|
11 |
+
"Content-Type": "application/json",
|
12 |
+
"Accept": "application/json",
|
13 |
+
"Authorization": "Key " + api_key,
|
14 |
+
}
|
15 |
+
|
16 |
+
try:
|
17 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
18 |
+
logger.info(
|
19 |
+
f"Request made to API: {url} with payload: {payload} and headers: {headers}"
|
20 |
+
)
|
21 |
+
if response.status_code == 200:
|
22 |
+
return response.json()
|
23 |
+
else:
|
24 |
+
logger.error(
|
25 |
+
f"API request failed with status code {response.status_code} and response: {response.text}"
|
26 |
+
)
|
27 |
+
return {
|
28 |
+
"error": f"Failed to get response from the fact-checking API. Status code: {response.status_code}"
|
29 |
+
}
|
30 |
+
except Exception as e:
|
31 |
+
logger.error(f"Exception during API request: {e}")
|
32 |
+
return {"error": f"Exception during API request: {str(e)}"}
|
33 |
+
|
34 |
+
# Page config
|
35 |
+
st.set_page_config(
|
36 |
+
page_title="AI Fact-Checking by WordLift",
|
37 |
+
page_icon="fav-ico.png",
|
38 |
+
layout="wide",
|
39 |
+
initial_sidebar_state="collapsed",
|
40 |
+
menu_items={
|
41 |
+
'Get Help': 'https://wordlift.io/book-a-demo/',
|
42 |
+
'About': "# This is a demo app for AI Fact-Checking"
|
43 |
+
}
|
44 |
+
)
|
45 |
+
|
46 |
+
# Sidebar
|
47 |
+
st.sidebar.image("logo-wordlift.png")
|
48 |
+
|
49 |
+
# Main content
|
50 |
+
with st.form(key='my_form'):
|
51 |
+
text_input = st.text_area(label='Enter a statement for fact-checking')
|
52 |
+
submit_button = st.form_submit_button(label='Check Facts')
|
53 |
+
|
54 |
+
if submit_button and text_input:
|
55 |
+
api_key = "your_api_key" # Replace with your actual API key
|
56 |
+
api_response = fact_check_statement(text_input, api_key)
|
57 |
+
|
58 |
+
if "error" not in api_response:
|
59 |
+
# Parse the outer JSON
|
60 |
+
inner_json_str = api_response.get("response", "{}")
|
61 |
+
# Parse the inner JSON string
|
62 |
+
inner_json = json.loads(inner_json_str)
|
63 |
+
|
64 |
+
# Extract relevant information
|
65 |
+
claim_reviewed = inner_json.get("claimReviewed", "N/A")
|
66 |
+
review_rating = inner_json.get("reviewRating", {})
|
67 |
+
rating_value = review_rating.get("ratingValue", "N/A")
|
68 |
+
review_body = inner_json.get("reviewBody", "N/A")
|
69 |
+
|
70 |
+
# Display the results
|
71 |
+
st.write(f"Claim Reviewed: {claim_reviewed}")
|
72 |
+
st.write(f"Review Body: {review_body}")
|
73 |
+
st.json(inner_json) # Display the entire JSON-LD data
|
74 |
+
|
75 |
+
# Assuming the rating value is a number between 1 and 5
|
76 |
+
if rating_value.isdigit():
|
77 |
+
st.progress(int(rating_value) / 5)
|
78 |
+
else:
|
79 |
+
st.write("Rating Value: ", rating_value)
|
80 |
+
else:
|
81 |
+
st.error("Error in fact-checking: " + api_response['error'])
|