File size: 2,633 Bytes
bbabc35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f77f672
 
 
 
bbabc35
 
 
 
 
 
 
 
 
 
 
f77f672
bbabc35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# pip install openai lxml cssselector requests xmltodict

from datetime import datetime
import json
import lxml.html
from lxml.cssselect import CSSSelector

from openai import OpenAI
import requests
import xmltodict

client = OpenAI()

# load from WikiNews English
r = requests.get("https://en.wikinews.org/w/index.php?title=Special:NewsFeed&feed=atom&categories=Published&count=5")
data = xmltodict.parse(r.content)

outputs = []
entries = data["feed"]["entry"]
for en in entries:
  # en["summary"]["#text"]
  # en["title"]
  dtme = datetime.strptime(en["updated"], "%Y-%m-%dT%H:%M:%SZ")
  dt = dtme.strftime("%Y/%m/%d")

  summ = lxml.html.fromstring(en["summary"]["#text"])

  selAnchor = CSSSelector('a[rel="nofollow"]')
  foundElements = selAnchor(summ)
  articleLinks = []
  for el in foundElements:
    link = el.get('href')
    if '.com/intent/tweet' in link or 'facebook.com/sharer.php' in link or 'mailto:' in link or 'reddit.com/submit' in link or 'linkedin.com/shareArticle' in link:
      continue
    articleLinks.append(link)

  plaintxt = summ.text_content()
  if 'Have an opinion on this story?' in plaintxt:
    plaintxt = plaintxt[:plaintxt.find('Have an opinion on this story?')]
  # print(plaintxt)

  response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
      {
        "role": "system",
        "content": "You will be provided with an article from today's news. Provide 3-5 multiple choice questions based on the content of the article, especially newly-introduced facts or knowledge. Don't make the correct answer any more specific, numeric, or realistic compared to the others.\n Respond in JSON format: [{ question: 'Who was elected president of Sesame Street?', choices: ['Big Bird', 'Donald Duck'], answer: 'Big Bird' }]",
      },
      {
        "role": "user",
        "content": f"Here's the article: \n{plaintxt}",
      },
    ],
  )
  reply = response.choices[0].message.content
  reply = reply[reply.index('[') : reply.rindex(']') + 1]
  qs = json.loads(reply)

  for q in qs:
    if q["answer"] not in q["choices"]:
      continue

    outputs.append({
      "question_date": dt,
      "question_url": en["link"]["@href"],
      "question_sentence": q["question"],
      "links": articleLinks,
      "choices": q["choices"],
      "answer_text": q["answer"],
      "answer": [ q["choices"].index(q["answer"]) ],
    })

tstamp = datetime.now().strftime("%Y%m%d")
with open(f"./{tstamp}_qa_public.jsonl", "w") as fi:
  for idx, op in enumerate(outputs):
    op["question_id"] = f"{tstamp}_{idx}"
    op["question_source"] = "WikiNews"
    fi.write(json.dumps(op) + "\n")