Spaces:
Sleeping
Sleeping
File size: 2,003 Bytes
c8b1fd7 c2f3fc9 c8b1fd7 | 1 2 3 4 5 6 7 8 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(PROJECT_ROOT))
import json
import requests
from bs4 import BeautifulSoup
from ai.sarvam_client import generate_response, extract_json
def get_page_details(url: str):
"""
Fetch webpage details.
"""
try:
response = requests.get(url, timeout=10)
soup = BeautifulSoup(response.text, "html.parser")
title = soup.title.string.strip() if soup.title else "Unknown"
domain = requests.utils.urlparse(url).netloc
return {
"title": title,
"domain": domain,
"status": response.status_code
}
except Exception:
return {
"title": "Unknown",
"domain": "",
"status": 0
}
def create_source_prompt(source):
prompt = f"""
You are an expert source credibility analyst.
Analyze the following website.
Title:
{source["title"]}
Domain:
{source["domain"]}
Status Code:
{source["status"]}
Return ONLY valid JSON.
{{
"credibility_score":85,
"reliability":"High",
"reason":"Short explanation."
}}
"""
return prompt
def get_source_analysis(url):
source = get_page_details(url)
prompt = create_source_prompt(source)
return generate_response(prompt)
def parse_model_response(response):
if response is None:
return {
"credibility_score": 0,
"reliability": "Unknown",
"reason": "No response received from Sarvam AI."
}
parsed = extract_json(response)
if parsed is None:
return {
"credibility_score": 0,
"reliability": "Unknown",
"reason": "Unable to analyze source."
}
return parsed
def validate_source(url):
raw = get_source_analysis(url)
return parse_model_response(raw)
if __name__ == "__main__":
sample = "https://www.bbc.com"
print(validate_source(sample)) |