Spaces:
Runtime error
Runtime error
login
Browse files
README.md
CHANGED
@@ -7,6 +7,16 @@ sdk: gradio
|
|
7 |
app_file: app.py
|
8 |
pinned: false
|
9 |
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
7 |
app_file: app.py
|
8 |
pinned: false
|
9 |
license: apache-2.0
|
10 |
+
|
11 |
+
hf_oauth: true
|
12 |
+
# optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
|
13 |
+
hf_oauth_expiration_minutes: 480
|
14 |
+
# optional, see "Scopes" below. "openid profile" is always included.
|
15 |
+
hf_oauth_scopes:
|
16 |
+
- read-repos
|
17 |
+
- write-repos
|
18 |
+
- manage-repos
|
19 |
+
- inference-api
|
20 |
---
|
21 |
|
22 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
CHANGED
@@ -1,25 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
# Hugging Faceにログインする関数
|
5 |
-
def login_to_huggingface(token):
|
6 |
-
api = HfApi()
|
7 |
-
try:
|
8 |
-
# トークンを使用してログイン
|
9 |
-
api.set_access_token(token)
|
10 |
-
user = api.whoami()
|
11 |
-
return f"ログイン成功: {user['name']}"
|
12 |
-
except Exception as e:
|
13 |
-
return f"ログイン失敗: {str(e)}"
|
14 |
|
15 |
-
# Gradioインターフェースの作成
|
16 |
with gr.Blocks() as demo:
|
17 |
-
gr.Markdown(
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import list_models
|
3 |
+
|
4 |
+
|
5 |
+
def hello(profile: gr.OAuthProfile | None) -> str:
|
6 |
+
# ^ expect a gr.OAuthProfile object as input to get the user's profile
|
7 |
+
# if the user is not logged in, profile will be None
|
8 |
+
if profile is None:
|
9 |
+
return "I don't know you."
|
10 |
+
return f"Hello {profile.name}"
|
11 |
+
|
12 |
+
|
13 |
+
def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> str:
|
14 |
+
# ^ expect a gr.OAuthToken object as input to get the user's token
|
15 |
+
# if the user is not logged in, oauth_token will be None
|
16 |
+
if oauth_token is None:
|
17 |
+
return "Please log in to list private models."
|
18 |
+
models = [
|
19 |
+
f"{model.id} ({'private' if model.private else 'public'})"
|
20 |
+
for model in list_models(author=profile.username, token=oauth_token.token)
|
21 |
+
]
|
22 |
+
return "Models:\n\n" + "\n - ".join(models) + "."
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
|
|
25 |
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown(
|
27 |
+
"# Gradio OAuth Space"
|
28 |
+
"\n\nThis Space is a demo for the **Sign in with Hugging Face** feature. "
|
29 |
+
"Duplicate this Space to get started."
|
30 |
+
"\n\nFor more details, check out:"
|
31 |
+
"\n- https://www.gradio.app/guides/sharing-your-app#o-auth-login-via-hugging-face"
|
32 |
+
"\n- https://huggingface.co/docs/hub/spaces-oauth"
|
33 |
+
)
|
34 |
+
gr.LoginButton()
|
35 |
+
# ^ add a login button to the Space
|
36 |
+
m1 = gr.Markdown()
|
37 |
+
m2 = gr.Markdown()
|
38 |
+
demo.load(hello, inputs=None, outputs=m1)
|
39 |
+
demo.load(list_private_models, inputs=None, outputs=m2)
|
40 |
|
41 |
+
demo.launch()
|
|