Saffn commited on
Commit
3871603
·
verified ·
1 Parent(s): 7a8f01d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -1,26 +1,24 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- def respond(
5
- message,
6
- history,
7
- system_message,
8
- max_tokens,
9
- temperature,
10
- top_p,
11
- hf_token
12
- ):
13
  """
14
- Handles streaming responses from Hugging Face Inference API
 
15
  """
16
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
 
 
 
 
 
17
 
18
  messages = [{"role": "system", "content": system_message}]
19
  messages.extend(history)
20
  messages.append({"role": "user", "content": message})
21
 
22
  response = ""
23
-
24
  for message_chunk in client.chat_completion(
25
  messages,
26
  max_tokens=max_tokens,
@@ -32,15 +30,14 @@ def respond(
32
  token = ""
33
  if len(choices) and choices[0].delta.content:
34
  token = choices[0].delta.content
35
-
36
  response += token
37
  yield response
38
 
39
- # Gradio app
40
  with gr.Blocks() as demo:
41
  with gr.Sidebar():
42
- hf_token = gr.OAuthToken() # Correct usage: no label
43
- gr.Markdown("## Configure Chatbot Settings")
44
  system_message = gr.Textbox(
45
  value="You are a friendly Chatbot.",
46
  label="System message"
@@ -55,14 +52,14 @@ with gr.Blocks() as demo:
55
  minimum=0.1, maximum=1.0, value=0.95, step=0.05,
56
  label="Top-p (nucleus sampling)"
57
  )
58
-
59
  chatbot = gr.Chatbot()
60
- state = gr.State([]) # to hold conversation history
61
  msg = gr.Textbox(label="Your message")
62
 
63
  msg.submit(
64
  respond,
65
- inputs=[msg, state, system_message, max_tokens, temperature, top_p, hf_token],
66
  outputs=[chatbot],
67
  show_progress=True
68
  )
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # --- Response function ---
5
+ def respond(message, history, system_message, max_tokens, temperature, top_p, hf_token_info):
 
 
 
 
 
 
 
6
  """
7
+ Handles streaming responses from Hugging Face Inference API.
8
+ `hf_token_info` comes from LoginButton: hf_token_info['access_token']
9
  """
10
+ if not hf_token_info or "access_token" not in hf_token_info:
11
+ yield "Error: You must log in with your Hugging Face token."
12
+ return
13
+
14
+ hf_token = hf_token_info["access_token"]
15
+ client = InferenceClient(token=hf_token, model="openai/gpt-oss-20b")
16
 
17
  messages = [{"role": "system", "content": system_message}]
18
  messages.extend(history)
19
  messages.append({"role": "user", "content": message})
20
 
21
  response = ""
 
22
  for message_chunk in client.chat_completion(
23
  messages,
24
  max_tokens=max_tokens,
 
30
  token = ""
31
  if len(choices) and choices[0].delta.content:
32
  token = choices[0].delta.content
 
33
  response += token
34
  yield response
35
 
36
+ # --- Gradio UI ---
37
  with gr.Blocks() as demo:
38
  with gr.Sidebar():
39
+ gr.Markdown("## Hugging Face Login & Chatbot Settings")
40
+ login_btn = gr.LoginButton("Log in with Hugging Face", provider="huggingface")
41
  system_message = gr.Textbox(
42
  value="You are a friendly Chatbot.",
43
  label="System message"
 
52
  minimum=0.1, maximum=1.0, value=0.95, step=0.05,
53
  label="Top-p (nucleus sampling)"
54
  )
55
+
56
  chatbot = gr.Chatbot()
57
+ state = gr.State([]) # conversation history
58
  msg = gr.Textbox(label="Your message")
59
 
60
  msg.submit(
61
  respond,
62
+ inputs=[msg, state, system_message, max_tokens, temperature, top_p, login_btn],
63
  outputs=[chatbot],
64
  show_progress=True
65
  )