aryn25 commited on
Commit
4133b77
·
verified ·
1 Parent(s): 5c17e7b

Create scoring/quality.py

Browse files
Files changed (1) hide show
  1. scoring/quality.py +48 -0
scoring/quality.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
7
+
8
+ def quality_score(entity: str) -> int:
9
+ """
10
+ If 'entity' is a URL:
11
+ • Fetch the first 2000 characters of its visible text.
12
+ • Ask GPT-4: "Rate this snippet 0–100 on combined Expertise, Authority, Trustworthiness."
13
+ • Return the integer score.
14
+ Else, return default 60.
15
+ """
16
+ if not entity.startswith("http"):
17
+ return 60
18
+
19
+ try:
20
+ resp = requests.get(entity, timeout=5)
21
+ text_snippet = BeautifulSoup(resp.text, "html.parser").get_text()[:2000]
22
+
23
+ prompt = (
24
+ "Below is the text snippet of a webpage. Rate it 0–100 on combined "
25
+ "Expertise, Authority, and Trustworthiness (E-E-A-T), and return just the integer score.\n\n"
26
+ f"{text_snippet}"
27
+ )
28
+ api_resp = openai.ChatCompletion.create(
29
+ model="gpt-4",
30
+ messages=[{"role": "user", "content": prompt}]
31
+ )
32
+ content = api_resp.choices[0].message.content.strip()
33
+ digits = "".join(filter(str.isdigit, content))
34
+ if digits:
35
+ score = int(digits)
36
+ return max(0, min(score, 100))
37
+ except Exception:
38
+ pass
39
+
40
+ return 60
41
+
42
+ def quality_recommendation(entity: str, score: int) -> str:
43
+ if score < 50:
44
+ return (
45
+ "Improve E-E-A-T: add author bylines, cite reputable sources, "
46
+ "and maintain factual consistency."
47
+ )
48
+ return "Content quality is strong; keep updating and citing reputable references."