使用Python连接Hugging Face API接口

之前不明白这个过程,现在记录一下。

我的原本目的:

在本地写Python代码,然后调用huggingface中某个项目中模型的接口,从而完成模型的调用。以期达到我在本地键入输入,得到模型的输出,从而测试模型效果。

之前的代码以及出现的问题:

<1>之前的代码:

import requests

API_URL = "https://api-inference.huggingface.co/models/flax-sentence-embeddings/st-codesearch-distilroberta-base"
headers = {"Authorization": "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}



def query(payload):
     response = requests.post(API_URL, headers=headers, json=payload, proxies=proxies)
     return response.json()
     
output = query({
     "inputs": {
          "source_sentence": "",
          "sentences": [
               "That is a happy dog",
               "That is a very happy person",
               "Today is a sunny day"
          ]
     },
})


print(output)

<2>运行报错:

Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 203, in _new_conn
    sock = connection.create_connection(
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    raise err
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

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

Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 776, in urlopen
    self._prepare_proxy(conn)
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 1041, in _prepare_proxy
    conn.connect()
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 611, in connect
    self.sock = sock = self._new_conn()
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 218, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00000290E7C78880>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

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

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000290E7C78880>: Failed 
to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))

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

Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\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='api-inference.huggingface.co', port=443): Max retries exceeded with url: /models/flax-sentence-embeddings/st-codesearch-distilroberta-base (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000290E7C78880>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:/code/code-search/code-search.py", line 16, in <module>
    output = query({
    response = requests.post(API_URL, headers=headers, json=payload, proxies=proxies)
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api-inference.huggingface.co', port=443): Max retries exceeded with url: /models/flax-sentence-embeddings/st-codesearch-distilroberta-base (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object 
at 0x00000290E7C78880>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

正确思想和做法:

<1>正确思想

因为huggingface现在不让国内访问,所以想要连接上的步骤为:

发送请求:

本地的Python程序→梯子→代理服务器→huggingface的服务器

获得响应:

huggingface的服务器→代理服务器→梯子→本地的Python程序

<2>做法

我之前是因为没有对梯子进行配置,所以其实上我的行为是“本地的Python程序→huggingface服务器”,这是无法访问的,所以会报错。

所以,这里需要做的是配置梯子,打开梯子允许LAN连接,还应知道梯子的端口是多少。从而对代码做点改变就可以了!

然后改变代码如下所示:

import requests

API_URL = "https://api-inference.huggingface.co/models/flax-sentence-embeddings/st-codesearch-distilroberta-base"
headers = {"Authorization": "Bearer xxxx"}

proxies = {
   'http': 'http://127.0.0.1:7890', # ip:梯子的端口号
   'https': 'http://127.0.0.1:7890', # ip:梯子的端口号
}


def query(payload):
     response = requests.post(API_URL, headers=headers, json=payload, proxies=proxies)
     return response.json()
     
output = query({
     "inputs": {
          "source_sentence": "",
          "sentences": [
               "That is a happy dog",
               "That is a very happy person",
               "Today is a sunny day"
          ]
     },
})


print(output)

物联沃分享整理
物联沃-IOTWORD物联网 » 使用Python连接Hugging Face API接口

发表评论