Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom headers/cookies in InferenceClient #1507

Merged
merged 1 commit into from
Jun 14, 2023

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Jun 13, 2023

Related to huggingface/api-inference-community#287 (and especially huggingface/api-inference-community#287 (comment)) cc @StephenHodgson @Narsil

With this PR, user can set custom headers and cookies to the InferenceClient session. This can prove useful when using custom endpoints. If set, custom headers overwrite the default user-agent and authorization headers.

(+fix documentation => save audio as .flac instead of .wav)

@Wauplin Wauplin requested review from Narsil and LysandreJik June 13, 2023 17:46
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jun 13, 2023

The documentation is not available anymore as the PR was closed or merged.

@codecov
Copy link

codecov bot commented Jun 13, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.01 🎉

Comparison is base (b4e01b0) 82.18% compared to head (b3db890) 82.19%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1507      +/-   ##
==========================================
+ Coverage   82.18%   82.19%   +0.01%     
==========================================
  Files          57       57              
  Lines        6089     6093       +4     
==========================================
+ Hits         5004     5008       +4     
  Misses       1085     1085              
Impacted Files Coverage Δ
src/huggingface_hub/_inference.py 77.04% <100.00%> (+0.51%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link

@StephenHodgson StephenHodgson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, would like to know @Narsil's thoughts on it given that it's working with the inference API

Copy link
Contributor

@Narsil Narsil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup LGTM.

I am guessing this comment relies on the undelying lib, but cookies is a header, I'm kind of curious of what happens if you set both to different values.

@Wauplin
Copy link
Contributor Author

Wauplin commented Jun 14, 2023

@Narsil to satisfy your curiosity:

import curl
import requests

curl.parse(requests.get("http://www.google.com", headers={"Cookie": "foo=bar"}))
curl.parse(requests.get("http://www.google.com", cookies={"foo": "something", "abc": "def"}))
curl.parse(requests.get("http://www.google.com", headers={"Cookie": "foo=bar"}, cookies={"foo": "something", "abc": "def"}))

which outputs

curl -X GET -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'Cookie: foo=bar' -H 'User-Agent: python-requests/2.28.1' http://www.google.com:80/
curl -X GET -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'Cookie: abc=def; foo=something' -H 'User-Agent: python-requests/2.28.1' http://www.google.com:80/
curl -X GET -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'Cookie: foo=bar' -H 'User-Agent: python-requests/2.28.1' http://www.google.com:80/

Conclusion:

  1. Setting in headers has priority.
  2. Setting in both headers and cookies just ignores the cookies value.

And thanks all 3 for the reviews! :)

@Wauplin Wauplin merged commit e8de966 into main Jun 14, 2023
@Wauplin Wauplin deleted the inference-client-custom-headers branch June 14, 2023 08:24
@Narsil
Copy link
Contributor

Narsil commented Jun 14, 2023

I love having my curiosity satisfied. Must be has caused a lot of headaches :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants