jl48590n commited on
Commit
c5e5728
·
verified ·
1 Parent(s): 8e719ae

requirements.txt

Browse files

streamlit
transformers
sentence-transformers
beautifulsoup4
requests

Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ from transformers import pipeline
5
+ from sentence_transformers import SentenceTransformer, util
6
+
7
+ class URLValidator:
8
+ def __init__(self):
9
+ self.similarity_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
10
+ self.fake_news_classifier = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
11
+ self.sentiment_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
12
+
13
+ def fetch_page_content(self, url):
14
+ try:
15
+ response = requests.get(url, timeout=10)
16
+ response.raise_for_status()
17
+ soup = BeautifulSoup(response.text, "html.parser")
18
+ return " ".join([p.text for p in soup.find_all("p")])
19
+ except:
20
+ return ""
21
+
22
+ def compute_similarity_score(self, user_query, content):
23
+ if not content:
24
+ return 0
25
+ return int(util.pytorch_cos_sim(
26
+ self.similarity_model.encode(user_query),
27
+ self.similarity_model.encode(content)
28
+ ).item() * 100)
29
+
30
+ def detect_bias(self, content):
31
+ if not content:
32
+ return 50
33
+ sentiment_result = self.sentiment_analyzer(content[:512])[0]
34
+ return 100 if sentiment_result["label"] == "POSITIVE" else 50 if sentiment_result["label"] == "NEUTRAL" else 30
35
+
36
+ def get_star_rating(self, score: float):
37
+ stars = max(1, min(5, round(score / 20)))
38
+ return stars, "⭐" * stars
39
+
40
+ def generate_explanation(self, domain_trust, similarity_score, fact_check_score, bias_score, citation_score, final_score):
41
+ reasons = []
42
+ if domain_trust < 50:
43
+ reasons.append("The source has low domain authority.")
44
+ if similarity_score < 50:
45
+ reasons.append("The content is not highly relevant to your query.")
46
+ if fact_check_score < 50:
47
+ reasons.append("Limited fact-checking verification found.")
48
+ if bias_score < 50:
49
+ reasons.append("Potential bias detected in the content.")
50
+ if citation_score < 30:
51
+ reasons.append("Few citations found for this content.")
52
+ return " ".join(reasons) if reasons else "This source is highly credible and relevant."
53
+
54
+ def rate_url_validity(self, user_query, url):
55
+ content = self.fetch_page_content(url)
56
+ similarity_score = self.compute_similarity_score(user_query, content)
57
+ bias_score = self.detect_bias(content)
58
+
59
+ domain_trust = 60 # Placeholder
60
+ fact_check_score = 70 # Placeholder
61
+ citation_score = 50 # Placeholder
62
+
63
+ final_score = (0.3 * domain_trust) + (0.3 * similarity_score) + (0.2 * fact_check_score) + (0.1 * bias_score) + (0.1 * citation_score)
64
+
65
+ stars, icon = self.get_star_rating(final_score)
66
+ explanation = self.generate_explanation(domain_trust, similarity_score, fact_check_score, bias_score, citation_score, final_score)
67
+
68
+ return {
69
+ "Final Score": f"{final_score:.2f}%",
70
+ "Star Rating": icon,
71
+ "Explanation": explanation
72
+ }
73
+
74
+ # Initialize validator
75
+ validator = URLValidator()
76
+
77
+ st.title("URL Credibility Checker")
78
+ url = st.text_input("Enter URL")
79
+ query = st.text_input("Enter Search Query")
80
+
81
+ if st.button("Check Credibility"):
82
+ if url and query:
83
+ result = validator.rate_url_validity(query, url)
84
+ st.write("**Final Score:**", result["Final Score"])
85
+ st.write("**Star Rating:**", result["Star Rating"])
86
+ st.write("**Explanation:**", result["Explanation"])
87
+ else:
88
+ st.warning("Please enter both a URL and a search query.")