import gradio as gr import json import requests from bs4 import BeautifulSoup try: from sentence_transformers import SentenceTransformer, util from transformers import pipeline MODULES_AVAILABLE = True except (ModuleNotFoundError, ImportError): print("Warning: Required ML modules are missing. Running in fallback mode.") MODULES_AVAILABLE = False class URLValidator: def __init__(self): if MODULES_AVAILABLE: self.similarity_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2') self.sentiment_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment") else: self.similarity_model = None self.sentiment_analyzer = None def fetch_page_content(self, url): """Fetches webpage text content.""" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" } try: response = requests.get(url, headers=headers, timeout=10) response.raise_for_status() soup = BeautifulSoup(response.text, "html.parser") return " ".join([p.text for p in soup.find_all("p")]) except requests.RequestException: return "ERROR: Unable to fetch webpage content." def rate_url_validity(self, user_query, url): """Validates URL credibility.""" content = self.fetch_page_content(url) if not content: return { "status": "error", "message": "ERROR: Failed to retrieve webpage content.", "suggestion": "Try another URL or check if the website blocks bots." } if not MODULES_AVAILABLE: return { "status": "warning", "message": "Machine learning models unavailable.", "suggestion": "Install necessary ML modules." } similarity_score = int(util.pytorch_cos_sim( self.similarity_model.encode(user_query), self.similarity_model.encode(content) ).item() * 100) sentiment_result = self.sentiment_analyzer(content[:512])[0] bias_score = 100 if sentiment_result["label"].upper() == "POSITIVE" else 50 if sentiment_result["label"].upper() == "NEUTRAL" else 30 final_score = round((0.5 * similarity_score) + (0.5 * bias_score), 2) return { "Content Relevance Score": f"{similarity_score} / 100", "Bias Score": f"{bias_score} / 100", "Final Validity Score": f"{final_score} / 100" } # Sample queries and URLs sample_queries = [ "What are the symptoms of the flu?", "How can I bake a chocolate cake step by step?", "Give a brief history of Ancient Rome.", "What are the side effects of ibuprofen?", "What are the best exercises for weight loss?", "How can I improve my sleep quality naturally?", "What are the latest advancements in AI?", "How should I prepare for a job interview effectively?", "Can you explain the theory of relativity in simple terms?", "What are some beginner-friendly programming languages for 2025?" ] sample_urls = [ "https://www.bbc.com/news/world-us-canada-64879434", "https://www.nytimes.com", "https://www.nature.com", "https://www.who.int/health-topics/coronavirus", "https://www.cdc.gov/flu/about/index.html", "https://www.nasa.gov/press-release/nasa-shares-stunning-new-images-of-galaxies", "https://en.wikipedia.org/wiki/Influenza", "https://www.python.org", "https://www.openai.com", "https://arxiv.org" ] validator = URLValidator() def validate_url(user_query, url): """Gradio function to validate URLs.""" result = validator.rate_url_validity(user_query, url) return json.dumps(result, indent=2) with gr.Blocks() as demo: gr.Markdown("# URL Credibility Validator") gr.Markdown("### Validate the credibility of any webpage using AI") user_query = gr.Dropdown(choices=sample_queries, label="Select a search query:") url_input = gr.Dropdown(choices=sample_urls, label="Select a URL to validate:") output = gr.Textbox(label="Validation Results") validate_button = gr.Button("Validate URL") validate_button.click(validate_url, inputs=[user_query, url_input], outputs=output) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)