SaiKumar1627 commited on
Commit
babc141
Β·
verified Β·
1 Parent(s): c05f206

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -52
app.py CHANGED
@@ -1,60 +1,102 @@
1
  import gradio as gr
 
2
  from deliverable2 import URLValidator
3
 
4
- # Initialize Validator
5
  validator = URLValidator()
6
 
 
7
  def validate_url(user_query, url_to_check):
 
8
  result = validator.rate_url_validity(user_query, url_to_check)
9
 
10
- # βœ… Ensure result is always a dictionary
11
- if not isinstance(result, dict):
12
- return {"error": "Unexpected error occurred, please try again."}
13
-
14
- # βœ… Check if an error occurred before accessing "raw_score"
15
- if "Validation Error" in result:
16
- return {"error": result["Validation Error"]}
17
-
18
- # βœ… Ensure "raw_score" exists in result
19
- raw_score = result.get("raw_score", {})
20
-
21
- return {
22
- "Domain Trust": raw_score.get("Domain Trust", "N/A"),
23
- "Content Relevance": raw_score.get("Content Relevance", "N/A"),
24
- "Fact-Check Score": raw_score.get("Fact-Check Score", "N/A"),
25
- "Bias Score": raw_score.get("Bias Score", "N/A"),
26
- "Final Validity Score": raw_score.get("Final Validity Score", "N/A"),
27
- "Star Rating": result.get("stars", {}).get("icon", "N/A"),
28
- "Explanation": result.get("explanation", "No explanation available.")
29
- }
30
-
31
-
32
- # UI Components
33
- query_options = [
34
- "How does climate change impact global weather?",
35
- "What are the latest advancements in AI?",
36
- "How does diet influence mental health?",
37
- "What are the effects of space travel on astronauts?",
38
- "Is cryptocurrency a safe investment?",
39
- "What are the advantages of renewable energy?",
40
- ]
41
-
42
- url_options = [
43
- "https://www.nationalgeographic.com/environment/article/climate-change",
44
- "https://www.technologyreview.com/2023/05/01/latest-ai-advancements/",
45
- "https://www.health.harvard.edu/mind-and-mood/foods-linked-to-better-brainpower",
46
- "https://www.nasa.gov/hrp/long-term-health-risks-of-space-travel",
47
- ]
48
-
49
- with gr.Blocks() as demo:
50
- gr.Markdown("# 🌍 URL Credibility Validator")
51
- gr.Markdown("Validate the credibility of any webpage using AI")
52
-
53
- user_query = gr.Dropdown(label="Select a search query:", choices=query_options)
54
- url_to_check = gr.Dropdown(label="Select a URL to validate:", choices=url_options)
55
- output = gr.JSON(label="Validation Results")
56
-
57
- btn = gr.Button("Validate URL")
58
- btn.click(fn=validate_url, inputs=[user_query, url_to_check], outputs=output)
59
-
60
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
  from deliverable2 import URLValidator
4
 
5
+ # βœ… Instantiate the URLValidator class
6
  validator = URLValidator()
7
 
8
+ # βœ… Function to validate a single query-URL pair
9
  def validate_url(user_query, url_to_check):
10
+ """Runs the credibility validation process and returns results."""
11
  result = validator.rate_url_validity(user_query, url_to_check)
12
 
