Spaces:
Sleeping
Sleeping
Update deliverable2.py
Browse files- deliverable2.py +40 -73
deliverable2.py
CHANGED
@@ -81,84 +81,51 @@ class URLValidator:
|
|
81 |
return " ".join(reasons) if reasons else "This source is highly credible and relevant."
|
82 |
|
83 |
def rate_url_validity(self, user_query: str, url: str):
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# Handle errors
|
88 |
-
if "Error" in content:
|
89 |
-
return {"Validation Error": content}
|
90 |
-
|
91 |
-
domain_trust = self.get_domain_trust(url, content)
|
92 |
-
similarity_score = self.compute_similarity_score(user_query, content)
|
93 |
-
fact_check_score = self.check_facts(content)
|
94 |
-
bias_score = self.detect_bias(content)
|
95 |
-
|
96 |
-
final_score = (
|
97 |
-
(0.3 * domain_trust) +
|
98 |
-
(0.3 * similarity_score) +
|
99 |
-
(0.2 * fact_check_score) +
|
100 |
-
(0.2 * bias_score)
|
101 |
-
)
|
102 |
-
|
103 |
-
stars, icon = self.get_star_rating(final_score)
|
104 |
-
explanation = self.generate_explanation(domain_trust, similarity_score, fact_check_score, bias_score, final_score)
|
105 |
|
|
|
|
|
106 |
return {
|
107 |
-
"raw_score": {
|
108 |
-
"Domain Trust":
|
109 |
-
"Content Relevance":
|
110 |
-
"Fact-Check Score":
|
111 |
-
"Bias Score":
|
112 |
-
"Final Validity Score":
|
113 |
},
|
114 |
"stars": {
|
115 |
-
"icon":
|
116 |
},
|
117 |
-
"explanation":
|
118 |
}
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
-
# **✅ Sample Queries and URLs (10 Each)**
|
122 |
-
sample_queries = [
|
123 |
-
"How does climate change impact global weather?",
|
124 |
-
"What are the latest advancements in AI?",
|
125 |
-
"How does diet influence mental health?",
|
126 |
-
"What are the effects of space travel on astronauts?",
|
127 |
-
"Is cryptocurrency a safe investment?",
|
128 |
-
"What are the advantages of renewable energy?",
|
129 |
-
"How does deep learning work?",
|
130 |
-
"What are the health risks of 5G technology?",
|
131 |
-
"Is intermittent fasting effective for weight loss?",
|
132 |
-
"How do electric vehicles compare to gas cars?"
|
133 |
-
]
|
134 |
-
|
135 |
-
sample_urls = [
|
136 |
-
"https://www.nationalgeographic.com/environment/article/climate-change",
|
137 |
-
"https://www.technologyreview.com/2023/05/01/latest-ai-advancements/",
|
138 |
-
"https://www.health.harvard.edu/mind-and-mood/foods-linked-to-better-brainpower",
|
139 |
-
"https://www.nasa.gov/hrp/long-term-health-risks-of-space-travel",
|
140 |
-
"https://www.investopedia.com/terms/c/cryptocurrency.asp",
|
141 |
-
"https://www.energy.gov/eere/renewable-energy",
|
142 |
-
"https://www.ibm.com/cloud/deep-learning",
|
143 |
-
"https://www.who.int/news-room/questions-and-answers/item/radiation-5g-mobile-networks-and-health",
|
144 |
-
"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6167940/",
|
145 |
-
"https://www.tesla.com/blog/benefits-of-electric-vehicles"
|
146 |
-
]
|
147 |
-
|
148 |
-
# **✅ Running the Validator and Saving to CSV**
|
149 |
-
validator = URLValidator()
|
150 |
-
|
151 |
-
data_rows = []
|
152 |
-
for query, url in zip(sample_queries, sample_urls):
|
153 |
-
result = validator.rate_url_validity(query, url)
|
154 |
-
func_rating = round(result["raw_score"]["Final Validity Score"] / 20) # Convert 100-scale to 1-5
|
155 |
-
custom_rating = func_rating + 1 if func_rating < 5 else func_rating # User-adjusted rating
|
156 |
-
|
157 |
-
data_rows.append([query, url, func_rating, custom_rating])
|
158 |
-
|
159 |
-
# Save to CSV
|
160 |
-
csv_filename = "url_validation_results.csv"
|
161 |
-
df = pd.DataFrame(data_rows, columns=["user_prompt", "url_to_check", "func_rating", "custom_rating"])
|
162 |
-
df.to_csv(csv_filename, index=False)
|
163 |
-
|
164 |
-
print(f"✅ CSV file '{csv_filename}' has been created successfully!")
|
|
|
81 |
return " ".join(reasons) if reasons else "This source is highly credible and relevant."
|
82 |
|
83 |
def rate_url_validity(self, user_query: str, url: str):
|
84 |
+
""" Main function to evaluate the validity of a webpage. """
|
85 |
+
content = self.fetch_page_content(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
# If content fetching failed, return a properly structured response
|
88 |
+
if "Error" in content:
|
89 |
return {
|
90 |
+
"raw_score": {
|
91 |
+
"Domain Trust": 0,
|
92 |
+
"Content Relevance": 0,
|
93 |
+
"Fact-Check Score": 0,
|
94 |
+
"Bias Score": 0,
|
95 |
+
"Final Validity Score": 0
|
96 |
},
|
97 |
"stars": {
|
98 |
+
"icon": "❌"
|
99 |
},
|
100 |
+
"explanation": content # Display the error message
|
101 |
}
|
102 |
|
103 |
+
domain_trust = self.get_domain_trust(url, content)
|
104 |
+
similarity_score = self.compute_similarity_score(user_query, content)
|
105 |
+
fact_check_score = self.check_facts(content)
|
106 |
+
bias_score = self.detect_bias(content)
|
107 |
+
|
108 |
+
final_score = (
|
109 |
+
(0.3 * domain_trust) +
|
110 |
+
(0.3 * similarity_score) +
|
111 |
+
(0.2 * fact_check_score) +
|
112 |
+
(0.2 * bias_score)
|
113 |
+
)
|
114 |
+
|
115 |
+
stars, icon = self.get_star_rating(final_score)
|
116 |
+
explanation = self.generate_explanation(domain_trust, similarity_score, fact_check_score, bias_score, final_score)
|
117 |
+
|
118 |
+
return {
|
119 |
+
"raw_score": {
|
120 |
+
"Domain Trust": domain_trust,
|
121 |
+
"Content Relevance": similarity_score,
|
122 |
+
"Fact-Check Score": fact_check_score,
|
123 |
+
"Bias Score": bias_score,
|
124 |
+
"Final Validity Score": final_score
|
125 |
+
},
|
126 |
+
"stars": {
|
127 |
+
"icon": icon
|
128 |
+
},
|
129 |
+
"explanation": explanation
|
130 |
+
}
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|