Hasan Iqbal
commited on
Added Dialog to add API Keys
Browse files
src/openfactcheck/app/app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import argparse
|
| 2 |
import streamlit as st
|
| 3 |
from streamlit_option_menu import option_menu
|
|
@@ -5,10 +6,10 @@ from streamlit_option_menu import option_menu
|
|
| 5 |
from openfactcheck import OpenFactCheck
|
| 6 |
from openfactcheck.lib import OpenFactCheckConfig
|
| 7 |
from openfactcheck.app.sidebar import sidebar
|
|
|
|
| 8 |
from openfactcheck.app.evaluate_response import evaluate_response
|
| 9 |
from openfactcheck.app.evaluate_llm import evaluate_llm
|
| 10 |
from openfactcheck.app.evaluate_factchecker import evaluate_factchecker
|
| 11 |
-
from openfactcheck.app.utils import footer
|
| 12 |
|
| 13 |
def parse_args():
|
| 14 |
parser = argparse.ArgumentParser(description='Initialize OpenFactCheck with custom configuration.')
|
|
@@ -25,26 +26,35 @@ def parse_args():
|
|
| 25 |
|
| 26 |
class App:
|
| 27 |
def __init__(self, config_path: str = "config.json"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@st.cache_resource(show_spinner=False)
|
| 29 |
def init(config_path: str = "config.json"):
|
| 30 |
-
# Initialize
|
| 31 |
config = OpenFactCheckConfig(config_path)
|
| 32 |
ofc = OpenFactCheck(config)
|
|
|
|
| 33 |
return ofc
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def run(self):
|
| 39 |
-
# Set up Dashboard
|
| 40 |
-
st.set_page_config(page_title="OpenFactCheck Dashboard",
|
| 41 |
-
page_icon=":bar_chart:",
|
| 42 |
-
layout="wide",
|
| 43 |
-
initial_sidebar_state="collapsed")
|
| 44 |
-
|
| 45 |
-
# # Set up footer
|
| 46 |
-
# footer("Copyright © 2024 MBZUAI | Made with ❤︎ by OpenFactCheck Team")
|
| 47 |
-
|
| 48 |
# Set up Sidebar
|
| 49 |
sidebar()
|
| 50 |
|
|
@@ -60,7 +70,10 @@ class App:
|
|
| 60 |
orientation="horizontal"
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Load the selected page
|
| 66 |
if selected == "Evaluate LLM Response":
|
|
|
|
| 1 |
+
import os
|
| 2 |
import argparse
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit_option_menu import option_menu
|
|
|
|
| 6 |
from openfactcheck import OpenFactCheck
|
| 7 |
from openfactcheck.lib import OpenFactCheckConfig
|
| 8 |
from openfactcheck.app.sidebar import sidebar
|
| 9 |
+
from openfactcheck.app.dialogs import get_secrets
|
| 10 |
from openfactcheck.app.evaluate_response import evaluate_response
|
| 11 |
from openfactcheck.app.evaluate_llm import evaluate_llm
|
| 12 |
from openfactcheck.app.evaluate_factchecker import evaluate_factchecker
|
|
|
|
| 13 |
|
| 14 |
def parse_args():
|
| 15 |
parser = argparse.ArgumentParser(description='Initialize OpenFactCheck with custom configuration.')
|
|
|
|
| 26 |
|
| 27 |
class App:
|
| 28 |
def __init__(self, config_path: str = "config.json"):
|
| 29 |
+
# Set up Dashboard
|
| 30 |
+
st.set_page_config(page_title="OpenFactCheck Dashboard",
|
| 31 |
+
page_icon=":bar_chart:",
|
| 32 |
+
layout="wide",
|
| 33 |
+
initial_sidebar_state="collapsed")
|
| 34 |
+
|
| 35 |
+
# Get API Keys
|
| 36 |
+
st.session_state.api_keys = False
|
| 37 |
+
# Check if the API keys are already set in the environment variables
|
| 38 |
+
if os.getenv("OPENAI_API_KEY") and os.getenv("SERPER_API_KEY") and os.getenv("SCRAPER_API_KEY"):
|
| 39 |
+
st.session_state.api_keys = True
|
| 40 |
+
else:
|
| 41 |
+
get_secrets()
|
| 42 |
+
|
| 43 |
+
# Initialize OpenFactCheck
|
| 44 |
@st.cache_resource(show_spinner=False)
|
| 45 |
def init(config_path: str = "config.json"):
|
| 46 |
+
# Initialize OpenFactCheckConfig
|
| 47 |
config = OpenFactCheckConfig(config_path)
|
| 48 |
ofc = OpenFactCheck(config)
|
| 49 |
+
|
| 50 |
return ofc
|
| 51 |
|
| 52 |
+
if st.session_state.api_keys:
|
| 53 |
+
self.ofc = init(config_path)
|
| 54 |
+
else:
|
| 55 |
+
self.ofc = None
|
| 56 |
|
| 57 |
def run(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Set up Sidebar
|
| 59 |
sidebar()
|
| 60 |
|
|
|
|
| 70 |
orientation="horizontal"
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Check if API keys are set
|
| 74 |
+
if not st.session_state.api_keys:
|
| 75 |
+
st.warning("Please provide the necessary API keys to proceed.")
|
| 76 |
+
return
|
| 77 |
|
| 78 |
# Load the selected page
|
| 79 |
if selected == "Evaluate LLM Response":
|
src/openfactcheck/app/dialogs.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
@st.dialog("Secrets Configuration")
|
| 5 |
+
def get_secrets():
|
| 6 |
+
st.markdown("Please provide the following keys to evaluate LLM response:")
|
| 7 |
+
st.info("Keys defined in your environment variables are automatically loaded.")
|
| 8 |
+
|
| 9 |
+
# OpenAI API Key
|
| 10 |
+
st.markdown("You can find the OpenAI API Key [here](https://platform.openai.com/account/api-keys).")
|
| 11 |
+
openai_api_key = st.text_input("OpenAI API Key", type="password", value=os.getenv("OPENAI_API_KEY", default=""))
|
| 12 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
| 13 |
+
|
| 14 |
+
# Serper API Key
|
| 15 |
+
st.markdown("You can find the Serper API Key [here](https://serpapi.com/dashboard).")
|
| 16 |
+
serper_api_key = st.text_input("Serper API Key", type="password", value=os.getenv("SERPER_API_KEY", default=""))
|
| 17 |
+
os.environ["SERPER_API_KEY"] = serper_api_key
|
| 18 |
+
|
| 19 |
+
# ScraperAPI Key
|
| 20 |
+
st.markdown("You can find the ScraperAPI Key [here](https://www.scraperapi.com/).")
|
| 21 |
+
scraper_api_key = st.text_input("ScraperAPI Key", type="password", value=os.getenv("SCRAPER_API_KEY", default=""))
|
| 22 |
+
os.environ["SCRAPER_API_KEY"] = scraper_api_key
|
| 23 |
+
|
| 24 |
+
if st.button("Submit"):
|
| 25 |
+
st.session_state.api_keys = True
|
| 26 |
+
st.rerun()
|
src/openfactcheck/app/evaluate_response.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import re
|
| 2 |
import time
|
| 3 |
import pandas as pd
|
|
|
|
| 1 |
+
import os
|
| 2 |
import re
|
| 3 |
import time
|
| 4 |
import pandas as pd
|
src/openfactcheck/app/sidebar.py
CHANGED
|
@@ -8,15 +8,15 @@ def sidebar():
|
|
| 8 |
|
| 9 |
# OpenAI API Key
|
| 10 |
st.sidebar.markdown("You can find the OpenAI API Key [here](https://platform.openai.com/account/api-keys).")
|
| 11 |
-
openai_api_key = st.sidebar.text_input("OpenAI API Key", key="openai_key", type="password", value=os.getenv("OPENAI_API_KEY"))
|
| 12 |
os.environ["OPENAI_API_KEY"] = openai_api_key
|
| 13 |
|
| 14 |
# Serper API Key
|
| 15 |
st.sidebar.markdown("You can find the Serper API Key [here](https://serpapi.com/dashboard).")
|
| 16 |
-
serper_api_key = st.sidebar.text_input("Serper API Key", key="serper_key", type="password", value=os.getenv("SERPER_API_KEY"))
|
| 17 |
os.environ["SERPER_API_KEY"] = serper_api_key
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
st.sidebar.markdown("You can find the
|
| 21 |
-
|
| 22 |
-
os.environ["
|
|
|
|
| 8 |
|
| 9 |
# OpenAI API Key
|
| 10 |
st.sidebar.markdown("You can find the OpenAI API Key [here](https://platform.openai.com/account/api-keys).")
|
| 11 |
+
openai_api_key = st.sidebar.text_input("OpenAI API Key", key="openai_key", type="password", value=os.getenv("OPENAI_API_KEY", default=""))
|
| 12 |
os.environ["OPENAI_API_KEY"] = openai_api_key
|
| 13 |
|
| 14 |
# Serper API Key
|
| 15 |
st.sidebar.markdown("You can find the Serper API Key [here](https://serpapi.com/dashboard).")
|
| 16 |
+
serper_api_key = st.sidebar.text_input("Serper API Key", key="serper_key", type="password", value=os.getenv("SERPER_API_KEY", default=""))
|
| 17 |
os.environ["SERPER_API_KEY"] = serper_api_key
|
| 18 |
|
| 19 |
+
# ScraperAPI Key
|
| 20 |
+
st.sidebar.markdown("You can find the ScraperAPI Key [here](https://www.scraperapi.com/).")
|
| 21 |
+
scraper_api_key = st.sidebar.text_input("ScraperAPI Key", key="scraper_key", type="password", value=os.getenv("SCRAPER_API_KEY", default=""))
|
| 22 |
+
os.environ["SCRAPER_API_KEY"] = scraper_api_key
|
src/openfactcheck/app/utils.py
CHANGED
|
@@ -37,36 +37,4 @@ def metric_card(
|
|
| 37 |
</div>
|
| 38 |
"""
|
| 39 |
st.markdown(html_str, unsafe_allow_html=True)
|
| 40 |
-
st.markdown('######')
|
| 41 |
-
|
| 42 |
-
def footer(text: str) -> None:
|
| 43 |
-
|
| 44 |
-
style = f"""
|
| 45 |
-
<style>
|
| 46 |
-
#MainMenu {{visibility: hidden;}}
|
| 47 |
-
footer {{visibility: hidden;}}
|
| 48 |
-
.stApp {{ bottom: 52px; }}
|
| 49 |
-
</style>
|
| 50 |
-
"""
|
| 51 |
-
|
| 52 |
-
st.markdown(style, unsafe_allow_html=True)
|
| 53 |
-
|
| 54 |
-
html_str = f"""
|
| 55 |
-
<div style='position: fixed;
|
| 56 |
-
left: 0;
|
| 57 |
-
bottom: 0;
|
| 58 |
-
margin: 0 0 0 0;
|
| 59 |
-
width: 100%;
|
| 60 |
-
color: black;
|
| 61 |
-
text-align: center;
|
| 62 |
-
height: auto;
|
| 63 |
-
opacity: 1'>
|
| 64 |
-
<hr style='display: block;
|
| 65 |
-
margin: 8px 8px 8px 8px;
|
| 66 |
-
border-style: inset;
|
| 67 |
-
border-width: 1px' />
|
| 68 |
-
<p>{text}</p>
|
| 69 |
-
</div>
|
| 70 |
-
"""
|
| 71 |
-
|
| 72 |
-
st.markdown(html_str, unsafe_allow_html=True)
|
|
|
|
| 37 |
</div>
|
| 38 |
"""
|
| 39 |
st.markdown(html_str, unsafe_allow_html=True)
|
| 40 |
+
st.markdown('######')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/openfactcheck/lib/config.py
CHANGED
|
@@ -9,6 +9,7 @@ from collections import namedtuple
|
|
| 9 |
from importlib import resources as pkg_resources
|
| 10 |
|
| 11 |
from openfactcheck.lib.logger import logger, set_logger_level
|
|
|
|
| 12 |
from openfactcheck.errors import ConfigValidationError
|
| 13 |
from openfactcheck import templates as solver_config_templates_dir
|
| 14 |
from openfactcheck import solvers as solver_templates_dir
|
|
@@ -70,7 +71,7 @@ class OpenFactCheckConfig:
|
|
| 70 |
# Define namedtuple structures
|
| 71 |
Secrets = namedtuple("Secrets", ["openai_api_key",
|
| 72 |
"serper_api_key",
|
| 73 |
-
"
|
| 74 |
|
| 75 |
# Define Attributes
|
| 76 |
self.config = None
|
|
@@ -150,18 +151,18 @@ class OpenFactCheckConfig:
|
|
| 150 |
if 'secrets' in self.config:
|
| 151 |
self.secrets = Secrets(openai_api_key=self.config['secrets']['openai_api_key'],
|
| 152 |
serper_api_key=self.config['secrets']['serper_api_key'],
|
| 153 |
-
|
| 154 |
else:
|
| 155 |
self.logger.warning("No secrets found in the configuration file. Make sure to set the environment variables.")
|
| 156 |
-
self.secrets = Secrets(openai_api_key=None, serper_api_key=None,
|
| 157 |
|
| 158 |
# Initialize Environment Variables
|
| 159 |
if self.secrets.openai_api_key:
|
| 160 |
os.environ['OPENAI_API_KEY'] = self.secrets.openai_api_key
|
| 161 |
if self.secrets.serper_api_key:
|
| 162 |
os.environ['SERPER_API_KEY'] = self.secrets.serper_api_key
|
| 163 |
-
if self.secrets.
|
| 164 |
-
os.environ['
|
| 165 |
|
| 166 |
# Initialize Verbose
|
| 167 |
if 'verbose' in self.config:
|
|
@@ -172,8 +173,9 @@ class OpenFactCheckConfig:
|
|
| 172 |
self.verbose = "INFO"
|
| 173 |
set_logger_level(self.logger, "INFO")
|
| 174 |
|
| 175 |
-
# Validate
|
| 176 |
-
|
|
|
|
| 177 |
|
| 178 |
# Disable Transformers and Datasets logging
|
| 179 |
transformers.logging.set_verbosity_error()
|
|
@@ -218,9 +220,9 @@ class OpenFactCheckConfig:
|
|
| 218 |
if 'SERPER_API_KEY' not in os.environ:
|
| 219 |
self.logger.warning("SERPER_API_KEY environment variable not found.")
|
| 220 |
raise ConfigValidationError("SERPER_API_KEY environment variable not found.")
|
| 221 |
-
if '
|
| 222 |
-
self.logger.warning("
|
| 223 |
-
raise ConfigValidationError("
|
| 224 |
|
| 225 |
|
| 226 |
|
|
|
|
| 9 |
from importlib import resources as pkg_resources
|
| 10 |
|
| 11 |
from openfactcheck.lib.logger import logger, set_logger_level
|
| 12 |
+
from openfactcheck.lib.utils import detect_streamlit
|
| 13 |
from openfactcheck.errors import ConfigValidationError
|
| 14 |
from openfactcheck import templates as solver_config_templates_dir
|
| 15 |
from openfactcheck import solvers as solver_templates_dir
|
|
|
|
| 71 |
# Define namedtuple structures
|
| 72 |
Secrets = namedtuple("Secrets", ["openai_api_key",
|
| 73 |
"serper_api_key",
|
| 74 |
+
"scraper_api_key"])
|
| 75 |
|
| 76 |
# Define Attributes
|
| 77 |
self.config = None
|
|
|
|
| 151 |
if 'secrets' in self.config:
|
| 152 |
self.secrets = Secrets(openai_api_key=self.config['secrets']['openai_api_key'],
|
| 153 |
serper_api_key=self.config['secrets']['serper_api_key'],
|
| 154 |
+
scraper_api_key=self.config['secrets']['scraper_api_key'])
|
| 155 |
else:
|
| 156 |
self.logger.warning("No secrets found in the configuration file. Make sure to set the environment variables.")
|
| 157 |
+
self.secrets = Secrets(openai_api_key=None, serper_api_key=None, scraper_api_key=None)
|
| 158 |
|
| 159 |
# Initialize Environment Variables
|
| 160 |
if self.secrets.openai_api_key:
|
| 161 |
os.environ['OPENAI_API_KEY'] = self.secrets.openai_api_key
|
| 162 |
if self.secrets.serper_api_key:
|
| 163 |
os.environ['SERPER_API_KEY'] = self.secrets.serper_api_key
|
| 164 |
+
if self.secrets.scraper_api_key:
|
| 165 |
+
os.environ['SCRAPER_API_KEY'] = self.secrets.scraper_api_key
|
| 166 |
|
| 167 |
# Initialize Verbose
|
| 168 |
if 'verbose' in self.config:
|
|
|
|
| 173 |
self.verbose = "INFO"
|
| 174 |
set_logger_level(self.logger, "INFO")
|
| 175 |
|
| 176 |
+
# Validate the configuration
|
| 177 |
+
if not detect_streamlit():
|
| 178 |
+
self.validate()
|
| 179 |
|
| 180 |
# Disable Transformers and Datasets logging
|
| 181 |
transformers.logging.set_verbosity_error()
|
|
|
|
| 220 |
if 'SERPER_API_KEY' not in os.environ:
|
| 221 |
self.logger.warning("SERPER_API_KEY environment variable not found.")
|
| 222 |
raise ConfigValidationError("SERPER_API_KEY environment variable not found.")
|
| 223 |
+
if 'SCRAPER_API_KEY' not in os.environ:
|
| 224 |
+
self.logger.warning("SCRAPER_API_KEY environment variable not found.")
|
| 225 |
+
raise ConfigValidationError("SCRAPER_API_KEY environment variable not found.")
|
| 226 |
|
| 227 |
|
| 228 |
|
src/openfactcheck/lib/utils.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def detect_streamlit() -> bool:
|
| 2 |
+
try:
|
| 3 |
+
from streamlit.runtime.scriptrunner import get_script_run_ctx
|
| 4 |
+
|
| 5 |
+
if get_script_run_ctx() is not None:
|
| 6 |
+
return True
|
| 7 |
+
else:
|
| 8 |
+
return False
|
| 9 |
+
|
| 10 |
+
except ImportError:
|
| 11 |
+
return False
|
| 12 |
+
|
| 13 |
+
except Exception:
|
| 14 |
+
return False
|
| 15 |
+
|