-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
411 lines (349 loc) · 14 KB
/
server.py
File metadata and controls
411 lines (349 loc) · 14 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
"""
FactCheck AI — web server pe 0.0.0.0:8765
Extrage articol cu newspaper3k si cere analiza de fact-checking de la claude CLI
cu WebSearch/WebFetch activ, apoi streameaza rezultatul in UI prin SSE.
"""
import json
import os
import queue
import re
import shlex
import subprocess
import threading
import time
from pathlib import Path
from flask import Flask, Response, jsonify, request, send_from_directory
# newspaper3k
from newspaper import Article, Config
APP_DIR = Path(__file__).resolve().parent
PORT = 8765
HOST = "0.0.0.0"
CLAUDE_BIN = os.path.expanduser("~/.local/bin/claude")
CLAUDE_MODEL = os.environ.get("FACTCHECK_MODEL", "sonnet")
app = Flask(__name__, static_folder=str(APP_DIR / "static"), static_url_path="/static")
# ---------- newspaper3k extraction ----------
def extract_from_url(url: str) -> dict:
cfg = Config()
cfg.browser_user_agent = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
)
cfg.request_timeout = 20
cfg.fetch_images = False
art = Article(url, config=cfg)
art.download()
art.parse()
try:
art.nlp()
except Exception:
pass
return {
"source_url": url,
"title": art.title or "",
"authors": art.authors or [],
"publish_date": art.publish_date.isoformat() if art.publish_date else None,
"text": art.text or "",
"top_image": art.top_image or "",
"keywords": getattr(art, "keywords", []) or [],
"summary": getattr(art, "summary", "") or "",
"meta_description": art.meta_description or "",
"meta_keywords": art.meta_keywords or [],
"canonical_link": art.canonical_link or "",
}
def extract_from_text(text: str) -> dict:
return {
"source_url": None,
"title": "(text introdus manual)",
"authors": [],
"publish_date": None,
"text": text.strip(),
"top_image": "",
"keywords": [],
"summary": "",
"meta_description": "",
"meta_keywords": [],
"canonical_link": "",
}
# ---------- Claude prompt ----------
SYSTEM_PROMPT = """Esti un analist profesionist de fact-checking cu standarde de redactie de calibru
Reuters / AP / AFP / BBC Verify. Vorbesti romana naturala, precisa, fara jargon gol.
Folosesti WebSearch si WebFetch pentru a verifica afirmatiile in surse primare si
independente de mare incredere. Cand gasesti o sursa, citeaza titlul si URL-ul exact.
Surse PREFERATE (nivel inalt de incredere): Reuters, AP News, AFP, BBC, DW,
Le Monde, The Guardian, NYT, WaPo, jurnale stiintifice (Nature, Science, Lancet, NEJM,
arXiv pentru preprinturi), baze oficiale (ONU, OMS, CE, guvern.ro, data.gov),
fact-checkers acreditati IFCN (Snopes, PolitiFact, FactCheck.org, Full Fact,
Africa Check, Maldita, Pagella Politica, Factual.ro, Veridica.ro).
Surse SUSPECTE: site-uri fara autor, fara ISSN, fara contact, anonime, clone,
domenii tipare conspirative, retele de dezinformare cunoscute (EUvsDisinfo le listeaza).
REGULI:
1. Separa afirmatiile verificabile de opinii.
2. Pentru fiecare afirmatie cheie: cauta 2-3 surse independente.
3. Daca afirmatia contrazice consensul stiintific (sanatate, clima, etc.), spune clar.
4. Argumentele trebuie sa convinga si un sceptic: explica LOGICA, nu da doar "autoritatea".
5. Niciodata nu inventa surse sau URL-uri. Daca nu gasesti, spune ca nu ai gasit.
"""
USER_PROMPT_TEMPLATE = """Verifica aceasta stire. Foloseste WebSearch si WebFetch pentru surse externe.
========= ARTICOL =========
Titlu: {title}
Autori: {authors}
Data: {date}
Sursa URL: {url}
Text:
{text}
===========================
Procedura:
1. Narreaza in timp real ce faci: ce afirmatii identifici, ce cauti, ce gasesti.
Scrie concis, in paragrafe scurte, in ROMANA.
2. Verifica afirmatiile cheie cu minim 2 surse independente de mare incredere.
3. Cauta si eventuale dezmintiri sau confirmari pe fact-checkers acreditati.
4. Analizeaza SENTIMENTUL articolului: ton emotional dominant, intensitate, daca
incearca sa induca frica/furie/entuziasm/dispret, daca foloseste limbaj
incarcat (cuvinte tari, exclamatii, etichete morale).
5. Identifica INTENTIA articolului: de ce a fost scris? Sa informeze neutru, sa
convinga (advocacy/opinie), sa atraga clickuri (clickbait), sa promoveze
comercial, sa induca in eroare deliberat (propaganda/dezinformare), sa amuze
(satira), sau sa polarizeze. Uita-te la titlu vs continut, surse selectate
tendentios, omisiuni, framing.
6. La final, dupa toata cercetarea, scrie exact acest bloc (si doar acest bloc JSON,
fara text dupa el):
```json
{{
"verdict": "REAL" | "FALS" | "AMBIGUU" | "INDUCE_IN_EROARE",
"confidence_percent": 0-100,
"breakdown": {{
"real_percent": 0-100,
"fals_percent": 0-100,
"neverificabil_percent": 0-100
}},
"titlu_scurt": "rezumat in max 15 cuvinte",
"explicatie_generala": "2-4 propozitii clare in romana, pentru cititor obisnuit",
"explicatie_pentru_sceptic": "argument logic in 3-5 propozitii care poate convinge un conspirationist: nu invoca autoritati goale, explica MECANISMUL, coincidente, probe materiale",
"sentiment": {{
"tip": "POZITIV" | "NEGATIV" | "NEUTRU" | "MIXT",
"intensitate": 0-100,
"tonuri": ["alarmist", "indignat", "encomiastic", "ironic", "factual", "..."],
"explicatie": "1-2 propozitii: ce ton predomina si ce indici lingvistici il tradeaza"
}},
"intentie": {{
"tip": "INFORMARE" | "OPINIE" | "ADVOCACY" | "CLICKBAIT" | "PROPAGANDA" | "DEZINFORMARE" | "SATIRA" | "PROMOVARE_COMERCIALA" | "POLARIZARE" | "ALTUL",
"incredere": 0-100,
"explicatie": "2-3 propozitii: de ce crezi ca aceasta e intentia (titlu vs continut, framing, surse alese, omisiuni, indemnuri)",
"publicul_tinta": "cui ii vorbeste articolul si ce reactie pare sa urmareasca"
}},
"claims": [
{{
"afirmatie": "...",
"verdict": "REAL|FALS|PARTIAL|NEVERIFICABIL",
"dovezi": "explicatie scurta",
"surse": [
{{"titlu": "...", "url": "https://...", "incredere": "inalta|medie|joasa"}}
]
}}
],
"red_flags": ["lista semnale de alarma detectate, daca exista"],
"surse_principale": [
{{"titlu": "...", "url": "https://...", "tip": "stire|oficial|academic|factchecker"}}
]
}}
```
IMPORTANT: procentele din breakdown trebuie sa insumeze 100.
"""
def build_prompt(article: dict) -> str:
authors = ", ".join(article["authors"]) if article["authors"] else "(necunoscut)"
date = article["publish_date"] or "(fara data)"
url = article["source_url"] or "(introdus manual)"
text = article["text"][:12000] # limiteaza dimensiunea
return USER_PROMPT_TEMPLATE.format(
title=article["title"] or "(fara titlu)",
authors=authors,
date=date,
url=url,
text=text,
)
# ---------- Claude streaming ----------
def run_claude_stream(prompt: str, q: queue.Queue):
"""Ruleaza claude -p cu stream-json si pune evenimente in q."""
cmd = [
CLAUDE_BIN,
"-p",
"--model", CLAUDE_MODEL,
"--permission-mode", "bypassPermissions",
"--allowedTools", "WebSearch", "WebFetch",
"--output-format", "stream-json",
"--include-partial-messages",
"--verbose",
"--append-system-prompt", SYSTEM_PROMPT,
prompt,
]
env = os.environ.copy()
env["CLAUDE_CODE_SIMPLE"] = "0"
try:
proc = subprocess.Popen(
cmd,
cwd=str(APP_DIR),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
text=True,
bufsize=1,
)
except Exception as e:
q.put(("error", {"message": f"Nu pot porni claude: {e}"}))
q.put(("done", {}))
return
stderr_buf = []
def read_stderr():
for line in proc.stderr:
stderr_buf.append(line)
threading.Thread(target=read_stderr, daemon=True).start()
full_text = []
try:
for line in proc.stdout:
line = line.strip()
if not line:
continue
try:
ev = json.loads(line)
except json.JSONDecodeError:
continue
etype = ev.get("type")
if etype == "system":
q.put(("claude_system", {"subtype": ev.get("subtype", "")}))
elif etype == "stream_event":
se = ev.get("event", {})
setype = se.get("type")
if setype == "content_block_delta":
d = se.get("delta", {})
if d.get("type") == "text_delta":
chunk = d.get("text", "")
full_text.append(chunk)
q.put(("claude_text", {"text": chunk}))
elif d.get("type") == "thinking_delta":
q.put(("claude_thinking", {"text": d.get("thinking", "")}))
elif setype == "content_block_start":
cb = se.get("content_block", {})
if cb.get("type") == "tool_use":
q.put(("tool_use_start", {
"name": cb.get("name", ""),
"id": cb.get("id", ""),
}))
elif etype == "assistant":
msg = ev.get("message", {})
for c in msg.get("content", []):
if c.get("type") == "tool_use":
q.put(("tool_use", {
"name": c.get("name", ""),
"input": c.get("input", {}),
}))
elif etype == "user":
msg = ev.get("message", {})
for c in msg.get("content", []):
if c.get("type") == "tool_result":
raw = c.get("content", "")
if isinstance(raw, list):
raw = " ".join(
(x.get("text", "") if isinstance(x, dict) else str(x))
for x in raw
)
q.put(("tool_result", {"preview": (raw or "")[:400]}))
elif etype == "result":
# final
final_text = ev.get("result") or "".join(full_text)
verdict = extract_verdict_json(final_text)
if verdict:
q.put(("verdict", verdict))
q.put(("result_meta", {
"cost_usd": ev.get("total_cost_usd"),
"duration_ms": ev.get("duration_ms"),
"num_turns": ev.get("num_turns"),
}))
proc.wait(timeout=5)
if proc.returncode not in (0, None):
q.put(("error", {
"message": f"claude a iesit cu cod {proc.returncode}",
"stderr": "".join(stderr_buf)[-1500:],
}))
except Exception as e:
q.put(("error", {"message": f"Eroare in streaming: {e}"}))
finally:
q.put(("done", {}))
def extract_verdict_json(text: str):
"""Extrage blocul JSON final din raspunsul claude."""
if not text:
return None
# Cauta ```json ... ``` fence
m = re.search(r"```json\s*(\{.*?\})\s*```", text, re.DOTALL)
if not m:
# fallback: ultima pereche de {} care contine "verdict"
m = re.search(r"(\{[^{}]*\"verdict\".*\})\s*$", text, re.DOTALL)
if not m:
return None
try:
return json.loads(m.group(1))
except json.JSONDecodeError:
return None
# ---------- SSE plumbing ----------
def sse_format(event: str, data) -> str:
payload = json.dumps(data, ensure_ascii=False)
return f"event: {event}\ndata: {payload}\n\n"
# ---------- Routes ----------
@app.route("/")
def index():
return send_from_directory(str(APP_DIR), "index.html")
@app.route("/factcheck", methods=["POST"])
def factcheck():
body = request.get_json(force=True, silent=True) or {}
url = (body.get("url") or "").strip()
text = (body.get("text") or "").strip()
if not url and not text:
return jsonify({"error": "dai un URL sau text"}), 400
def generate():
try:
yield sse_format("status", {"message": "extrag articolul..."})
if url:
try:
article = extract_from_url(url)
except Exception as e:
yield sse_format("error", {"message": f"newspaper3k a esuat: {e}"})
return
else:
article = extract_from_text(text)
if not article["text"] or len(article["text"]) < 30:
yield sse_format("error", {"message": "nu am putut extrage text util"})
return
yield sse_format("extracted", article)
yield sse_format("status", {"message": "pornesc analiza claude..."})
prompt = build_prompt(article)
q: queue.Queue = queue.Queue()
t = threading.Thread(target=run_claude_stream, args=(prompt, q), daemon=True)
t.start()
last_keepalive = time.time()
while True:
try:
etype, data = q.get(timeout=5)
except queue.Empty:
# keepalive
yield ": keepalive\n\n"
last_keepalive = time.time()
continue
yield sse_format(etype, data)
if etype == "done":
break
except GeneratorExit:
return
except Exception as e:
yield sse_format("error", {"message": f"eroare server: {e}"})
yield sse_format("done", {})
return Response(generate(), mimetype="text/event-stream", headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"Connection": "keep-alive",
})
@app.route("/health")
def health():
return jsonify({"ok": True, "port": PORT, "model": CLAUDE_MODEL})
if __name__ == "__main__":
print(f"FactCheck AI pornit pe http://{HOST}:{PORT}")
app.run(host=HOST, port=PORT, threaded=True, debug=False)