File size: 3,796 Bytes
db86b6a
 
 
e7c6db7
96d2717
 
 
 
 
db86b6a
5a3b7f8
db86b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1771ae
db86b6a
 
95502e6
 
 
 
db86b6a
 
 
 
fabfabe
db86b6a
 
e7c6db7
db86b6a
 
c207765
 
 
44183dc
c207765
 
41de54f
c207765
 
 
41de54f
c207765
 
 
 
 
 
62cc121
c207765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eb216c
c207765
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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'])