Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ class URLValidator:
|
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
# SerpAPI Key
|
|
|
|
| 17 |
self.serpapi_key = os.getenv("SERPAPI_KEY")
|
| 18 |
|
| 19 |
# Load models once to avoid redundant API calls
|
|
@@ -91,4 +92,79 @@ class URLValidator:
|
|
| 91 |
def get_star_rating(self, score: float) -> tuple:
|
| 92 |
""" Converts a score (0-100) into a 1-5 star rating. """
|
| 93 |
stars = max(1, min(5, round(score / 20))) # Normalize 100-scale to 5-star scale
|
| 94 |
-
return stars, "⭐" *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
# SerpAPI Key
|
| 17 |
+
# This api key is acquired from SerpAPI website.
|
| 18 |
self.serpapi_key = os.getenv("SERPAPI_KEY")
|
| 19 |
|
| 20 |
# Load models once to avoid redundant API calls
|
|
|
|
| 92 |
def get_star_rating(self, score: float) -> tuple:
|
| 93 |
""" Converts a score (0-100) into a 1-5 star rating. """
|
| 94 |
stars = max(1, min(5, round(score / 20))) # Normalize 100-scale to 5-star scale
|
| 95 |
+
return stars, "⭐" * stars
|
| 96 |
+
|
| 97 |
+
def generate_explanation(self, domain_trust, similarity_score, fact_check_score, bias_score, citation_score, final_score) -> str:
|
| 98 |
+
""" Generates a human-readable explanation for the score. """
|
| 99 |
+
reasons = []
|
| 100 |
+
if domain_trust < 50:
|
| 101 |
+
reasons.append("The source has low domain authority.")
|
| 102 |
+
if similarity_score < 50:
|
| 103 |
+
reasons.append("The content is not highly relevant to your query.")
|
| 104 |
+
if fact_check_score < 50:
|
| 105 |
+
reasons.append("Limited fact-checking verification found.")
|
| 106 |
+
if bias_score < 50:
|
| 107 |
+
reasons.append("Potential bias detected in the content.")
|
| 108 |
+
if citation_score < 30:
|
| 109 |
+
reasons.append("Few citations found for this content.")
|
| 110 |
+
|
| 111 |
+
return " ".join(reasons) if reasons else "This source is highly credible and relevant."
|
| 112 |
+
|
| 113 |
+
def rate_url_validity(self, user_query: str, url: str) -> dict:
|
| 114 |
+
""" Main function to evaluate the validity of a webpage. """
|
| 115 |
+
content = self.fetch_page_content(url)
|
| 116 |
+
|
| 117 |
+
domain_trust = self.get_domain_trust(url, content)
|
| 118 |
+
similarity_score = self.compute_similarity_score(user_query, content)
|
| 119 |
+
fact_check_score = self.check_facts(content)
|
| 120 |
+
bias_score = self.detect_bias(content)
|
| 121 |
+
citation_score = self.check_google_scholar(url)
|
| 122 |
+
|
| 123 |
+
final_score = (
|
| 124 |
+
(0.3 * domain_trust) +
|
| 125 |
+
(0.3 * similarity_score) +
|
| 126 |
+
(0.2 * fact_check_score) +
|
| 127 |
+
(0.1 * bias_score) +
|
| 128 |
+
(0.1 * citation_score)
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
stars, icon = self.get_star_rating(final_score)
|
| 132 |
+
explanation = self.generate_explanation(domain_trust, similarity_score, fact_check_score, bias_score, citation_score, final_score)
|
| 133 |
+
|
| 134 |
+
return {
|
| 135 |
+
"raw_score": {
|
| 136 |
+
"Domain Trust": domain_trust,
|
| 137 |
+
"Content Relevance": similarity_score,
|
| 138 |
+
"Fact-Check Score": fact_check_score,
|
| 139 |
+
"Bias Score": bias_score,
|
| 140 |
+
"Citation Score": citation_score,
|
| 141 |
+
"Final Validity Score": final_score
|
| 142 |
+
},
|
| 143 |
+
"stars": {
|
| 144 |
+
"score": stars,
|
| 145 |
+
"icon": icon
|
| 146 |
+
},
|
| 147 |
+
"explanation": explanation
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
# Streamlit app
|
| 151 |
+
st.write("LEVEL1 TITLE: APP")
|
| 152 |
+
st.write("This is my first app")
|
| 153 |
+
|
| 154 |
+
# User input fields
|
| 155 |
+
user_prompt = "I have just been on an international flight, can I come back home to hold my 1-month-old newborn?"
|
| 156 |
+
url_to_check = "https://www.mayoclinic.org/healthy-lifestyle/infant-and-toddler-health/expert-answers/air-travel-with-infant/faq-20058539"
|
| 157 |
+
|
| 158 |
+
# Run validation when the button is clicked
|
| 159 |
+
if st.button("Validate URL"):
|
| 160 |
+
if not user_prompt.strip() or not url_to_check.strip():
|
| 161 |
+
st.warning("Please enter both a search query and a URL.")
|
| 162 |
+
else:
|
| 163 |
+
with st.spinner("Validating URL..."):
|
| 164 |
+
# Instantiate the URLValidator class
|
| 165 |
+
validator = URLValidator() # Corrected instantiation
|
| 166 |
+
result = validator.rate_url_validity(user_prompt, url_to_check)
|
| 167 |
+
|
| 168 |
+
# Display results in JSON format
|
| 169 |
+
st.subheader("Validation Results")
|
| 170 |
+
st.json(result)
|