Saffn commited on
Commit
1ebf6f2
·
verified ·
1 Parent(s): 82320bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -26
app.py CHANGED
@@ -1,18 +1,17 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
-
5
  def respond(
6
  message,
7
- history: list[dict[str, str]],
8
  system_message,
9
  max_tokens,
10
  temperature,
11
  top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
  """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
  """
17
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
@@ -22,14 +21,14 @@ def respond(
22
 
23
  response = ""
24
 
25
- for message in client.chat_completion(
26
  messages,
27
  max_tokens=max_tokens,
28
  stream=True,
29
  temperature=temperature,
30
  top_p=top_p,
31
  ):
32
- choices = message.choices
33
  token = ""
34
  if len(choices) and choices[0].delta.content:
35
  token = choices[0].delta.content
@@ -37,28 +36,37 @@ def respond(
37
  response += token
38
  yield response
39
 
40
-
41
- # ChatInterface does not require the 'type' argument
42
- chatbot = gr.ChatInterface(
43
- fn=respond,
44
- inputs=[
45
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
46
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
47
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
- gr.Slider(
49
- minimum=0.1,
50
- maximum=1.0,
51
- value=0.95,
52
- step=0.05,
53
- label="Top-p (nucleus sampling)",
54
- ),
55
- ],
56
- )
57
-
58
  with gr.Blocks() as demo:
59
  with gr.Sidebar():
60
- gr.LoginButton()
61
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
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
 
 
21
 
22
  response = ""
23
 
24
+ for message_chunk in client.chat_completion(
25
  messages,
26
  max_tokens=max_tokens,
27
  stream=True,
28
  temperature=temperature,
29
  top_p=top_p,
30
  ):
31
+ choices = message_chunk.choices
32
  token = ""
33
  if len(choices) and choices[0].delta.content:
34
  token = choices[0].delta.content
 
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(label="Hugging Face Token")
43
+ system_message = gr.Textbox(
44
+ value="You are a friendly Chatbot.",
45
+ label="System message"
46
+ )
47
+ max_tokens = gr.Slider(
48
+ minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"
49
+ )
50
+ temperature = gr.Slider(
51
+ minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"
52
+ )
53
+ top_p = gr.Slider(
54
+ minimum=0.1, maximum=1.0, value=0.95, step=0.05,
55
+ label="Top-p (nucleus sampling)"
56
+ )
57
+
58
+ chatbot = gr.Chatbot()
59
+
60
+ state = gr.State([]) # to hold conversation history
61
+
62
+ msg = gr.Textbox(label="Your message")
63
+
64
+ msg.submit(
65
+ respond,
66
+ inputs=[msg, state, system_message, max_tokens, temperature, top_p, hf_token],
67
+ outputs=[chatbot],
68
+ show_progress=True
69
+ )
70
 
71
  if __name__ == "__main__":
72
  demo.launch()