MakiAi commited on
Commit
e87ce6e
·
verified ·
1 Parent(s): fc95b15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -12
app.py CHANGED
@@ -1,34 +1,69 @@
1
  # app.py
 
2
  import gradio as gr
3
 
 
 
 
 
 
 
4
  def my_tool(q: str, dev_token: str, request: gr.Request) -> str:
5
- # 1) MCP/HTTP ヘッダから取得
6
  auth = request.headers.get("authorization")
7
 
8
- # 2) ヘッダが無ければ UI入力欄から(開発用)
9
  if not auth and dev_token:
10
  auth = f"Bearer {dev_token.strip()}"
11
 
12
- # 3) 最終的にキーがなければ guest として扱う
13
  if not auth:
14
- user = "guest"
 
 
 
 
 
 
15
  else:
16
- # "Bearer <token>" をパース
17
- token = auth.split(" ", 1)[1] if " " in auth else auth
18
- user = f"user:{token[:6]}..." # トークン先頭数文字だけ表示
 
 
 
19
 
20
- return f"Hello {user}, you said: {q}"
 
 
 
 
 
21
 
22
  with gr.Blocks() as demo:
23
- gr.Markdown("### Gradio MCP 認証デモ")
24
- q = gr.Textbox(label="Query")
25
  dev_token = gr.Textbox(
26
  label="API Token (開発用オプション)",
27
  type="password",
28
  placeholder="sk_xxx / hf_xxx"
29
  )
30
- out = gr.Textbox(label="Result")
31
  run = gr.Button("Run")
32
  run.click(my_tool, inputs=[q, dev_token], outputs=[out])
33
 
34
- demo.launch(mcp_server=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # app.py
2
+ import os
3
  import gradio as gr
4
 
5
+ # --- コア処理(ここに本来のツール処理を書く) ---
6
+ def handle_request(q: str, user_label: str) -> str:
7
+ # 実際にはここから外部API呼び出し・DB操作などを行う
8
+ return f"[{user_label}] You said: {q}"
9
+
10
+ # --- 認証+実行:キーが無いときは guest ---
11
  def my_tool(q: str, dev_token: str, request: gr.Request) -> str:
12
+ # 1) MCP/HTTPのAuthorizationヘッダ
13
  auth = request.headers.get("authorization")
14
 
15
+ # 2) UIのトークン入力(開発用)
16
  if not auth and dev_token:
17
  auth = f"Bearer {dev_token.strip()}"
18
 
19
+ # 3) 環境変数(任意の開発用)
20
  if not auth:
21
+ env_token = os.getenv("DEMO_TOKEN", "").strip()
22
+ if env_token:
23
+ auth = f"Bearer {env_token}"
24
+
25
+ # ラベル決定(guest or user)
26
+ if not auth or not auth.lower().startswith("bearer "):
27
+ user_label = "guest"
28
  else:
29
+ token = auth.split(" ", 1)[1].strip()
30
+ short = token[:6] + "..." if len(token) > 6 else token
31
+ user_label = f"user:{short}"
32
+
33
+ # ここから本来の処理へ
34
+ return handle_request(q, user_label)
35
 
36
+ # ---- Gradio UI ----
37
+ examples = [
38
+ ["Hello world", ""], # 1) guest
39
+ ["Check my status", "sk_test_123456"], # 2) ダミーAPIキー
40
+ ["Fetch HF data", "hf_dummy_token_abcdef"], # 3) HFトークン例
41
+ ]
42
 
43
  with gr.Blocks() as demo:
44
+ gr.Markdown("### Gradio MCP 認証デモ(キー無し=guest)")
45
+ q = gr.Textbox(label="Query", placeholder="Type something")
46
  dev_token = gr.Textbox(
47
  label="API Token (開発用オプション)",
48
  type="password",
49
  placeholder="sk_xxx / hf_xxx"
50
  )
51
+ out = gr.Textbox(label="Result", lines=3)
52
  run = gr.Button("Run")
53
  run.click(my_tool, inputs=[q, dev_token], outputs=[out])
54
 
55
+ gr.Examples(
56
+ examples=examples,
57
+ inputs=[q, dev_token],
58
+ outputs=[out],
59
+ fn=my_tool,
60
+ cache_examples=False,
61
+ )
62
+
63
+ # MCPサーバを同時起動(SSEは非推奨。/gradio_api/mcp/ を使用)
64
+ demo.launch(
65
+ mcp_server=True,
66
+ ssr_mode=True, # SSRログの⚡表示用(不要ならFalse)
67
+ server_name="0.0.0.0",
68
+ server_port=int(os.getenv("PORT", "7860")),
69
+ )