File size: 2,885 Bytes
db86b6a
 
 
e7c6db7
db86b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7c6db7
db86b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import json
import os

# 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'])