Spaces:
Sleeping
Sleeping
Tuchuanhuhuhu
commited on
Commit
·
1f16025
1
Parent(s):
5d70a8a
removed hack logout button, use gradio4 official logout button instead
Browse files- ChuanhuChatbot.py +3 -15
- modules/gradio_patch.py +0 -115
ChuanhuChatbot.py
CHANGED
@@ -16,10 +16,6 @@ from modules.config import *
|
|
16 |
from modules import config
|
17 |
import gradio as gr
|
18 |
import colorama
|
19 |
-
from modules.gradio_patch import reg_patch
|
20 |
-
|
21 |
-
if not hfspaceflag:
|
22 |
-
reg_patch()
|
23 |
|
24 |
logging.getLogger("httpx").setLevel(logging.WARNING)
|
25 |
|
@@ -36,8 +32,6 @@ def create_new_model():
|
|
36 |
|
37 |
with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
38 |
user_name = gr.Textbox("", visible=False)
|
39 |
-
# 激活/logout路由
|
40 |
-
logout_hidden_btn = gr.LogoutButton(visible=False)
|
41 |
promptTemplates = gr.State(load_template(get_template_names()[0], mode=2))
|
42 |
user_question = gr.State("")
|
43 |
assert type(my_api_key) == str
|
@@ -396,8 +390,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
396 |
single_turn_checkbox = gr.Checkbox(label=i18n(
|
397 |
"单轮对话"), value=False, elem_classes="switch-checkbox", elem_id="gr-single-session-cb", visible=False)
|
398 |
# checkUpdateBtn = gr.Button(i18n("🔄 检查更新..."), visible=check_update)
|
399 |
-
logout_btn = gr.Button(
|
400 |
-
i18n("退出用户"), variant="primary", visible=authflag)
|
401 |
|
402 |
with gr.Tab(i18n("网络")):
|
403 |
gr.Markdown(
|
@@ -534,6 +527,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
534 |
],
|
535 |
outputs=[chatbot, status_display],
|
536 |
show_progress=True,
|
|
|
537 |
)
|
538 |
|
539 |
start_outputing_args = dict(
|
@@ -553,7 +547,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
553 |
|
554 |
transfer_input_args = dict(
|
555 |
fn=transfer_input, inputs=[user_input], outputs=[
|
556 |
-
user_question, user_input, submitBtn, cancelBtn], show_progress=True
|
557 |
)
|
558 |
|
559 |
get_usage_args = dict(
|
@@ -808,12 +802,6 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
808 |
outputs=[saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt],
|
809 |
js='(a,b)=>{return bgSelectHistory(a,b);}'
|
810 |
)
|
811 |
-
logout_btn.click(
|
812 |
-
fn=None,
|
813 |
-
inputs=[],
|
814 |
-
outputs=[],
|
815 |
-
js='self.location="/logout"'
|
816 |
-
)
|
817 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
818 |
demo.title = i18n("川虎Chat 🚀")
|
819 |
|
|
|
16 |
from modules import config
|
17 |
import gradio as gr
|
18 |
import colorama
|
|
|
|
|
|
|
|
|
19 |
|
20 |
logging.getLogger("httpx").setLevel(logging.WARNING)
|
21 |
|
|
|
32 |
|
33 |
with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
34 |
user_name = gr.Textbox("", visible=False)
|
|
|
|
|
35 |
promptTemplates = gr.State(load_template(get_template_names()[0], mode=2))
|
36 |
user_question = gr.State("")
|
37 |
assert type(my_api_key) == str
|
|
|
390 |
single_turn_checkbox = gr.Checkbox(label=i18n(
|
391 |
"单轮对话"), value=False, elem_classes="switch-checkbox", elem_id="gr-single-session-cb", visible=False)
|
392 |
# checkUpdateBtn = gr.Button(i18n("🔄 检查更新..."), visible=check_update)
|
393 |
+
logout_btn = gr.Button("Logout", link="/logout")
|
|
|
394 |
|
395 |
with gr.Tab(i18n("网络")):
|
396 |
gr.Markdown(
|
|
|
527 |
],
|
528 |
outputs=[chatbot, status_display],
|
529 |
show_progress=True,
|
530 |
+
concurrency_limit=CONCURRENT_COUNT
|
531 |
)
|
532 |
|
533 |
start_outputing_args = dict(
|
|
|
547 |
|
548 |
transfer_input_args = dict(
|
549 |
fn=transfer_input, inputs=[user_input], outputs=[
|
550 |
+
user_question, user_input, submitBtn, cancelBtn], show_progress=True
|
551 |
)
|
552 |
|
553 |
get_usage_args = dict(
|
|
|
802 |
outputs=[saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt],
|
803 |
js='(a,b)=>{return bgSelectHistory(a,b);}'
|
804 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
805 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
806 |
demo.title = i18n("川虎Chat 🚀")
|
807 |
|
modules/gradio_patch.py
DELETED
@@ -1,115 +0,0 @@
|
|
1 |
-
import logging
|
2 |
-
import os
|
3 |
-
|
4 |
-
import fastapi
|
5 |
-
import gradio
|
6 |
-
from fastapi.responses import RedirectResponse
|
7 |
-
# from gradio.oauth import MOCKED_OAUTH_TOKEN
|
8 |
-
|
9 |
-
from modules.presets import i18n
|
10 |
-
|
11 |
-
OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
|
12 |
-
OAUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET")
|
13 |
-
OAUTH_SCOPES = os.environ.get("OAUTH_SCOPES")
|
14 |
-
OPENID_PROVIDER_URL = os.environ.get("OPENID_PROVIDER_URL")
|
15 |
-
def _add_oauth_routes(app: fastapi.FastAPI) -> None:
|
16 |
-
"""Add OAuth routes to the FastAPI app (login, callback handler and logout)."""
|
17 |
-
try:
|
18 |
-
from authlib.integrations.starlette_client import OAuth
|
19 |
-
except ImportError as e:
|
20 |
-
raise ImportError(
|
21 |
-
"Cannot initialize OAuth to due a missing library. Please run `pip install gradio[oauth]` or add "
|
22 |
-
"`gradio[oauth]` to your requirements.txt file in order to install the required dependencies."
|
23 |
-
) from e
|
24 |
-
|
25 |
-
# Check environment variables
|
26 |
-
msg = (
|
27 |
-
"OAuth is required but {} environment variable is not set. Make sure you've enabled OAuth in your Space by"
|
28 |
-
" setting `hf_oauth: true` in the Space metadata."
|
29 |
-
)
|
30 |
-
if OAUTH_CLIENT_ID is None:
|
31 |
-
raise ValueError(msg.format("OAUTH_CLIENT_ID"))
|
32 |
-
if OAUTH_CLIENT_SECRET is None:
|
33 |
-
raise ValueError(msg.format("OAUTH_CLIENT_SECRET"))
|
34 |
-
if OAUTH_SCOPES is None:
|
35 |
-
raise ValueError(msg.format("OAUTH_SCOPES"))
|
36 |
-
if OPENID_PROVIDER_URL is None:
|
37 |
-
raise ValueError(msg.format("OPENID_PROVIDER_URL"))
|
38 |
-
|
39 |
-
# Register OAuth server
|
40 |
-
oauth = OAuth()
|
41 |
-
oauth.register(
|
42 |
-
name="huggingface",
|
43 |
-
client_id=OAUTH_CLIENT_ID,
|
44 |
-
client_secret=OAUTH_CLIENT_SECRET,
|
45 |
-
client_kwargs={"scope": OAUTH_SCOPES},
|
46 |
-
server_metadata_url=OPENID_PROVIDER_URL + "/.well-known/openid-configuration",
|
47 |
-
)
|
48 |
-
|
49 |
-
# Define OAuth routes
|
50 |
-
@app.get("/login/huggingface")
|
51 |
-
async def oauth_login(request: fastapi.Request):
|
52 |
-
"""Endpoint that redirects to HF OAuth page."""
|
53 |
-
redirect_uri = str(request.url_for("oauth_redirect_callback"))
|
54 |
-
if ".hf.space" in redirect_uri:
|
55 |
-
# In Space, FastAPI redirect as http but we want https
|
56 |
-
redirect_uri = redirect_uri.replace("http://", "https://")
|
57 |
-
return await oauth.huggingface.authorize_redirect(request, redirect_uri)
|
58 |
-
|
59 |
-
@app.get("/login/callback")
|
60 |
-
async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
|
61 |
-
"""Endpoint that handles the OAuth callback."""
|
62 |
-
token = await oauth.huggingface.authorize_access_token(request)
|
63 |
-
request.session["oauth_profile"] = token["userinfo"]
|
64 |
-
request.session["oauth_token"] = token
|
65 |
-
return RedirectResponse("/")
|
66 |
-
|
67 |
-
@app.get("/logout")
|
68 |
-
async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
|
69 |
-
"""Endpoint that logs out the user (e.g. delete cookie session)."""
|
70 |
-
request.session.pop("oauth_profile", None)
|
71 |
-
request.session.pop("oauth_token", None)
|
72 |
-
# 清除cookie并跳转到首页
|
73 |
-
response = RedirectResponse(url="/", status_code=302)
|
74 |
-
response.delete_cookie(key=f"access-token")
|
75 |
-
response.delete_cookie(key=f"access-token-unsecure")
|
76 |
-
return response
|
77 |
-
|
78 |
-
|
79 |
-
def _add_mocked_oauth_routes(app: fastapi.FastAPI) -> None:
|
80 |
-
"""Add fake oauth routes if Gradio is run locally and OAuth is enabled.
|
81 |
-
|
82 |
-
Clicking on a gr.LoginButton will have the same behavior as in a Space (i.e. gets redirected in a new tab) but
|
83 |
-
instead of authenticating with HF, a mocked user profile is added to the session.
|
84 |
-
"""
|
85 |
-
|
86 |
-
# Define OAuth routes
|
87 |
-
@app.get("/login/huggingface")
|
88 |
-
async def oauth_login(request: fastapi.Request):
|
89 |
-
"""Fake endpoint that redirects to HF OAuth page."""
|
90 |
-
return RedirectResponse("/login/callback")
|
91 |
-
|
92 |
-
@app.get("/login/callback")
|
93 |
-
async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
|
94 |
-
"""Endpoint that handles the OAuth callback."""
|
95 |
-
request.session["oauth_profile"] = MOCKED_OAUTH_TOKEN["userinfo"]
|
96 |
-
request.session["oauth_token"] = MOCKED_OAUTH_TOKEN
|
97 |
-
return RedirectResponse("/")
|
98 |
-
|
99 |
-
@app.get("/logout")
|
100 |
-
async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
|
101 |
-
"""Endpoint that logs out the user (e.g. delete cookie session)."""
|
102 |
-
request.session.pop("oauth_profile", None)
|
103 |
-
request.session.pop("oauth_token", None)
|
104 |
-
# 清除cookie并跳转到首页
|
105 |
-
response = RedirectResponse(url="/", status_code=302)
|
106 |
-
response.delete_cookie(key=f"access-token")
|
107 |
-
response.delete_cookie(key=f"access-token-unsecure")
|
108 |
-
return response
|
109 |
-
|
110 |
-
|
111 |
-
def reg_patch():
|
112 |
-
# gradio.oauth._add_mocked_oauth_routes = _add_mocked_oauth_routes
|
113 |
-
# gradio.oauth._add_oauth_routes = _add_oauth_routes
|
114 |
-
# logging.info(i18n("覆盖gradio.oauth /logout路由"))
|
115 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|