-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
87 lines (66 loc) · 2.65 KB
/
tasks.py
File metadata and controls
87 lines (66 loc) · 2.65 KB
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
# tasks.py
import logging
import os
from dotenv import load_dotenv
from pathlib import Path
from celery import Celery
from factcheck.utils.config_loader import config
from factcheck import build_fact_check_system
logging.getLogger('sentence_transformers').setLevel(logging.ERROR)
logging.getLogger('chromadb').setLevel(logging.ERROR)
logging.getLogger('urllib3').setLevel(logging.WARNING)
os.environ["TOKENIZERS_PARALLELISM"] = "false"
project_dir = Path(__file__).resolve().parent
env_path = project_dir / '.env'
print(f"Attempting to load environment variables from: {env_path}")
if env_path.exists():
load_dotenv(dotenv_path=env_path)
print("Successfully loaded .env file.")
celery_app = Celery(
'tasks',
broker=config.get('celery.broker_url'),
backend=config.get('celery.backend_url')
)
factcheck_instance = None
def get_factcheck_instance():
global factcheck_instance
if factcheck_instance is None:
print("Celery Worker is initializing FactCheck instance for the first time...")
factcheck_instance = build_fact_check_system(
default_model=config.get('llm.default_model'),
api_config=None,
prompt_name=config.get('llm.default_prompt'),
retriever_name=config.get('pipeline.retriever'),
)
print("FactCheck instance initialized for Celery Worker.")
return factcheck_instance
@celery_app.task(bind=True)
def run_fact_check_task(self, text_to_check: str):
"""
The main asynchronous worker task.
This function runs in a separate process (managed by Celery) to avoid blocking the Web Server.
It initializes the `FactCheck` pipeline and executes the check, reporting progress back to Redis.
Args:
text_to_check (str): The raw input text/article to verify.
"""
instance = get_factcheck_instance()
def update_progress(state, message, payload=None):
meta = {
'current_step': message,
'data': payload or {}
}
self.update_state(state=state, meta=meta)
if payload and 'event' in payload:
print(f"[PROGRESS] {message} (Event: {payload['event']})")
else:
print(f"[PROGRESS] {message}")
try:
update_progress('PROGRESS', 'Initializing pipeline...')
result_data = instance.check_text_with_progress(text_to_check, update_progress)
return {'status': 'SUCCESS', 'result': result_data}
except Exception as e:
print(f"!!! Task failed. Error: {e}")
import traceback
traceback.print_exc()
self.update_state(state='FAILURE', meta={'error': str(e)})
raise e