13
+ # Extract relevant fields from the result dictionary
14
+ func_rating = round(result["raw_score"]["Final Validity Score"] / 20) # Convert to 1-5 scale
15
+ custom_rating = min(func_rating + 1, 5) # Ensure max rating is 5
16
+ explanation = result["explanation"]
17
+ stars = result["stars"]["icon"]
18
+
19
+ return func_rating, custom_rating, stars, explanation
20
+
21
+ # βœ… Batch processing for all queries & URLs
22
+ def validate_all():
23
+ """Runs validation for all 15 queries & URLs and saves to CSV."""
24
+ sample_queries = [
25
+ "How does artificial intelligence impact the job market?",
26
+ "What are the risks of genetically modified organisms (GMOs)?",
27
+ "What are the environmental effects of plastic pollution?",
28
+ "How does 5G technology affect human health?",
29
+ "What are the latest treatments for Alzheimer's disease?",
30
+ "Is red meat consumption linked to heart disease?",
31
+ "How does cryptocurrency mining impact the environment?",
32
+ "What are the benefits of electric cars?",
33
+ "How does sleep deprivation affect cognitive function?",
34
+ "What are the effects of social media on teenage mental health?",
35
+ "What are the ethical concerns of facial recognition technology?",
36
+ "How does air pollution contribute to lung diseases?",
37
+ "What are the potential dangers of artificial general intelligence?",
38
+ "How does meditation impact brain function?",
39
+ "What are the psychological effects of video game addiction?"
40
+ ]
41
+
42
+ sample_urls = [
43
+ "https://www.forbes.com/sites/forbestechcouncil/2023/10/15/impact-of-ai-on-the-job-market/",
44
+ "https://www.fda.gov/food/food-labeling-nutrition/consumers-guide-gmo-foods",
45
+ "https://www.nationalgeographic.com/environment/article/plastic-pollution",
46
+ "https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7453195/",
47
+ "https://www.alz.org/alzheimers-dementia/treatments",
48
+ "https://www.heart.org/en/news/2021/02/10/how-red-meat-affects-heart-health",
49
+ "https://www.scientificamerican.com/article/how-bitcoin-mining-impacts-the-environment/",
50
+ "https://www.tesla.com/blog/environmental-benefits-electric-cars",
51
+ "https://www.sleepfoundation.org/sleep-deprivation",
52
+ "https://www.psychologytoday.com/us/basics/teenagers-and-social-media",
53
+ "https://www.brookings.edu/research/facial-recognition-technology-ethical-concerns/",
54
+ "https://www.who.int/news-room/fact-sheets/detail/ambient-(outdoor)-air-quality-and-health",
55
+ "https://futureoflife.org/background/benefits-risks-of-artificial-intelligence/",
56
+ "https://www.mindful.org/meditation/mindfulness-getting-started/",
57
+ "https://www.apa.org/news/press/releases/stress/2020/video-games"
58
+ ]
59
+
60
+ # Process all queries & URLs
61
+ results = []
62
+ for query, url in zip(sample_queries, sample_urls):
63
+ func_rating, custom_rating, stars, explanation = validate_url(query, url)
64
+ results.append([query, url, func_rating, custom_rating, stars, explanation])
65
+
66
+ # Save results to CSV
67
+ df = pd.DataFrame(results, columns=["user_query", "url_to_check", "func_rating", "custom_rating", "stars", "explanation"])
68
+ df.to_csv("url_validation_results.csv", index=False)
69
+
70
+ return df
71
+
72
+ # βœ… Define the Gradio UI interface
73
+ with gr.Blocks() as app:
74
+ gr.Markdown("# 🌍 URL Credibility Validator πŸš€")
75
+ gr.Markdown("Enter a **query** and a **URL** to check its credibility.")
76
+
77
+ # User input fields
78
+ user_query = gr.Textbox(label="πŸ” User Query", placeholder="Enter your search query...")
79
+ url_to_check = gr.Textbox(label="🌐 URL to Validate", placeholder="Enter the URL...")
80
+
81
+ # Output fields
82
+ func_rating_output = gr.Textbox(label="πŸ”’ Function Rating (1-5)", interactive=False)
83
+ custom_rating_output = gr.Textbox(label="⭐ Custom Rating (1-5)", interactive=False)
84
+ stars_output = gr.Textbox(label="🌟 Star Rating", interactive=False)
85
+ explanation_output = gr.Textbox(label="πŸ“„ Explanation", interactive=False)
86
+
87
+ # Validate Button (Single Query)
88
+ validate_button = gr.Button("βœ… Validate URL")
89
+ validate_button.click(
90
+ validate_url,
91
+ inputs=[user_query, url_to_check],
92
+ outputs=[func_rating_output, custom_rating_output, stars_output, explanation_output]
93
+ )
94
+
95
+ # Batch Validate Button
96
+ gr.Markdown("## Validate All Predefined Queries & URLs")
97
+ batch_validate_button = gr.Button("πŸ“Š Validate All")
98
+ results_table = gr.DataFrame()
99
+ batch_validate_button.click(validate_all, outputs=results_table)
100
+
101
+ # βœ… Launch Gradio App
102
+ app.launch()