Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -43,48 +43,4 @@ async def analyze_text(payload: TextPayload):
|
|
| 43 |
"status": "success",
|
| 44 |
"metrics": {"total_words": len(doc), "total_entities_found": len(sorted_entities)},
|
| 45 |
"entities": sorted_entities
|
| 46 |
-
}
|
| 47 |
-
exit;
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
// 2. Clean the text for AI analysis
|
| 51 |
-
$doc = new DOMDocument();
|
| 52 |
-
libxml_use_internal_errors(true);
|
| 53 |
-
@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
|
| 54 |
-
$xpath = new DOMXPath($doc);
|
| 55 |
-
|
| 56 |
-
// Strip out non-content elements to save AI tokens
|
| 57 |
-
foreach ($xpath->query('//script | //style | //nav | //footer | //header | //aside | //noscript') as $node) {
|
| 58 |
-
$node->parentNode->removeChild($node);
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
$body = $doc->getElementsByTagName('body')->item(0);
|
| 62 |
-
$clean_text = $body ? trim(preg_replace('/\s+/', ' ', $body->textContent)) : '';
|
| 63 |
-
$clean_text = substr($clean_text, 0, 8000);
|
| 64 |
-
|
| 65 |
-
if (empty($clean_text)) {
|
| 66 |
-
echo json_encode(['error' => 'We reached the site, but couldn\'t find any readable text in the body.']);
|
| 67 |
-
exit;
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
// 3. Talk to Hugging Face API
|
| 71 |
-
$api_url = 'https://webtraffic-seo-entity-analyzer.hf.space/analyze';
|
| 72 |
-
|
| 73 |
-
$ch = curl_init($api_url);
|
| 74 |
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
| 75 |
-
curl_setopt($ch, CURLOPT_POST, true);
|
| 76 |
-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['text' => $clean_text]));
|
| 77 |
-
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
| 78 |
-
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
| 79 |
-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
| 80 |
-
|
| 81 |
-
$response = curl_exec($ch);
|
| 82 |
-
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
| 83 |
-
curl_close($ch);
|
| 84 |
-
|
| 85 |
-
if ($http_code !== 200) {
|
| 86 |
-
echo json_encode(['error' => 'AI Engine is currently overloaded or waking up. Please try again in a moment.']);
|
| 87 |
-
exit;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
echo $response;
|
|
|
|
| 43 |
"status": "success",
|
| 44 |
"metrics": {"total_words": len(doc), "total_entities_found": len(sorted_entities)},
|
| 45 |
"entities": sorted_entities
|
| 46 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|