E5K7 commited on
Commit
ff479eb
·
verified ·
1 Parent(s): b619ff9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -23
app.py CHANGED
@@ -1,66 +1,113 @@
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="E5K7/eshalskoibito")
 
 
 
 
 
18
 
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
 
 
 
 
 
 
22
 
23
  messages.append({"role": "user", "content": message})
24
 
25
  response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
  max_tokens=max_tokens,
30
  stream=True,
31
  temperature=temperature,
32
  top_p=top_p,
33
  ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  chatbot = gr.ChatInterface(
47
  respond,
48
  type="messages",
 
 
 
49
  additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  gr.Slider(
54
  minimum=0.1,
55
  maximum=1.0,
56
  value=0.95,
57
  step=0.05,
58
  label="Top-p (nucleus sampling)",
 
59
  ),
60
  ],
 
 
 
61
  )
62
 
63
  with gr.Blocks() as demo:
 
64
  with gr.Sidebar():
65
  gr.LoginButton()
66
  chatbot.render()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Import the 'gr.OAuthToken' type for Gradio to handle the OAuth token automatically.
5
+
6
+ # It is a best practice to define the model ID separately for clarity.
7
+ MODEL_ID = "E5K7/eshalskoibito"
8
+
9
 
10
  def respond(
11
  message,
12
  history: list[dict[str, str]],
13
+ system_message: str,
14
+ max_tokens: int,
15
+ temperature: float,
16
+ top_p: float,
17
  hf_token: gr.OAuthToken,
18
  ):
19
  """
20
+ Handles the chatbot's response by sending a request to the Hugging Face Inference API.
21
+
22
+ Args:
23
+ message (str): The user's message.
24
+ history (list): The list of previous conversation turns.
25
+ system_message (str): The system message for the model.
26
+ max_tokens (int): The maximum number of new tokens to generate.
27
+ temperature (float): The sampling temperature.
28
+ top_p (float): The top-p value for nucleus sampling.
29
+ hf_token (gr.OAuthToken): The Hugging Face OAuth token for authentication.
30
+
31
+ Yields:
32
+ str: The generated response, streamed token by token.
33
  """
34
+ # Ensure the Hugging Face token is available before proceeding.
35
+ if hf_token is None:
36
+ raise gr.Error("You must log in to use the chatbot!")
37
+
38
+ # Initialize the InferenceClient with the provided token.
39
+ client = InferenceClient(token=hf_token.token, model=MODEL_ID)
40
 
41
  messages = [{"role": "system", "content": system_message}]
42
 
43
+ # Format the chat history for the client, which expects a list of dictionaries.
44
+ messages.extend(
45
+ [
46
+ {"role": turn["role"], "content": turn["content"]}
47
+ for turn in history
48
+ ]
49
+ )
50
 
51
  messages.append({"role": "user", "content": message})
52
 
53
  response = ""
54
+ for token in client.chat_completion(
55
+ messages=messages,
 
56
  max_tokens=max_tokens,
57
  stream=True,
58
  temperature=temperature,
59
  top_p=top_p,
60
  ):
61
+ if token.choices and token.choices[0].delta.content:
62
+ response += token.choices[0].delta.content
63
+ yield response
 
 
 
 
64
 
65
 
66
+ # Create the ChatInterface with updated parameters for better user experience.
 
 
67
  chatbot = gr.ChatInterface(
68
  respond,
69
  type="messages",
70
+ # Add a title and description for better context.
71
+ title="Eshalskoibito Chatbot",
72
+ description=f"Interact with the model: **{MODEL_ID}**",
73
  additional_inputs=[
74
+ gr.Textbox(
75
+ value="You are a friendly Chatbot.",
76
+ label="System message",
77
+ info="Define the persona and behavior of the chatbot.",
78
+ ),
79
+ gr.Slider(
80
+ minimum=1,
81
+ maximum=2048,
82
+ value=512,
83
+ step=1,
84
+ label="Max new tokens",
85
+ info="The maximum number of tokens to generate in the response.",
86
+ ),
87
+ gr.Slider(
88
+ minimum=0.1,
89
+ maximum=4.0,
90
+ value=0.7,
91
+ step=0.1,
92
+ label="Temperature",
93
+ info="Controls the randomness of the output. Higher values lead to more creative responses.",
94
+ ),
95
  gr.Slider(
96
  minimum=0.1,
97
  maximum=1.0,
98
  value=0.95,
99
  step=0.05,
100
  label="Top-p (nucleus sampling)",
101
+ info="Filters out low-probability tokens. Lower values make the response more focused.",
102
  ),
103
  ],
104
+ # Add a parameter to save chat history locally in the user's browser.
105
+ # This prevents conversation mixing between multiple users.
106
+ save_history=True,
107
  )
108
 
109
  with gr.Blocks() as demo:
110
+ # Use gr.LoginButton() and pass the oauth token to the chatbot function.
111
  with gr.Sidebar():
112
  gr.LoginButton()
113
  chatbot.render()