Velpurisubbarao19 commited on
Commit
4e78973
·
verified ·
1 Parent(s): 572d519

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+
6
+ try:
7
+ from sentence_transformers import SentenceTransformer, util
8
+ from transformers import pipeline
9
+ MODULES_AVAILABLE = True
10
+ except (ModuleNotFoundError, ImportError):
11
+ print("Warning: Required ML modules are missing. Running in fallback mode.")
12
+ MODULES_AVAILABLE = False
13
+
14
+ class 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 = None
21
+ self.sentiment_analyzer = None
22
+
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 30
60
+ 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 URLs
69
+ sample_queries = [
70
+ "What are the benefits of a plant-based diet?"
71
+ "How does quantum computing work?"
72
+ "What are the causes of climate change?"
73
+ "Explain the basics of blockchain technology."
74
+ "How can I learn a new language quickly?"
75
+ "What are the symptoms of diabetes?"
76
+ "What are the best books for personal development?"
77
+ "How does 5G technology impact daily life?"
78
+ "What are the career opportunities in data science?"
79
+ "What are the ethical concerns surrounding AI?"
80
+ ]
81
+
82
+ sample_urls = [
83
+ https://www.healthline.com/nutrition/plant-based-diet-guide
84
+ https://www.ibm.com/quantum-computing/what-is-quantum-computing
85
+ https://climate.nasa.gov/evidence/
86
+ https://www.investopedia.com/terms/b/blockchain.asp
87
+ https://www.duolingo.com/
88
+ https://www.diabetes.org/diabetes
89
+ https://jamesclear.com/book-summaries
90
+ https://www.qualcomm.com/news/onq/2020/01/10/what-5g-and-how-it-changing-everything
91
+ https://datasciencedegree.wisconsin.edu/data-science/what-do-data-scientists-do/
92
+ https://aiethicslab.com/
93
+ ]
94
+
95
+ validator = URLValidator()
96
+
97
+ def 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
+
102
+ with 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
+
114
+ if _name_ == "_main_":
115
+ demo.launch(server_name="0.0.0.0", server_port=7860)