Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -73,19 +73,35 @@ class URLProcessor:
|
|
73 |
if not validators.url(url):
|
74 |
return {'is_valid': False, 'message': 'Invalid URL format'}
|
75 |
|
76 |
-
#
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
return {'is_valid': True, 'message': 'URL is valid and accessible'}
|
89 |
except Exception as e:
|
90 |
logger.error(f"URL validation failed for {url}: {str(e)}")
|
91 |
return {'is_valid': False, 'message': f'URL validation failed: {str(e)}'}
|
@@ -496,13 +512,48 @@ def create_interface():
|
|
496 |
""")
|
497 |
return interface
|
498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
499 |
def main():
|
500 |
# Configure system settings
|
501 |
mimetypes.init()
|
502 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
# Create and launch interface
|
504 |
interface = create_interface()
|
505 |
-
|
506 |
# Launch with proper configuration
|
507 |
interface.launch(
|
508 |
server_name="0.0.0.0",
|
|
|
73 |
if not validators.url(url):
|
74 |
return {'is_valid': False, 'message': 'Invalid URL format'}
|
75 |
|
76 |
+
# Try with DNS resolution retry
|
77 |
+
for attempt in range(3): # Try up to 3 times
|
78 |
+
try:
|
79 |
+
# Some sites block HEAD requests but allow GET
|
80 |
+
try:
|
81 |
+
response = self.session.head(url, timeout=self.timeout)
|
82 |
+
response.raise_for_status()
|
83 |
+
except (requests.exceptions.RequestException, Exception) as e:
|
84 |
+
logger.warning(f"HEAD request failed for {url}, trying GET: {e}")
|
85 |
+
# Try with GET request if HEAD fails
|
86 |
+
response = self.session.get(url, timeout=self.timeout, stream=True)
|
87 |
+
response.raise_for_status()
|
88 |
+
# Close the connection to avoid downloading the entire content
|
89 |
+
response.close()
|
90 |
+
|
91 |
+
return {'is_valid': True, 'message': 'URL is valid and accessible'}
|
92 |
+
except requests.exceptions.ConnectionError as e:
|
93 |
+
if "NameResolutionError" in str(e) or "Failed to resolve" in str(e):
|
94 |
+
logger.warning(f"DNS resolution failed for {url}, attempt {attempt+1}/3")
|
95 |
+
time.sleep(1) # Wait a bit before retrying
|
96 |
+
continue
|
97 |
+
else:
|
98 |
+
raise
|
99 |
+
except Exception as e:
|
100 |
+
raise
|
101 |
+
|
102 |
+
# If we get here, all attempts failed
|
103 |
+
return {'is_valid': False, 'message': f'URL validation failed: DNS resolution failed after multiple attempts'}
|
104 |
|
|
|
105 |
except Exception as e:
|
106 |
logger.error(f"URL validation failed for {url}: {str(e)}")
|
107 |
return {'is_valid': False, 'message': f'URL validation failed: {str(e)}'}
|
|
|
512 |
""")
|
513 |
return interface
|
514 |
|
515 |
+
def check_network_connectivity():
|
516 |
+
"""Check if the network is working properly by testing connection to common sites"""
|
517 |
+
test_sites = ["https://www.google.com", "https://www.cloudflare.com", "https://www.amazon.com"]
|
518 |
+
results = []
|
519 |
+
|
520 |
+
for site in test_sites:
|
521 |
+
try:
|
522 |
+
response = requests.get(site, timeout=5)
|
523 |
+
results.append({
|
524 |
+
"site": site,
|
525 |
+
"status": "OK" if response.status_code == 200 else f"Error: {response.status_code}",
|
526 |
+
"response_time": response.elapsed.total_seconds()
|
527 |
+
})
|
528 |
+
except Exception as e:
|
529 |
+
results.append({
|
530 |
+
"site": site,
|
531 |
+
"status": f"Error: {str(e)}",
|
532 |
+
"response_time": None
|
533 |
+
})
|
534 |
+
|
535 |
+
# If all sites failed, there might be a network issue
|
536 |
+
if all(result["status"].startswith("Error") for result in results):
|
537 |
+
logger.error("Network connectivity issue detected. All test sites failed.")
|
538 |
+
return False, results
|
539 |
+
|
540 |
+
return True, results
|
541 |
+
|
542 |
+
# Add this to the main function
|
543 |
def main():
|
544 |
# Configure system settings
|
545 |
mimetypes.init()
|
546 |
+
|
547 |
+
# Check network connectivity
|
548 |
+
network_ok, network_results = check_network_connectivity()
|
549 |
+
if not network_ok:
|
550 |
+
logger.warning("Network connectivity issues detected. Some features may not work properly.")
|
551 |
+
for result in network_results:
|
552 |
+
logger.warning(f"Test site {result['site']}: {result['status']}")
|
553 |
+
|
554 |
# Create and launch interface
|
555 |
interface = create_interface()
|
556 |
+
|
557 |
# Launch with proper configuration
|
558 |
interface.launch(
|
559 |
server_name="0.0.0.0",
|