url_utils.py 783 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. from urllib.parse import urlparse
  3. import pandas as pd
  4. from modelscope.utils.logger import get_logger
  5. logger = get_logger()
  6. def valid_url(url) -> bool:
  7. try:
  8. result = urlparse(url)
  9. return all([result.scheme, result.netloc])
  10. except ValueError as e:
  11. logger.warning(e)
  12. return False
  13. def fetch_csv_with_url(csv_url: str) -> pd.DataFrame:
  14. """Fetch the csv content from url.
  15. Args:
  16. csv_url (str): The input url of csv data.
  17. Returns:
  18. A pandas DataFrame object which contains the csv content.
  19. """
  20. try:
  21. df = pd.read_csv(csv_url)
  22. except Exception as e:
  23. logger.error(f'Failed to fetch csv from url: {csv_url}')
  24. raise e
  25. return df