Skip to content

Commit

Permalink
Merge pull request #161 from ufal/add-public-mode
Browse files Browse the repository at this point in the history
Add public mode
  • Loading branch information
kasnerz authored Dec 2, 2024
2 parents dcbf53d + 5e6fadc commit 63df13c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 30 deletions.
45 changes: 25 additions & 20 deletions factgenie/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from slugify import slugify
import logging
import coloredlogs
import traceback
import factgenie.workflows as workflows

from factgenie import CAMPAIGN_DIR
Expand Down Expand Up @@ -225,26 +226,30 @@ def compute_extra_fields_stats(example_index):
# compute aggregate statistics for flags, options and text_fields (aggregates of value counts for each label)
extra_fields_stats = {}

for field in ["flags", "options", "text_fields"]:
# each of `example_index[field]` is a list of dicts
# each dict contains `label` and `value` keys
# we want to count the number of occurrences of each `value` for each unique `label`
# and then assign the dictionary with these counts to extra_fields_stats[label]

# find unique labels
labels = set()
for example in example_index[field]:
for d in example:
labels.add(d["label"])

# create a dictionary for each label
for label in labels:
extra_fields_stats[label] = defaultdict(int)

# count the occurrences of each value for each label
for example in example_index[field]:
for d in example:
extra_fields_stats[d["label"]][d["value"]] += 1
try:
for field in ["flags", "options", "text_fields"]:
# each of `example_index[field]` is a list of dicts
# each dict contains `label` and `value` keys
# we want to count the number of occurrences of each `value` for each unique `label`
# and then assign the dictionary with these counts to extra_fields_stats[label]

# find unique labels
labels = set()
for example in example_index[field]:
for d in example:
labels.add(d["label"])

# create a dictionary for each label
for label in labels:
extra_fields_stats[label] = defaultdict(int)

# count the occurrences of each value for each label
for example in example_index[field]:
for d in example:
extra_fields_stats[d["label"]][d["value"]] += 1
except Exception as e:
logger.error(f"Error while computing extra fields statistics: {e}")
traceback.print_exc()

return extra_fields_stats

Expand Down
22 changes: 21 additions & 1 deletion factgenie/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,25 @@ def prettify_json(value):
# Decorators
# -----------------
# Very simple decorator to protect routes


def is_view_allowed(path):
# if view pages are locked, do not allow any page
if app.config["login"].get("lock_view_pages", True):
return False

# otherwise allow to view the main page, browse and analyze pages
if path == "/" or path.startswith("/browse") or path.startswith("/analyze"):
return True

# and lock the rest of pages
return False


def login_required(f):
def wrapper(*args, **kwargs):
if app.config["login"]["active"]:
# the browse/analyze pages are allowed without login
if app.config["login"]["active"] and not is_view_allowed(request.path):
auth = request.cookies.get("auth")
if not auth:
return redirect(app.config["host_prefix"] + "/login")
Expand All @@ -117,9 +133,13 @@ def wrapper(*args, **kwargs):
@login_required
def index():
logger.info(f"Main page loaded")

dim_unaccessible_pages = app.config["login"]["active"] and app.config["login"].get("lock_view_pages") == False

return render_template(
"pages/index.html",
host_prefix=app.config["host_prefix"],
dim_unaccessible_pages=dim_unaccessible_pages,
)


Expand Down
4 changes: 3 additions & 1 deletion factgenie/bin/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,11 @@ def create_app(**kwargs):
logger=logger,
fmt="%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s",
)

config["host_prefix"] = os.getenv("FACTGENIE_HOST_PREFIX", config["host_prefix"])
config["login"]["active"] = os.getenv("FACTGENIE_LOGIN_ACTIVE", config["login"]["active"])
config["login"]["lock_view_pages"] = os.getenv(
"FACTGENIE_LOCK_VIEW_PAGES", config["login"].get("lock_view_pages", True)
)
config["login"]["username"] = os.getenv("FACTGENIE_LOGIN_USERNAME", config["login"]["username"])
config["login"]["password"] = os.getenv("FACTGENIE_LOGIN_PASSWORD", config["login"]["password"])

