CoolFace
Apppublic

KAMAL18/AlgorithmOfDataScienceProjects

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py116 linesDownload Raw Back to root
1import gradio as gr2import json3import requests4from bs4 import BeautifulSoup5 6try:7    from sentence_transformers import SentenceTransformer, util8    from transformers import pipeline9    MODULES_AVAILABLE = True10except (ModuleNotFoundError, ImportError):11    print("Warning: Required ML modules are missing. Running in fallback mode.")12    MODULES_AVAILABLE = False13 14class URLValidator:15    def __init__(self):16        if MODULES_AVAILABLE:17            self.similarity_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')18            self.sentiment_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")19        else:20            self.similarity_model = None21            self.sentiment_analyzer = None22 23    def fetch_page_content(self, url):24        """Fetches webpage text content."""25        headers = {26            "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"27        }28        try:29            response = requests.get(url, headers=headers, timeout=10)30            response.raise_for_status()31            soup = BeautifulSoup(response.text, "html.parser")32            return " ".join([p.text for p in soup.find_all("p")])33        except requests.RequestException:34            return "ERROR: Unable to fetch webpage content."35 36    def rate_url_validity(self, user_query, url):37        """Validates URL credibility."""38        content = self.fetch_page_content(url)39        if not content:40            return {41                "status": "error",42                "message": "ERROR: Failed to retrieve webpage content.",43                "suggestion": "Try another URL or check if the website blocks bots."44            }45 46        if not MODULES_AVAILABLE:47            return {48                "status": "warning",49                "message": "Machine learning models unavailable.",50                "suggestion": "Install necessary ML modules."51            }52 53        similarity_score = int(util.pytorch_cos_sim(54            self.similarity_model.encode(user_query),55            self.similarity_model.encode(content)56        ).item() * 100)57 58        sentiment_result = self.sentiment_analyzer(content[:512])[0]59        bias_score = 100 if sentiment_result["label"].upper() == "POSITIVE" else 50 if sentiment_result["label"].upper() == "NEUTRAL" else 3060        final_score = round((0.5 * similarity_score) + (0.5 * bias_score), 2)61 62        return {63            "Content Relevance Score": f"{similarity_score} / 100",64            "Bias Score": f"{bias_score} / 100",65            "Final Validity Score": f"{final_score} / 100"66        }67 68# Sample queries and URLs69sample_queries = [70    "What are the symptoms of the flu?",71    "How can I bake a chocolate cake step by step?",72    "Give a brief history of Ancient Rome.",73    "What are the side effects of ibuprofen?",74    "What are the best exercises for weight loss?",75    "How can I improve my sleep quality naturally?",76    "What are the latest advancements in AI?",77    "How should I prepare for a job interview effectively?",78    "Can you explain the theory of relativity in simple terms?",79    "What are some beginner-friendly programming languages for 2025?"80]81 82sample_urls = [83    "https://www.bbc.com/news/world-us-canada-64879434",84    "https://www.nytimes.com",85    "https://www.nature.com",86    "https://www.who.int/health-topics/coronavirus",87    "https://www.cdc.gov/flu/about/index.html",88    "https://www.nasa.gov/press-release/nasa-shares-stunning-new-images-of-galaxies",89    "https://en.wikipedia.org/wiki/Influenza",90    "https://www.python.org",91    "https://www.openai.com",92    "https://arxiv.org"93]94 95validator = URLValidator()96 97def validate_url(user_query, url):98    """Gradio function to validate URLs."""99    result = validator.rate_url_validity(user_query, url)100    return json.dumps(result, indent=2)101 102with gr.Blocks() as demo:103    gr.Markdown("# URL Credibility Validator")104    gr.Markdown("### Validate the credibility of any webpage using AI")105 106    user_query = gr.Dropdown(choices=sample_queries, label="Select a search query:")107    url_input = gr.Dropdown(choices=sample_urls, label="Select a URL to validate:")108    109    output = gr.Textbox(label="Validation Results")  110    111    validate_button = gr.Button("Validate URL")112    validate_button.click(validate_url, inputs=[user_query, url_input], outputs=output)113 114if __name__ == "__main__":115    demo.launch(server_name="0.0.0.0", server_port=7860)116