Inara132000 commited on
Commit
639d80a
·
verified ·
1 Parent(s): cccb2ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -33
app.py CHANGED
@@ -1,10 +1,18 @@
1
- import gradio as gr
2
  from deliverable2 import URLValidator
 
 
3
 
4
- # Instantiate the validator
5
- validator = URLValidator()
6
 
7
- # Updated queries and URLs
 
 
 
 
 
 
 
8
  queries = [
9
  "How blockchain works",
10
  "Climate change effects",
@@ -16,9 +24,10 @@ queries = [
16
  "Electric vehicle benefits",
17
  "History of the internet",
18
  "Nutritional benefits of a vegan diet",
19
- "Mental health awareness",
20
  ]
21
 
 
22
  urls = [
23
  "https://www.ibm.com/topics/what-is-blockchain",
24
  "https://www.nationalgeographic.com/environment/article/climate-change-overview",
@@ -33,31 +42,15 @@ urls = [
33
  "https://www.who.int/news-room/fact-sheets/detail/mental-health-strengthening-our-response"
34
  ]
35
 
36
- # Function to validate URL
37
- def validate_url(user_query, url_to_check):
38
- result = validator.rate_url_validity(user_query, url_to_check)
39
-
40
- if "Validation Error" in result:
41
- return {"Error": result["Validation Error"]}
42
-
43
- return {
44
- "Content Relevance Score": f"{result['raw_score']['Content Relevance']} / 100",
45
- "Bias Score": f"{result['raw_score']['Bias Score']} / 100",
46
- "Final Validity Score": f"{result['raw_score']['Final Validity Score']} / 100"
47
- }
48
-
49
- # Gradio UI
50
- with gr.Blocks() as app:
51
- gr.Markdown("# 🌍 URL Credibility Validator")
52
- gr.Markdown("### Validate the credibility of any webpage using AI")
53
-
54
- user_query = gr.Dropdown(queries, label="Select a search query:")
55
- url_to_check = gr.Dropdown(urls, label="Select a URL to validate:")
56
-
57
- result_output = gr.JSON(label="Validation Results")
58
-
59
- submit_button = gr.Button("Validate URL")
60
- submit_button.click(validate_url, inputs=[user_query, url_to_check], outputs=result_output)
61
-
62
- # Launch the app
63
- app.launch(server_name="0.0.0.0", server_port=7860)
 
 
1
  from deliverable2 import URLValidator
2
+ import os
3
+ import json
4
 
5
+ # Retrieve API key from environment variables
6
+ SERPAPI_API_KEY = os.getenv("SERPAPI_API_KEY")
7
 
8
+ # Ensure API key is provided before initializing
9
+ if not SERPAPI_API_KEY:
10
+ raise ValueError("SERPAPI_API_KEY is not set. Please configure your environment variables.")
11
+
12
+ # Initialize the validator correctly
13
+ validator = URLValidator(serpapi_key=SERPAPI_API_KEY)
14
+
15
+ # List of queries
16
  queries = [
17
  "How blockchain works",
18
  "Climate change effects",
 
24
  "Electric vehicle benefits",
25
  "History of the internet",
26
  "Nutritional benefits of a vegan diet",
27
+ "Mental health awareness"
28
  ]
29
 
30
+ # Corresponding URLs
31
  urls = [
32
  "https://www.ibm.com/topics/what-is-blockchain",
33
  "https://www.nationalgeographic.com/environment/article/climate-change-overview",
 
42
  "https://www.who.int/news-room/fact-sheets/detail/mental-health-strengthening-our-response"
43
  ]
44
 
45
+ # Process queries and URLs separately but use them together
46
+ results = []
47
+ for query, url in zip(queries, urls):
48
+ result = validator.rate_url_validity(query, url)
49
+ results.append({
50
+ "Query": query,
51
+ "URL": url,
52
+ "Result": result
53
+ })
54
+
55
+ # Print formatted output
56
+ print(json.dumps(results, indent=2))