Threatthriver commited on
Commit
ba6c3d0
1 Parent(s): cdcd8df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -34
app.py CHANGED
@@ -1,49 +1,67 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- 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
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
 
10
  def respond(
11
- message,
12
  history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
25
 
 
26
  messages.append({"role": "user", "content": message})
27
 
 
28
  response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
 
 
 
45
  demo = gr.ChatInterface(
46
- respond,
47
  additional_inputs=[
48
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -56,8 +74,10 @@ demo = gr.ChatInterface(
56
  label="Top-p (nucleus sampling)",
57
  ),
58
  ],
 
 
59
  )
60
 
61
-
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Initialize the InferenceClient with the model ID from Hugging Face
5
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
 
 
 
6
 
7
  def respond(
8
+ message: str,
9
  history: list[tuple[str, str]],
10
+ system_message: str,
11
+ max_tokens: int,
12
+ temperature: float,
13
+ top_p: float,
14
  ):
15
+ """
16
+ Generates a response from the AI model based on the user's message and chat history.
17
+
18
+ Args:
19
+ message (str): The user's input message.
20
+ history (list): A list of tuples representing the conversation history (user, assistant).
21
+ system_message (str): A system-level message guiding the AI's behavior.
22
+ max_tokens (int): The maximum number of tokens for the output.
23
+ temperature (float): Sampling temperature for controlling the randomness.
24
+ top_p (float): Top-p (nucleus sampling) for controlling diversity.
25
 
26
+ Yields:
27
+ str: The AI's response as it is generated.
28
+ """
29
+
30
+ # Prepare the conversation history for the API call
31
+ messages = [{"role": "system", "content": system_message}]
32
+
33
+ for user_input, assistant_response in history:
34
+ if user_input:
35
+ messages.append({"role": "user", "content": user_input})
36
+ if assistant_response:
37
+ messages.append({"role": "assistant", "content": assistant_response})
38
 
39
+ # Add the latest user message to the conversation
40
  messages.append({"role": "user", "content": message})
41
 
42
+ # Initialize an empty response
43
  response = ""
44
 
45
+ try:
46
+ # Generate a response from the model with streaming
47
+ for message in client.chat_completion(
48
+ messages=messages,
49
+ max_tokens=max_tokens,
50
+ stream=True,
51
+ temperature=temperature,
52
+ top_p=top_p,
53
+ ):
54
+ token = message.choices[0].delta.content
55
+ response += token
56
+ yield response
57
+
58
+ except Exception as e:
59
+ yield f"An error occurred: {str(e)}"
60
+
61
+
62
+ # Define the ChatInterface with additional input components for user customization
63
  demo = gr.ChatInterface(
64
+ fn=respond,
65
  additional_inputs=[
66
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
67
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
74
  label="Top-p (nucleus sampling)",
75
  ),
76
  ],
77
+ title="Chatbot Interface",
78
+ description="A customizable chatbot interface using Hugging Face's Inference API.",
79
  )
80
 
81
+ # Launch the Gradio interface
82
  if __name__ == "__main__":
83
+ demo.launch()