fact-check / app.py
cyberandy's picture
Update app.py
2eb216c verified
import streamlit as st
import requests
import json
import os
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define fact_check_statement function here...
def fact_check_statement(query, api_key):
url = "https://api.wordlift.io/fact-check/score"
payload = json.dumps({"query": query})
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Key " + api_key,
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
logger.info(
f"Request made to API: {url} with payload: {payload} and headers: {headers}"
)
if response.status_code == 200:
return response.json()
else:
logger.error(
f"API request failed with status code {response.status_code} and response: {response.text}"
)
return {
"error": f"Failed to get response from the fact-checking API. Status code: {response.status_code}"
}
except Exception as e:
logger.error(f"Exception during API request: {e}")
return {"error": f"Exception during API request: {str(e)}"}
# Page config
st.set_page_config(
page_title="AI Fact-Checking by WordLift",
page_icon="fav-ico.png",
layout="wide",
initial_sidebar_state="collapsed",
menu_items={
'Get Help': 'https://wordlift.io/book-a-demo/',
'About': "# This is a demo app for AI Fact-Checking"
}
)
# Sidebar
st.sidebar.image("logo-wordlift.png")
st.sidebar.markdown("""
- βœ… AI Assistance with Immediate Fact Verification
- βœ… Prevention of AI-Generated Misinformation
""")
# Main content
with st.form(key='my_form'):
text_input = st.text_area(label='Enter a statement for fact-checking')
submit_button = st.form_submit_button(label='Check Claim')
if submit_button and text_input:
api_key = os.environ.get('WL_KEY', 'default_key') # Use a default or handle the case where the key is not set
api_response = fact_check_statement(text_input, api_key)
if "error" not in api_response:
# Extract the 'response' string from api_response
response_str = api_response.get("response", "{}")
# Remove the triple backticks from the response string if they exist
response_str = response_str.strip('`')
# Parse the string as JSON
try:
inner_json = json.loads(response_str)
# Now, extract the information from inner_json as before
claim_reviewed = inner_json.get("claimReviewed", "N/A")
review_rating = inner_json.get("reviewRating", {})
rating_value = review_rating.get("ratingValue", "N/A")
alternate_name = review_rating.get("alternateName", "N/A")
review_body = inner_json.get("reviewBody", "N/A")
# Display the results
if rating_value.isdigit():
st.progress(int(rating_value) / 5, "Fact-checking in progress.")
else:
st.write("Rating Value: ", rating_value)
st.write(f"**Review Body**: {review_body}")
# Create a two-column layout
col1, col2 = st.columns(2)
# Use the metric widget to display the rating and alternate name
col1.metric("Rating", rating_value)
col2.metric("Verdict", alternate_name)
#with st.expander("Here is the final JSON-LD"):
# st.json(api_response) # Display the entire JSON-LD data
except json.JSONDecodeError as e:
st.error(f"Error decoding JSON: {e}")
else:
st.error("Error in fact-checking: " + api_response['error'])