Project2 / app.py
SaiKumar1627's picture
Update app.py
babc141 verified
raw
history blame
4.99 kB
import gradio as gr
import pandas as pd
from deliverable2 import URLValidator
# βœ… Instantiate the URLValidator class
validator = URLValidator()
# βœ… Function to validate a single query-URL pair
def validate_url(user_query, url_to_check):
"""Runs the credibility validation process and returns results."""
result = validator.rate_url_validity(user_query, url_to_check)
# Extract relevant fields from the result dictionary
func_rating = round(result["raw_score"]["Final Validity Score"] / 20) # Convert to 1-5 scale
custom_rating = min(func_rating + 1, 5) # Ensure max rating is 5
explanation = result["explanation"]
stars = result["stars"]["icon"]
return func_rating, custom_rating, stars, explanation
# βœ… Batch processing for all queries & URLs
def validate_all():
"""Runs validation for all 15 queries & URLs and saves to CSV."""
sample_queries = [
"How does artificial intelligence impact the job market?",
"What are the risks of genetically modified organisms (GMOs)?",
"What are the environmental effects of plastic pollution?",
"How does 5G technology affect human health?",
"What are the latest treatments for Alzheimer's disease?",
"Is red meat consumption linked to heart disease?",
"How does cryptocurrency mining impact the environment?",
"What are the benefits of electric cars?",
"How does sleep deprivation affect cognitive function?",
"What are the effects of social media on teenage mental health?",
"What are the ethical concerns of facial recognition technology?",
"How does air pollution contribute to lung diseases?",
"What are the potential dangers of artificial general intelligence?",
"How does meditation impact brain function?",
"What are the psychological effects of video game addiction?"
]
sample_urls = [
"https://www.forbes.com/sites/forbestechcouncil/2023/10/15/impact-of-ai-on-the-job-market/",
"https://www.fda.gov/food/food-labeling-nutrition/consumers-guide-gmo-foods",
"https://www.nationalgeographic.com/environment/article/plastic-pollution",
"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7453195/",
"https://www.alz.org/alzheimers-dementia/treatments",
"https://www.heart.org/en/news/2021/02/10/how-red-meat-affects-heart-health",
"https://www.scientificamerican.com/article/how-bitcoin-mining-impacts-the-environment/",
"https://www.tesla.com/blog/environmental-benefits-electric-cars",
"https://www.sleepfoundation.org/sleep-deprivation",
"https://www.psychologytoday.com/us/basics/teenagers-and-social-media",
"https://www.brookings.edu/research/facial-recognition-technology-ethical-concerns/",
"https://www.who.int/news-room/fact-sheets/detail/ambient-(outdoor)-air-quality-and-health",
"https://futureoflife.org/background/benefits-risks-of-artificial-intelligence/",
"https://www.mindful.org/meditation/mindfulness-getting-started/",
"https://www.apa.org/news/press/releases/stress/2020/video-games"
]
# Process all queries & URLs
results = []
for query, url in zip(sample_queries, sample_urls):
func_rating, custom_rating, stars, explanation = validate_url(query, url)
results.append([query, url, func_rating, custom_rating, stars, explanation])
# Save results to CSV
df = pd.DataFrame(results, columns=["user_query", "url_to_check", "func_rating", "custom_rating", "stars", "explanation"])
df.to_csv("url_validation_results.csv", index=False)
return df
# βœ… Define the Gradio UI interface
with gr.Blocks() as app:
gr.Markdown("# 🌍 URL Credibility Validator πŸš€")
gr.Markdown("Enter a **query** and a **URL** to check its credibility.")
# User input fields
user_query = gr.Textbox(label="πŸ” User Query", placeholder="Enter your search query...")
url_to_check = gr.Textbox(label="🌐 URL to Validate", placeholder="Enter the URL...")
# Output fields
func_rating_output = gr.Textbox(label="πŸ”’ Function Rating (1-5)", interactive=False)
custom_rating_output = gr.Textbox(label="⭐ Custom Rating (1-5)", interactive=False)
stars_output = gr.Textbox(label="🌟 Star Rating", interactive=False)
explanation_output = gr.Textbox(label="πŸ“„ Explanation", interactive=False)
# Validate Button (Single Query)
validate_button = gr.Button("βœ… Validate URL")
validate_button.click(
validate_url,
inputs=[user_query, url_to_check],
outputs=[func_rating_output, custom_rating_output, stars_output, explanation_output]
)
# Batch Validate Button
gr.Markdown("## Validate All Predefined Queries & URLs")
batch_validate_button = gr.Button("πŸ“Š Validate All")
results_table = gr.DataFrame()
batch_validate_button.click(validate_all, outputs=results_table)
# βœ… Launch Gradio App
app.launch()