Spaces:
Running
Running
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") | |
# 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 Facts') | |
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: | |
# Parse the outer JSON | |
inner_json_str = api_response.get("response", "{}") | |
# Parse the inner JSON string | |
inner_json = json.loads(inner_json_str) | |
# Extract relevant information | |
claim_reviewed = inner_json.get("claimReviewed", "N/A") | |
review_rating = inner_json.get("reviewRating", {}) | |
rating_value = review_rating.get("ratingValue", "N/A") | |
review_body = inner_json.get("reviewBody", "N/A") | |
# Display the results | |
st.write(f"Claim Reviewed: {claim_reviewed}") | |
st.write(f"Review Body: {review_body}") | |
st.json(inner_json) # Display the entire JSON-LD data | |
# Assuming the rating value is a number between 1 and 5 | |
if rating_value.isdigit(): | |
st.progress(int(rating_value) / 5) | |
else: | |
st.write("Rating Value: ", rating_value) | |
else: | |
st.error("Error in fact-checking: " + api_response['error']) |