Inara132000 commited on
Commit
a96aba1
·
verified ·
1 Parent(s): a7e3938

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -48
app.py CHANGED
@@ -1,17 +1,8 @@
1
- import os
2
- import json
3
- from deliverable2 import URLValidator
4
- from transformers import pipeline
5
 
6
- # Retrieve API key from environment variables
7
- SERPAPI_API_KEY = os.getenv("SERPAPI_API_KEY")
8
-
9
- # Ensure API key is provided before initializing
10
- if not SERPAPI_API_KEY:
11
- raise ValueError("SERPAPI_API_KEY is not set. Please configure your environment variables.")
12
-
13
- # Initialize the validator correctly
14
- validator = URLValidator(serpapi_key=SERPAPI_API_KEY)
15
 
16
  # List of queries
17
  queries = [
@@ -43,41 +34,31 @@ urls = [
43
  "https://www.who.int/news-room/fact-sheets/detail/mental-health-strengthening-our-response"
44
  ]
45
 
46
- # Process queries and URLs separately but use them together
47
- results = []
48
- for query, url in zip(queries, urls):
49
- try:
50
- # Get content from the URL
51
- content = validator.fetch_page_content(url)
52
- if content:
53
- # Get the fake news score and relevance score
54
- fake_news_score = validator.get_domain_trust_huggingface(content) # Fix for long text
55
- relevance_score = validator.get_content_relevance(query, content)
 
 
 
 
 
 
 
56
 
57
- # Store the results
58
- results.append({
59
- "Query": query,
60
- "URL": url,
61
- "Trust": fake_news_score,
62
- "Relevance": relevance_score
63
- })
64
- else:
65
- results.append({
66
- "Query": query,
67
- "URL": url,
68
- "Trust": "Error fetching content",
69
- "Relevance": 0.0
70
- })
71
 
72
- except Exception as e:
73
- # Handle any unexpected errors
74
- results.append({
75
- "Query": query,
76
- "URL": url,
77
- "Trust": "Error",
78
- "Relevance": "Error",
79
- "Error": str(e)
80
- })
81
 
82
- # Print formatted output
83
- print(json.dumps(results, indent=2))
 
1
+ import gradio as gr
2
+ from Deliverable2 import URLValidator
 
 
3
 
4
+ # Instantiate the validator
5
+ validator = URLValidator()
 
 
 
 
 
 
 
6
 
7
  # List of queries
8
  queries = [
 
34
  "https://www.who.int/news-room/fact-sheets/detail/mental-health-strengthening-our-response"
35
  ]
36
 
37
+ # Function to validate URL
38
+ def validate_url(user_query, url_to_check):
39
+ result = validator.rate_url_validity(user_query, url_to_check)
40
+
41
+ if "Validation Error" in result:
42
+ return {"Error": result["Validation Error"]}
43
+
44
+ return {
45
+ "Content Relevance Score": f"{result['raw_score']['Content Relevance']} / 100",
46
+ "Bias Score": f"{result['raw_score']['Bias Score']} / 100",
47
+ "Final Validity Score": f"{result['raw_score']['Final Validity Score']} / 100"
48
+ }
49
+
50
+ # Gradio UI
51
+ with gr.Blocks() as app:
52
+ gr.Markdown("# 🌍 URL Credibility Validator")
53
+ gr.Markdown("### Validate the credibility of any webpage using AI")
54
 
55
+ user_query = gr.Dropdown(queries, label="Select a search query:")
56
+ url_to_check = gr.Dropdown(urls, label="Select a URL to validate:")
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ result_output = gr.JSON(label="Validation Results")
59
+
60
+ submit_button = gr.Button("Validate URL")
61
+ submit_button.click(validate_url, inputs=[user_query, url_to_check], outputs=result_output)
 
 
 
 
 
62
 
63
+ # Launch the app
64
+ app.launch(server_name="0.0.0.0", server_port=7860)