Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,102 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from deliverable2 import URLValidator
|
| 3 |
|
| 4 |
-
#
|
| 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 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|