Expand Down
15 changes: 11 additions & 4 deletions factgenie/config/config_TEMPLATE.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
---
host_prefix: ""
logging:
level: INFO
flask_debug: false
login:
# require user login for all pages except the page for human annotators
active: false
# if set to `false`, the Browse and Analyze pages will not require login even when login is active (useful for publishing results)
lock_view_pages: true
# !!! change the default values !!!
username: admin
password: factgenie
logging:
# accepts values from the logging module: https://docs.python.org/3/library/logging.html#logging-levels
level: INFO
# printing extra Flask debug logs
flask_debug: false
# set e.g. to "/demo/factgenie" if your app is running behind a reverse proxy on https://your-server.com/demo/factgenie
host_prefix: ""
14 changes: 14 additions & 0 deletions factgenie/static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -996,4 +996,18 @@ a:hover {

.panel-50 {
min-width: 900px;
}

.locked-grey {
background-color: #f3f3f3;
border: 1px solid #dcdcdc;
color: #666666;
}

.locked-grey a img {
filter: opacity(0.5);
}

.locked-grey h5 {
filter: opacity(0.5);
}
1 change: 1 addition & 0 deletions factgenie/static/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function updateConfig() {
host_prefix: $('#host_prefix').val(),
login: {
active: $('#login_active').is(':checked'),
lock_view_pages: $('#lock_view_pages').is(':checked'),
username: $('#login_username').val(),
password: $('#login_password').val()
}
Expand Down
11 changes: 11 additions & 0 deletions factgenie/templates/pages/app_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ <h5>Password protection</h5>
app_config.login.active %} checked {% endif %}>
</div>
</div>
<div class="form-group mt-3">
<label for="lock_view_pages">Include view pages in password protection</label>
<div class="mb-2">
<small class="form-text text-muted">If disable, the Browse and Analyze pages will not require login even
when login is active. Useful for publishing results collected with factgenie.</small>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="lock_view_pages" name="lock_view_pages" {% if
app_config.login.lock_view_pages %} checked {% endif %}>
</div>
</div>
<div class="form-group mt-3">
<label for="login_username">Username</label>
<div class="mb-2">
Expand Down
8 changes: 4 additions & 4 deletions factgenie/templates/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h5 class="card-title">Browse</h5>
</div>
</div>
<div class="col-md-4">
<div class="card text-center mt-3">
<div class="card text-center mt-3 {% if dim_unaccessible_pages %}locked-grey{% endif %}">
<div class="card-body">
<a href="{{ host_prefix }}/llm_eval">
<img src="{{ host_prefix }}/static/img/llm_eval.png" class="intro-img">
Expand All @@ -41,7 +41,7 @@ <h5 class="card-title">Annotate with LLMs</h5>
</div>
</div>
<div class="col-md-4">
<div class="card text-center mt-3">
<div class="card text-center mt-3 {% if dim_unaccessible_pages %}locked-grey{% endif %}">
<div class="card-body">
<a href="{{ host_prefix }}/crowdsourcing">
<img src="{{ host_prefix }}/static/img/crowdsourcing.png" class="intro-img">
Expand All @@ -52,7 +52,7 @@ <h5 class="card-title">Annotate with human annotators</h5>
</div>
</div>
<div class="col-md-4">
<div class="card text-center mt-3">
<div class="card text-center mt-3 {% if dim_unaccessible_pages %}locked-grey{% endif %}">
<div class="card-body">
<a href="{{ host_prefix }}/llm_gen">
<img src="{{ host_prefix }}/static/img/llm_gen.png" class="intro-img">
Expand All @@ -74,7 +74,7 @@ <h5 class="card-title">Analyze</h5>
</div>
</div>
<div class="col-md-4">
<div class="card text-center mt-3">
<div class="card text-center mt-3 {% if dim_unaccessible_pages %}locked-grey{% endif %}">
<div class="card-body">
<a href="{{ host_prefix }}/manage">
<img src="{{ host_prefix }}/static/img/manage.png" class="intro-img">
Expand Down

0 comments on commit 63df13c

Please sign in to comment.