Hasan Iqbal
commited on
Commit
•
5f0301f
1
Parent(s):
a753e9f
Added initial skeleton
Browse files- .gitignore +19 -0
- .python-version +1 -0
- configs/devhasaniqbal.json +3 -0
- scripts/cli.sh +14 -0
- scripts/common.sh +62 -0
- src/openfactcheck/__init__.py +2 -0
- src/openfactcheck/core/__init__.py +1 -0
- src/openfactcheck/core/base.py +13 -0
- src/openfactcheck/core/cli.py +24 -0
- src/openfactcheck/core/fact_check_state.py +49 -0
- src/openfactcheck/core/standard_task_solver.py +45 -0
- src/openfactcheck/lib/__init__.py +2 -0
- src/openfactcheck/lib/config.py +30 -0
- src/openfactcheck/lib/logger.py +38 -0
- src/openfactcheck/lib/openai.py +0 -0
.gitignore
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# MacOS
|
2 |
+
.DS_Store
|
3 |
+
|
4 |
+
# Terraform Specific
|
5 |
+
**/.terraform/*
|
6 |
+
ignore.*.tfvars
|
7 |
+
|
8 |
+
# IDE specific
|
9 |
+
.idea/
|
10 |
+
.vscode/
|
11 |
+
|
12 |
+
# Project Specific
|
13 |
+
DEVNOTES
|
14 |
+
tmp/
|
15 |
+
|
16 |
+
# Python Specific
|
17 |
+
.venv/
|
18 |
+
__pycache__/
|
19 |
+
*.pyc
|
.python-version
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
3.11.9
|
configs/devhasaniqbal.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"hello": "world"
|
3 |
+
}
|
scripts/cli.sh
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# Script for running openfactcheck
|
4 |
+
#
|
5 |
+
# Examples
|
6 |
+
# --------
|
7 |
+
# $ ./run.sh
|
8 |
+
#
|
9 |
+
|
10 |
+
source "${BASH_SOURCE%/*}/common.sh"
|
11 |
+
|
12 |
+
# Executing Python script
|
13 |
+
export PYTHONPATH="$PYTHONPATH:src/"
|
14 |
+
python src/openfactcheck/core/cli.py "$@"
|
scripts/common.sh
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
################################################################################
|
4 |
+
# Shell dependencies #
|
5 |
+
################################################################################
|
6 |
+
# https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script
|
7 |
+
|
8 |
+
if ! [ -x "$(command -v git)" ]; then
|
9 |
+
echo 'Error: git is not installed.' >&2
|
10 |
+
exit 1
|
11 |
+
fi
|
12 |
+
|
13 |
+
################################################################################
|
14 |
+
# Common Functions #
|
15 |
+
################################################################################
|
16 |
+
|
17 |
+
# Echo's the text to the screen with the designated color
|
18 |
+
c_echo () {
|
19 |
+
local color=$1
|
20 |
+
local txt=$2
|
21 |
+
|
22 |
+
echo -e "${color}${txt}${NC}"
|
23 |
+
}
|
24 |
+
|
25 |
+
# Enforces you are running from project root
|
26 |
+
force_project_root () {
|
27 |
+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
28 |
+
PARENT_DIR=$(dirname $DIR)
|
29 |
+
|
30 |
+
if [ "$(pwd)" != $PARENT_DIR ]
|
31 |
+
then
|
32 |
+
c_echo $RED "You must be in $PARENT_DIR to run"
|
33 |
+
exit 1
|
34 |
+
fi
|
35 |
+
}
|
36 |
+
|
37 |
+
################################################################################
|
38 |
+
# Color Constants #
|
39 |
+
################################################################################
|
40 |
+
GREEN='\033[0;32m'
|
41 |
+
YELLOW='\033[0;33m'
|
42 |
+
RED='\033[0;31m'
|
43 |
+
NC='\033[0m'
|
44 |
+
|
45 |
+
################################################################################
|
46 |
+
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ENFORCE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
|
47 |
+
################################################################################
|
48 |
+
force_project_root
|
49 |
+
|
50 |
+
################################################################################
|
51 |
+
# Application Versioning Information #
|
52 |
+
################################################################################
|
53 |
+
APP_NAME=openfactcheck
|
54 |
+
APP_VERSION=$(git describe --tags --dirty) || exit 1
|
55 |
+
COMMIT=$(git rev-parse HEAD)
|
56 |
+
|
57 |
+
################################################################################
|
58 |
+
# Common Paths #
|
59 |
+
################################################################################
|
60 |
+
BUILDBIN_PATH=$(pwd)/tmp/build/bin
|
61 |
+
DEPLOYMENTS_PATH=$(pwd)/deployments
|
62 |
+
TERRAFORM_PATH=$(pwd)/deployments/terraform
|
src/openfactcheck/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
from .lib.config import OpenFactCheckConfig
|
2 |
+
from .core.base import OpenFactCheck
|
src/openfactcheck/core/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .base import OpenFactCheck
|
src/openfactcheck/core/base.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openfactcheck.lib.logger import logger
|
2 |
+
from openfactcheck.lib.config import OpenFactCheckConfig
|
3 |
+
|
4 |
+
class OpenFactCheck:
|
5 |
+
def __init__(self, config: OpenFactCheckConfig):
|
6 |
+
self.logger = logger
|
7 |
+
self.config = config
|
8 |
+
|
9 |
+
self.logger.info("OpenFactCheck initialized")
|
10 |
+
|
11 |
+
if __name__ == "__main__":
|
12 |
+
config = OpenFactCheckConfig()
|
13 |
+
ofc = OpenFactCheck(config)
|
src/openfactcheck/core/cli.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
|
3 |
+
from openfactcheck.core.base import OpenFactCheck
|
4 |
+
from openfactcheck.lib.config import OpenFactCheckConfig
|
5 |
+
|
6 |
+
def parse_args():
|
7 |
+
parser = argparse.ArgumentParser(description='Initialize OpenFactCheck with custom configuration.')
|
8 |
+
|
9 |
+
# Add arguments here, example:
|
10 |
+
parser.add_argument("--config-path",
|
11 |
+
type=str,
|
12 |
+
help="Config File Path",
|
13 |
+
default="config.json")
|
14 |
+
|
15 |
+
# Parse arguments from command line
|
16 |
+
args = parser.parse_args()
|
17 |
+
return args
|
18 |
+
|
19 |
+
if __name__ == "__main__":
|
20 |
+
args = parse_args()
|
21 |
+
|
22 |
+
ofc = OpenFactCheck(OpenFactCheckConfig(args.config_path))
|
23 |
+
|
24 |
+
|
src/openfactcheck/core/fact_check_state.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
|
3 |
+
class FactCheckerState:
|
4 |
+
"""
|
5 |
+
A class to manage the state of a fact checking system. It holds a question
|
6 |
+
and its corresponding response, and provides methods to set and get these
|
7 |
+
attributes dynamically.
|
8 |
+
|
9 |
+
Parameters
|
10 |
+
----------
|
11 |
+
question : str
|
12 |
+
The question to be fact-checked.
|
13 |
+
response : str
|
14 |
+
The response to the question.
|
15 |
+
"""
|
16 |
+
def __init__(self, question: str = None, response: str = None):
|
17 |
+
"""
|
18 |
+
Initialize the FactCheckerState object.
|
19 |
+
"""
|
20 |
+
self.question: str = question
|
21 |
+
self.response: str = response
|
22 |
+
|
23 |
+
def set(self, name, value):
|
24 |
+
"""
|
25 |
+
Set an attribute of the state object.
|
26 |
+
"""
|
27 |
+
if hasattr(self, name):
|
28 |
+
logging.warning(f"FactCheckerState.set: Modifying existing attribute {name}")
|
29 |
+
setattr(self, name, value)
|
30 |
+
|
31 |
+
def get(self, name):
|
32 |
+
"""
|
33 |
+
Get an attribute of the state object.
|
34 |
+
"""
|
35 |
+
if not hasattr(self, name):
|
36 |
+
raise ValueError(f"FactCheckerState.get: Attribute {name} does not exist")
|
37 |
+
return getattr(self, name, None)
|
38 |
+
|
39 |
+
def __str__(self):
|
40 |
+
"""
|
41 |
+
Return a string representation of the state object.
|
42 |
+
"""
|
43 |
+
return str(self.__dict__)
|
44 |
+
|
45 |
+
def to_dict(self):
|
46 |
+
"""
|
47 |
+
Return a dictionary representation of the state object.
|
48 |
+
"""
|
49 |
+
return self.__dict__
|
src/openfactcheck/core/standard_task_solver.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
from typing import Tuple
|
3 |
+
|
4 |
+
from fact_check_state import FactCheckerState
|
5 |
+
|
6 |
+
class StandardTaskSolver:
|
7 |
+
"""
|
8 |
+
A class to represent a standard task solver. A standard task solver is a
|
9 |
+
class that implements a specific task in a fact-checking system. It
|
10 |
+
receives a FactCheckerState object as input and returns a new
|
11 |
+
FactCheckerState object as output.
|
12 |
+
|
13 |
+
Parameters
|
14 |
+
----------
|
15 |
+
args : dict
|
16 |
+
A dictionary containing the arguments to be passed to the solver.
|
17 |
+
"""
|
18 |
+
|
19 |
+
name: str = None
|
20 |
+
input_name: str = None
|
21 |
+
output_name: str = None
|
22 |
+
global_config: dict = dict()
|
23 |
+
|
24 |
+
def __init__(self, args: dict):
|
25 |
+
self.args = args
|
26 |
+
logging.debug(self.args)
|
27 |
+
|
28 |
+
def __call__(self, state: FactCheckerState, **kwargs) -> Tuple[
|
29 |
+
bool, FactCheckerState]:
|
30 |
+
raise NotImplementedError
|
31 |
+
|
32 |
+
@classmethod
|
33 |
+
def build_solver(cls, args):
|
34 |
+
raise NotImplementedError
|
35 |
+
|
36 |
+
@property
|
37 |
+
def input_name(self):
|
38 |
+
return self.__class__.input_name
|
39 |
+
|
40 |
+
@property
|
41 |
+
def output_name(self):
|
42 |
+
return self.__class__.output_name
|
43 |
+
|
44 |
+
def __str__(self):
|
45 |
+
return f'[name:"{self.__class__.name}", input: "{self.__class__.input_name}": output: "{self.__class__.output_name}"]'
|
src/openfactcheck/lib/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
from .config import OpenFactCheckConfig
|
2 |
+
from .logger import logger
|
src/openfactcheck/lib/config.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from pathlib import Path
|
3 |
+
from typing import Union
|
4 |
+
|
5 |
+
from .logger import logger
|
6 |
+
|
7 |
+
class OpenFactCheckConfig:
|
8 |
+
def __init__(self, filename: Union[str, Path] = "config.json"):
|
9 |
+
# Setup Logger
|
10 |
+
self.logger = logger
|
11 |
+
|
12 |
+
self.filename = filename
|
13 |
+
|
14 |
+
try:
|
15 |
+
# Loading Config File
|
16 |
+
with open(self.filename, encoding="utf-8") as file:
|
17 |
+
self.filename = json.load(file)
|
18 |
+
|
19 |
+
except FileNotFoundError:
|
20 |
+
self.logger.error(f"Config file not found: {self.filename}")
|
21 |
+
raise FileNotFoundError(f"Config file not found: {self.filename}")
|
22 |
+
|
23 |
+
except json.JSONDecodeError:
|
24 |
+
self.logger.error(f"Invalid JSON in config file: {self.filename}")
|
25 |
+
raise ValueError(f"Invalid JSON in config file: {self.filename}")
|
26 |
+
|
27 |
+
except Exception as e:
|
28 |
+
self.logger.error(f"Error loading config file: {e}")
|
29 |
+
raise Exception(f"Error loading config file: {e}")
|
30 |
+
|
src/openfactcheck/lib/logger.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
|
3 |
+
def get_logger():
|
4 |
+
"""
|
5 |
+
This function returns a logger object that can be used to log messages
|
6 |
+
to the console and a file.
|
7 |
+
"""
|
8 |
+
|
9 |
+
# Console Logger
|
10 |
+
console_formatter = logging.Formatter('%(levelname)s -- %(message)s')
|
11 |
+
console_handler = logging.StreamHandler()
|
12 |
+
console_handler.setLevel(logging.DEBUG)
|
13 |
+
console_handler.setFormatter(console_formatter)
|
14 |
+
|
15 |
+
# File Logger
|
16 |
+
# file_formatter = logging.Formatter(
|
17 |
+
# '%(asctime)s — %(levelname)s — %(funcName)s:%(lineno)d — %(message)s', datefmt='%m-%d-%Y %H:%M:%S')
|
18 |
+
# file_handler = logging.FileHandler("lambda.log")
|
19 |
+
# file_handler.setLevel(logging.DEBUG)
|
20 |
+
# file_handler.setFormatter(file_formatter)
|
21 |
+
|
22 |
+
# Getting the root logger
|
23 |
+
newlogger = logging.getLogger(__name__)
|
24 |
+
|
25 |
+
# Adding the handlers
|
26 |
+
# logger.addHandler(file_handler)
|
27 |
+
newlogger.addHandler(console_handler)
|
28 |
+
|
29 |
+
# Setting the level
|
30 |
+
newlogger.setLevel(logging.DEBUG)
|
31 |
+
|
32 |
+
# Preventing the loggers from propagating to the root logger
|
33 |
+
newlogger.propagate = False
|
34 |
+
|
35 |
+
return newlogger
|
36 |
+
|
37 |
+
|
38 |
+
logger = get_logger()
|
src/openfactcheck/lib/openai.py
ADDED
File without changes
|