Files
pgz-sport/scrapers/dabi_self_quiz.py_prije_env_deepseek

55 lines
2.3 KiB
Python

#!/usr/bin/env python3
import os
# Self-quiz loop — DABI gets randomized PGŽ sport questions every 5min
import psycopg2, requests, time, random, hashlib, logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s [self_quiz] %(message)s')
log = logging.getLogger("self_quiz")
DSN = f"host=10.10.0.2 port=6432 dbname=rinet_v3 user=rinet password={os.environ['DB_PASSWORD']}"
ORCH = "http://localhost:8080/api/v3/ask"
def main():
while True:
try:
conn = psycopg2.connect(DSN); conn.autocommit = True
cur = conn.cursor()
cur.execute("""
SELECT question, answer FROM dabi.training_qa
WHERE category LIKE 'pgz_sport_%'
ORDER BY random() LIMIT 20
""")
qa_pairs = cur.fetchall()
cur.close(); conn.close()
for q, expected_a in qa_pairs:
try:
r = requests.post(ORCH, json={"question": q, "persona": "app"}, timeout=15)
if r.status_code == 200:
d = r.json()
actual = d.get('answer', '')
# Log to dabi.system_log za eval
conn2 = psycopg2.connect(DSN); conn2.autocommit = True
c2 = conn2.cursor()
try:
c2.execute("""
INSERT INTO dabi.system_log (event_type, message, metadata, created_at)
VALUES ('self_quiz', %s, %s::jsonb, now())
""", (q[:200],
'{"expected":' + repr(expected_a[:200])[1:-1].replace('"','\\"') + ',"actual":' + repr(actual[:200])[1:-1].replace('"','\\"') + '}'))
except Exception as e:
pass
c2.close(); conn2.close()
log.info(f"Q: {q[:60]}... A: {actual[:80]}")
except Exception as e:
log.warning(f"Quiz fail: {e}")
time.sleep(3)
log.info(f"Cycle done, sleep 300s")
time.sleep(300)
except Exception as e:
log.error(f"Loop error: {e}")
time.sleep(60)
if __name__ == "__main__":
main()