我是指针的狗 2024-05-11 14:27 采纳率: 25%
浏览 1487
已结题

python中transformers可以正常下载,但是没有办法使用pipeline

python中的transformers是可以下载下来并且正常import的,但是却没有办法使用
这里是一个最简单的代码

from transformers import pipeline

classifier = pipeline(task='sentiment-analysis')

res = classifier('Today is a nice day!')

print(res)

如果我开clash的话,会产生如下报错:

D:\桌面\pythonProject1\venv\Scripts\python.exe D:\桌面\pythonProject1\mne\transformers_try.py 
2024-05-11 22:23:00.272259: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-05-11 22:23:00.872298: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
  warnings.warn(
urllib3.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:1129)

The above exception was the direct cause of the following exception:

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /distilbert/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)'))))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\mne\transformers_try.py", line 3, in <module>
    classifier = pipeline(task='sentiment-analysis')
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\pipelines\__init__.py", line 875, in pipeline
    config = AutoConfig.from_pretrained(model, _from_pipeline=task, **hub_kwargs, **model_kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\models\auto\configuration_auto.py", line 928, in from_pretrained
    config_dict, unused_kwargs = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\configuration_utils.py", line 631, in get_config_dict
    config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\configuration_utils.py", line 686, in _get_config_dict
    resolved_config_file = cached_file(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\utils\hub.py", line 398, in cached_file
    resolved_file = hf_hub_download(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\utils\_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1221, in hf_hub_download
    return _hf_hub_download_to_cache_dir(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1282, in _hf_hub_download_to_cache_dir
    (url_to_download, etag, commit_hash, expected_size, head_call_error) = _get_metadata_or_catch_error(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1722, in _get_metadata_or_catch_error
    metadata = get_hf_file_metadata(url=url, proxies=proxies, timeout=etag_timeout, headers=headers)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\utils\_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1645, in get_hf_file_metadata
    r = _request_wrapper(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 372, in _request_wrapper
    response = _request_wrapper(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 395, in _request_wrapper
    response = get_session().request(method=method, url=url, **params)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\utils\_http.py", line 66, in send
    return super().send(request, *args, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /distilbert/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)'))))"), '(Request ID: fca6c479-9a60-4f19-a4ae-f296560ca597)')

Process finished with exit code 1

如果我不开clash的话,也会产生如下报错:


D:\桌面\pythonProject1\venv\Scripts\python.exe D:\桌面\pythonProject1\mne\transformers_try.py 
2024-05-11 22:24:11.833690: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-05-11 22:24:12.441548: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
  warnings.warn(
Traceback (most recent call last):
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connection.py", line 198, in _new_conn
    sock = connection.create_connection(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    raise err
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
socket.timeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connectionpool.py", line 793, in urlopen
    response = self._make_request(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connectionpool.py", line 1099, in _validate_conn
    conn.connect()
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connection.py", line 616, in connect
    self.sock = sock = self._new_conn()
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connection.py", line 207, in _new_conn
    raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x0000017951631F40>, 'Connection to huggingface.co timed out. (connect timeout=10)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /distilbert/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x0000017951631F40>, 'Connection to huggingface.co timed out. (connect timeout=10)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1722, in _get_metadata_or_catch_error
    metadata = get_hf_file_metadata(url=url, proxies=proxies, timeout=etag_timeout, headers=headers)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\utils\_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1645, in get_hf_file_metadata
    r = _request_wrapper(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 372, in _request_wrapper
    response = _request_wrapper(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 395, in _request_wrapper
    response = get_session().request(method=method, url=url, **params)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\utils\_http.py", line 66, in send
    return super().send(request, *args, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\requests\adapters.py", line 507, in send
    raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /distilbert/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x0000017951631F40>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 142bce7b-1f54-4c29-886c-37ff87abdbe2)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\utils\hub.py", line 398, in cached_file
    resolved_file = hf_hub_download(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\utils\_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1221, in hf_hub_download
    return _hf_hub_download_to_cache_dir(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1325, in _hf_hub_download_to_cache_dir
    _raise_on_head_call_error(head_call_error, force_download, local_files_only)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\huggingface_hub\file_download.py", line 1826, in _raise_on_head_call_error
    raise LocalEntryNotFoundError(
huggingface_hub.utils._errors.LocalEntryNotFoundError: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\桌面\pythonProject1\mne\transformers_try.py", line 3, in <module>
    classifier = pipeline(task='sentiment-analysis')
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\pipelines\__init__.py", line 875, in pipeline
    config = AutoConfig.from_pretrained(model, _from_pipeline=task, **hub_kwargs, **model_kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\models\auto\configuration_auto.py", line 928, in from_pretrained
    config_dict, unused_kwargs = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\configuration_utils.py", line 631, in get_config_dict
    config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\configuration_utils.py", line 686, in _get_config_dict
    resolved_config_file = cached_file(
  File "D:\桌面\pythonProject1\venv\lib\site-packages\transformers\utils\hub.py", line 441, in cached_file
    raise EnvironmentError(
OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like distilbert/distilbert-base-uncased-finetuned-sst-2-english is not the path to a directory containing a file named config.json.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.

Process finished with exit code 1

总之就是连和huggingface.co的连接应该是有问题的,连最基本的模型都没办法导入,请各位指点一下应该具体怎么改进,能在不把模型下到本地的情况下完成对transformers的应用,也就是解决这里面的网络问题

展开全部

  • 写回答

18条回答

  • 专家-皆知 2024-05-11 14:28
    关注
    获得0.15元问题酬金

    请确保已安装transformers的依赖库,如torch,并正确导入pipeline。

    以上回复有部分引自免费微信小程序:皆我百晓生。 如问题尚未解决,还请稍待,如有疑问请回复进一步沟通。

    评论
  • 关注
    获得0.45元问题酬金

    晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    问题描述:python中的transformers可以正常下载,但是无法使用pipeline。

    问题分析:根据报错信息可以看出,出现了SSL错误和未连接代理的错误,这可能是使用了Python的requests模块,而代理设置有误导致的。

    解决方案:

    1. 检查代理设置是否正确:参考urllib3.exceptions.ProxyError的结果,代理设置可能存在问题,需要检查代理IP和端口是否正确。
    2. 关闭代理:如果不需要使用代理,直接关闭代理即可。
    3. 添加证书:根据报错信息中的_ssl.c:1129可以看出是SSL证书问题,可能需要使用受信任的证书。可以尝试手动添加huggingface.co的证书,并确保使用的是TLS1.2及以上协议。
    4. 更新requests模块:如果问题仍然未解决,可以尝试使用最新的requests模块,有些BUG可能会在更新后得到修复。

    修改完整代码如下:

    from transformers import pipeline
    import urllib3
    
    urllib3.disable_warnings() # 禁用警告信息
    
    # 关闭代理
    import os
    os.environ['NO_PROXY'] = 'huggingface.co'
    
    classifier = pipeline(task='sentiment-analysis')
    
    res = classifier('Today is a nice day!')
    
    print(res)
    
    评论
  • 码农阿豪@新空间 Java领域优质创作者 2024-05-11 14:28
    关注
    获得0.45元问题酬金
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    这个问题的主要原因可能是代理设置不正确或无效。可以尝试关闭代理,或者修改代理设置以解决此问题。同时,还可以尝试更新transformers库,以确保使用的是最新的版本。 以下是修改代理设置的示例代码:
    import requests
    proxies = {
      'http': 'http://myproxy:1234',
      'https': 'https://myproxy:5678'
    }
    # 在requests请求中加入proxies参数
    response = requests.get('https://www.example.com', proxies=proxies)
    

    在这个例子中,我们使用了一个名为proxies 的字典来指定http和https代理。然后,在发送请求时,将这个字典作为参数传递给requests请求。 需要注意的是,代理地址和端口号应该根据实际的代理设置进行修改。

    评论
  • 阿齐Archie 嵌入式领域优质创作者 2024-05-11 14:28
    关注
    获得0.30元问题酬金

    以下参考免费微信小程序:皆我百晓生 作答,并由阿齐Archie整理回复,若还有疑问可评论或留言。

    从您提供的错误信息来看,问题主要出在网络连接上,特别是在尝试从 Hugging Face 的服务器下载模型时。这里有几个可能的解决方案:

    1. 检查网络连接:确保您的网络连接是稳定的,并且没有任何防火墙或代理设置阻止您连接到 huggingface.co

    2. 使用国内镜像:如果您在中国大陆,可能需要使用国内的镜像来加速下载。例如,可以使用清华大学的镜像:

      classifier = pipeline(task='sentiment-analysis', model='distilbert-base-chinese')
      
    3. 手动下载模型:如果自动下载失败,您可以尝试手动下载模型并将其保存在本地。然后,您可以在 pipeline 中指定模型的本地路径。例如:

      from transformers import pipeline
      
      # 假设您已经下载了模型并解压到本地路径 './local_model_directory/'
      classifier = pipeline(task='sentiment-analysis', model='./local_model_directory/')
      res = classifier('Today is a nice day!')
      print(res)
      
    4. 使用离线模式:如果您无法连接到互联网,可以尝试使用 Transformers 库的离线模式。这需要您提前下载所有需要的模型和文件,并在运行脚本时设置环境变量 TRANSFORMERS_OFFLINE=1。更多信息可以参考官方文档:https://huggingface.co/docs/transformers/installation#offline-mode

    5. 调整连接超时设置:如果网络连接较慢,您可以尝试增加连接超时的时间。这可以通过修改 requests 库的超时设置来实现,但这通常不是推荐的做法,因为它可能会掩盖潜在的网络问题。

    6. 检查系统防火墙和安全软件设置:确保您的防火墙或安全软件没有阻止 Python 或 Transformers 库访问网络。

    7. 使用 VPN 或代理:如果您所在的网络环境限制了对某些网站的访问,您可能需要使用 VPN 或代理来解决这个问题。

    8. 更新库:确保您的 transformershuggingface_hub 库是最新版本的,因为旧版本可能存在已知的网络问题。

    尝试上述解决方案后,如果问题仍然存在,您可能需要进一步诊断网络问题或联系您的网络管理员以获取帮助。

    评论
  • m0_904277151 2024-05-11 14:28
    关注
    获得0.45元问题酬金

    以下参考免费微信小程序:皆我百晓生 作答,并由妄北y整理回复。

    根据您提供的错误信息,这个问题主要是由于网络连接问题导致的。无论是使用代理(clash)还是直接连接,都无法成功地从 Hugging Face 的服务器下载模型。以下是一些可能的解决方案:

    1. 检查网络连接:确保您的网络连接是稳定的,并且没有任何防火墙或路由器设置阻止您访问 huggingface.co

    2. 使用国内镜像:如果您在中国大陆,可能需要使用国内的镜像源来下载模型。例如,使用阿里云的镜像源:

      classifier = pipeline(task='sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english', use_auth_token=True)
      

      然后,设置环境变量 TRANSFORMERS_CACHE 为阿里云的镜像地址:

      set TRANSFORMERS_CACHE=/root/.cache/torch/hub/checkpoints
      
    3. 手动下载模型:如果在线下载始终失败,您可以尝试手动下载模型文件并将其保存到本地。然后,使用 from_pretrained 方法加载本地模型:

      from transformers import AutoModelForSequenceClassification, AutoTokenizer
      
      model_path = 'path_to_manually_downloaded_model_directory'
      model = AutoModelForSequenceClassification.from_pretrained(model_path)
      tokenizer = AutoTokenizer.from_pretrained(model_path)
      
      classifier = pipeline(task='sentiment-analysis', model=model, tokenizer=tokenizer)
      
    4. 使用代理:如果您的网络环境需要使用代理,确保您的代理设置是正确的,并且代理服务器允许您访问外部网络。

    5. 增加连接超时时间:在某些情况下,增加连接超时时间可能会有所帮助。您可以在 pipeline 调用中添加 timeout 参数:

      classifier = pipeline(task='sentiment-analysis', timeout=60)
      
    6. 检查 DNS 设置:确保您的 DNS 设置是正确的,或者尝试使用公共 DNS 服务,如 Google 的 8.8.8.8

    7. 使用 VPN:如果您所在的网络环境对访问外部资源有限制,使用 VPN 可能会有所帮助。

    8. 联系您的网络管理员:如果您在公司或学校的网络环境中遇到问题,可能需要联系网络管理员以解决访问限制。

    请尝试上述解决方案,看看是否能够解决您的问题。如果问题仍然存在,请提供更多的网络环境信息,以便进一步分析。

    评论
  • 叫兽-郭老师 Java领域新星创作者 2024-05-11 14:28
    关注
    获得0.45元问题酬金

    🕗晚上好,题主!目前:🟢[在线]
    📢参考通义千问、由郭老师整理提供!
    📣今日诗号:若似月轮终皎洁,不辞冰雪为卿热。

    根据你提供的错误信息,看起来你在使用Hugging Face Transformers时遇到了连接Hugging Face库的问题。这个问题可能是由于网络限制(如代理、防火墙或超时)导致的。以下是一些可能的解决方案:

    1. 检查网络连接: 确保你的设备已连接到互联网,并且没有防火墙或代理设置阻止访问Hugging Face的服务器。如果你使用的是代理服务器,请确保它允许访问huggingface.co

    2. 修改代理设置: 如果你必须通过代理访问,你可以尝试在代码中设置代理。首先,安装requests库(如果尚未安装):

      pip install requests
      

      然后,在运行代码前,设置代理:

      import os
      os.environ["HTTP_PROXY"] = "http://your_proxy_url:port"
      os.environ["HTTPS_PROXY"] = "https://your_proxy_url:port"
      

      请替换your_proxy_urlport为你的实际代理设置。

    3. 离线模式: 如果你不想通过网络下载模型,可以尝试在本地下载模型并将其添加到~/.cache/huggingface/hub目录。首先,从Hugging Face下载模型:

      !pip install --upgrade --no-cache-dir transformers
      !transformers-cli login -u your_username -p your_password
      !transformers-cli repo clone huggingface/dummy-model ./
      

      然后在代码中指定本地模型路径:

      from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
      model_path = "dummy-model"  # 替换为你的本地模型路径
      model = AutoModelForSequenceClassification.from_pretrained(model_path)
      tokenizer = AutoTokenizer.from_pretrained(model_path)
      classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
      
    4. 环境变量: 关于TF_ENABLE_ONEDNN_OPTS的警告,这通常与TensorFlow有关,但不是造成连接问题的原因。你可以忽略这个警告,它不会影响你的代码运行。

    5. 重试或等待: 如果网络问题暂时存在,你可以尝试稍后再运行代码,看看是否能解决问题。

    请根据你的实际情况尝试上述方法。如果问题仍然存在,可能需要进一步检查你的网络环境或联系网络管理员。

    评论
  • threenewbee 2024-05-11 15:54
    关注
    获得0.45元问题酬金
    评论
  • Java毕设王 2024-05-11 17:31
    关注
    获得0.45元问题酬金

    引用讯飞星火
    从报错信息来看,问题可能是由于代理设置导致的。你可以尝试关闭代理或者检查代理设置是否正确。如果你不需要使用代理,可以在代码中添加以下内容来禁用代理:

    import os
    os.environ['http_proxy'] = ''
    os.environ['https_proxy'] = ''
    

    将这段代码添加到你的脚本开头,然后再尝试运行。

    评论
  • 唯有遗忘最漫长 2024-05-12 02:10
    关注
    获得0.15元问题酬金

    看起来你的代码在尝试从 Hugging Face Hub 下载模型时遇到了代理错误。错误信息中提到了 ProxyError 和 SSLEOFError,这可能是由于你的网络环境或代理设置导致的问题。

    以下是一些可能的解决方法:

    1. 检查网络连接和代理设置: 确保你的网络连接正常,并且没有设置任何代理或者代理设置是正确的。有时候代理设置不正确或者网络连接不稳定会导致无法正常连接到 Hugging Face Hub。

    2. 尝试更改网络环境: 如果你在使用公司或学校的网络,可能存在网络限制或者防火墙设置,尝试切换到其他网络环境,比如家庭网络或者移动热点,看是否可以解决问题。

    3. 尝试禁用代理: 如果你在使用代理,请尝试禁用代理并重新运行代码,看是否可以正常连接到 Hugging Face Hub。

    4. 尝试使用清华镜像源: 如果以上方法都无法解决问题,可以尝试使用清华大学的镜像源来安装模型。你可以通过修改 ~/.pip/pip.conf 文件(如果是 Windows,可能在 %APPDATA%\pip\pip.ini)来设置清华镜像源:

      [global]
      index-url = https://pypi.tuna.tsinghua.edu.cn/simple
      

      然后重新运行安装命令:

      pip install transformers
      
    5. 检查 SSL 配置: 如果仍然遇到 SSL 相关的错误,可能是由于 SSL 配置问题导致的,尝试检查你的系统的 SSL 配置是否正确。

    如果以上方法都无法解决问题,请提供更多的细节,例如你的网络环境、操作系统信息等,这样可以帮助更准确地定位问题。

    评论
  • bug菌¹ Java领域优质创作者 2024-05-12 04:56
    关注
    获得0.45元问题酬金

    该回答引用ChatGPT,望对题主有所帮助/启发;若有帮助,还望采纳🤝🤝🤝


    根据你提供的错误信息,问题似乎出在网络连接上,具体来说是与 Hugging Face 的服务器 huggingface.co 的连接问题。这可能是由于代理设置、网络连接超时、SSL错误或连接数超过限制等原因造成的。

    以下是一些可能的解决方案:

    1. 检查网络连接:确保你的网络连接是稳定的,并且没有任何防火墙或安全软件阻止你访问 huggingface.co

    2. 代理设置:如果你使用代理服务器(如 Clash),检查代理设置是否正确,或者尝试暂时禁用代理来测试是否是代理导致的问题。

    3. SSL/TLS 问题:SSL错误可能表明存在SSL/TLS配置问题。确保你的系统支持最新的SSL/TLS协议。

    4. 连接超时:增加连接超时设置,或者尝试在网络条件更好的地方运行代码。

    5. Hugging Face 服务器问题:有时候,问题可能是由于 Hugging Face 服务器暂时不可用。稍后重试,或者检查 Hugging Face 的状态页面看是否有已知的服务中断。

    6. 本地缓存:尽管你希望不下载模型到本地,但如果网络问题持续存在,你可能需要考虑将模型下载并保存到本地,然后在本地加载模型以避免网络连接问题。

    7. 使用其他模型:尝试使用其他预训练模型来看是否问题依然存在,这有助于确定问题是否特定于某个模型。

    8. 离线模式:如果你需要在没有互联网连接的情况下工作,可以下载模型并在离线模式下使用。你可以从 Hugging Face 的网站上下载模型,并按照官方文档中的说明进行设置。

    9. 环境变量:有时候,设置环境变量 TF_ENABLE_ONEDNN_OPTS=0 可以解决一些与 oneDNN 相关的数值稳定性问题。

    10. 升级库:确保你安装的 transformersrequestsurllib3 等库都是最新版本。

    11. 手动下载模型:如果自动下载失败,你可以尝试手动从 Hugging Face 的网站上下载模型文件,并指定本地路径给 pipeline

    12. 联系 Hugging Face 支持:如果以上方法都不能解决问题,你可能需要联系 Hugging Face 的技术支持以获得帮助。

    最后,如果你希望避免将来出现类似问题,考虑在网络条件较好且有足够时间的情况下预先下载所需的模型,并在离线模式下使用。这可以减少对网络的依赖,并提高程序的稳定性。

    评论
  • 喵手 2024-05-12 13:45
    关注
    获得0.30元问题酬金

    该回答引用ChatGPT辅助答疑,若有帮助,还请题主采纳。


    您遇到的问题是由于网络连接问题导致的,无法从 Hugging Face 的服务器下载所需的模型文件。这可能是因为代理设置、网络连接超时、连接中断或者防火墙设置等原因。以下是一些可能的解决方案:

    1. 检查网络连接:确保您的网络连接是稳定的,并且没有任何防火墙或代理设置阻止您连接到 huggingface.co

    2. 代理设置:如果您使用的是代理服务器(如 Clash),检查代理设置确保它正确无误,并且代理服务器正在运行。

    3. 代理错误:从错误日志来看,当您使用 Clash 时,遇到了 ProxyErrorSSLError。这可能是因为 SSL 连接在代理中被错误地处理。您可能需要检查 Clash 的配置,或者尝试暂时禁用代理来测试。

    4. 连接超时:如果您没有使用代理,但仍然遇到连接问题,可能是由于连接超时。您可以尝试增加请求的超时时间。

    5. 本地缓存:如果您希望避免每次运行代码时都从 Hugging Face 下载模型,您可以手动下载模型并将其保存在本地,然后在 pipeline 中指定模型的本地路径。

    6. 离线模式:如果您完全无法连接到互联网,您可以尝试使用 transformers 库的离线模式。这需要您手动下载模型文件并解压缩到本地路径,然后在代码中指定这个路径。

    7. 环境变量:您可以尝试设置环境变量 TRANSFORMERS_OFFLINE=1 来启用离线模式,但这需要您已经手动下载了模型。

    8. 手动下载:访问 Hugging Face 模型库的网页版,找到您需要的模型,手动下载配置文件(config.json)、模型权重(pytorch_model.bin)等,并将其放置在本地文件系统中的某个目录里。

    9. 指定本地路径:使用 pipeline 时,通过 model 参数指定本地模型路径,例如:

    from transformers import pipeline
    
    # 指定本地模型文件的路径
    model_path = 'path_to_your_local_model_directory'
    
    classifier = pipeline(task='sentiment-analysis', model=model_path)
    res = classifier('Today is a nice day!')
    print(res)
    
    1. 错误处理:检查错误日志,了解问题的具体原因。例如,如果是 SSL 问题,可能需要更新您的 SSL 证书或调整代理设置。

    2. 联系您的网络管理员:如果您在公司或学校网络环境中,可能存在一些网络访问限制。联系网络管理员获取帮助。

    3. Hugging Face 社区:如果问题依然无法解决,您可以考虑在 Hugging Face 的论坛或 GitHub 仓库中寻求帮助。

    请尝试上述建议,看看是否能够解决您的问题。如果问题仍然存在,请提供更多的信息,以便进一步诊断。

    评论
  • yy64ll826 2024-05-13 01:42
    关注
    获得0.15元问题酬金
    评论
  • 小明爱吃火锅 2024-05-13 09:19
    关注
    获得0.30元问题酬金

    引用文心一言回答:

    from transformers import pipeline
    import urllib3
     
    urllib3.disable_warnings() # 禁用警告信息
     
    # 关闭代理
    import os
    os.environ['NO_PROXY'] = 'huggingface.co'
     
    classifier = pipeline(task='sentiment-analysis')
     
    res = classifier('Today is a nice day!')
     
    print(res)
    
    
    评论
  • 百锦再@新空间代码工作室 全栈领域优质创作者 2024-05-17 03:42
    关注
    获得0.45元问题酬金

    微信GPT辅助作答
    我来把关

    如果您在Python中成功安装了transformers库,但是无法使用pipeline功能,可能是由于以下几个原因:

    1. 版本不兼容:确保您安装的transformers版本与您使用的Python版本兼容。您可以通过运行pip show transformers来检查已安装的transformers版本。

    2. 缺少依赖transformers库依赖于其他一些库,如torchtensorflow。确保这些依赖库也已正确安装,并且版本兼容。

    3. 模型未下载:当您使用pipeline时,transformers库会尝试下载相应的预训练模型。如果您的网络环境阻止了模型的下载,或者下载过程中出现错误,那么pipeline将无法正常工作。您可以尝试手动下载模型并将其放置在正确的目录下。

    4. 权限问题:如果您在尝试使用pipeline时遇到权限错误,可能是因为您没有足够的权限来访问模型文件或执行某些操作。确保您有适当的权限。

    5. 代码错误:检查您的代码是否正确调用了pipeline。例如,确保您为pipeline提供了正确的参数,如模型类型和任务类型。

    以下是一个使用pipeline的示例代码:

    from transformers import pipeline
    
    # 创建一个文本分类的pipeline
    nlp = pipeline("sentiment-analysis")
    
    # 使用pipeline对文本进行分类
    result = nlp("I love using transformers library!")
    
    print(result)
    

    如果您遇到具体的错误信息,请提供错误信息,这样可以更准确地帮助您解决问题。如果您没有收到任何错误信息,但是pipeline没有返回预期的结果,请检查您的代码逻辑和参数设置。

    评论
  • Minuw 2024-05-17 08:23
    关注
    获得0.30元问题酬金

    参考gpt
    从你提供的错误信息来看,问题似乎出现在通过代理服务器(可能是你提到的clash)进行网络请求时。错误提示 urllib3.exceptions.ProxyError 和 SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')) 指出在尝试通过SSL连接到代理服务器时,连接被意外关闭。

    这个问题可能是由以下几个原因造成的:

    代理服务器配置不正确或不稳定。
    代理服务器不支持SSL连接,或者SSL证书有问题。
    Python环境或transformers库中的网络请求代码与你的代理服务器不兼容。
    为了解决这个问题,你可以尝试以下几个步骤:

    1. 检查并更新代理服务器设置
      确保你的代理服务器配置正确,并且稳定可靠。如果你使用的是VPN或代理软件(如clash),请尝试重新启动它,或者检查是否有更新可用。

    2. 禁用代理或切换到不同的代理
      如果你不需要通过代理来访问Hugging Face的模型,你可以尝试禁用代理设置。在Python中,你可以通过设置环境变量来禁用代理,例如:

    bash

    在Windows命令行中

    set HTTP_PROXY=
    set HTTPS_PROXY=

    在Unix/Linux或Mac的终端中

    export HTTP_PROXY=""
    export HTTPS_PROXY=""
    然后重新运行你的Python脚本。

    如果你必须使用代理,请尝试切换到另一个稳定的代理服务器。

    1. 升级transformers和requests库
      确保你安装了最新版本的transformers和requests库。你可以使用pip来升级它们:

    bash
    pip install --upgrade transformers requests
    4. 尝试不使用代理下载模型
    如果你只是想下载模型而不是通过代理进行实时预测,你可以尝试先下载模型文件,然后在没有代理的情况下加载它们。你可以使用Hugging Face的模型仓库来手动下载模型文件。

    1. 调试网络请求
      如果以上方法都不奏效,你可能需要更深入地调试网络请求。你可以尝试使用curl命令或Python的requests库来手动发送HTTPS请求到你的代理服务器,以查看是否能够得到成功的响应。

    2. 联系技术支持
      如果问题仍然无法解决,你可以考虑联系你的代理服务器提供商或Hugging Face的技术支持以获取帮助。

    展开全部

    评论
  • @半夏微凉科技 @半夏微凉官方账号 2024-05-17 09:20
    关注
    获得0.15元问题酬金

    这个问题可能是由于你的网络设置或者代理设置导致的,尤其是报错中提到了代理错误。你可以尝试以下几种方法来解决这个问题:

    1. 检查网络连接和代理设置:确保你的网络连接正常,并且没有设置代理或者代理设置正确。你可以尝试在没有代理的情况下运行你的代码,看看是否可以正常工作。

    2. 使用 VPN:如果你的网络连接存在问题,可以尝试使用 VPN 连接,以确保你能够成功连接到 Hugging Face 的服务器。

    3. 使用离线模式:如果你无法连接到 Hugging Face 的服务器,你可以考虑使用 Transformers 库的离线模式。你可以下载所需的模型和配置文件,然后在本地进行加载。在离线模式下,你可以通过设置 HF_HOME 环境变量来指定本地存储模型文件的路径。

    4. 检查防火墙设置:有时候防火墙设置会阻止程序连接外部服务器。确保你的防火墙设置允许程序访问 Hugging Face 的服务器。

    5. 查看相关文档:查看 Transformers 和 Hugging Face Hub 的文档,看看是否有特定于网络连接的设置或建议。

    通过以上方法,你应该能够解决 Transformers 库无法连接到 Hugging Face 服务器的问题,从而能够正常使用该库。

    评论
  • GIS工具开发 2024-05-18 13:36
    关注
    获得0.30元问题酬金

    如果你在使用代理(比如Clash),确保你的代理设置正确,并且允许访问Hugging Face的域名。如果你的代理配置有问题,你可能需要临时禁用它,或者配置一个不通过代理的例外规则。

    评论
  • 会跑的小鹿 2024-05-18 15:58
    关注
    获得0.15元问题酬金

    根据报错信息中的_ssl.c:1129可以看出是SSL证书问题,可能需要使用受信任的证书。可以尝试手动添加huggingface.co的证书,并确保使用的是TLS1.2及以上协议。

    评论
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 创建了问题 5月11日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部