Threatthriver commited on
Commit
e7f3548
1 Parent(s): 09fa947

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -19
app.py CHANGED
@@ -4,6 +4,16 @@ from huggingface_hub import InferenceClient
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]],
@@ -14,7 +24,7 @@ def respond(
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).
@@ -26,46 +36,48 @@ def respond(
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"),
68
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
69
  gr.Slider(
70
  minimum=0.1,
71
  maximum=1.0,
@@ -74,10 +86,22 @@ demo = gr.ChatInterface(
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()
 
4
  # Initialize the InferenceClient with the model ID from Hugging Face
5
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
6
 
7
+ # Latest updates (you can replace this with actual update information)
8
+ latest_updates = """
9
+ **Zephyr 7B Beta Chatbot - Latest Updates:**
10
+
11
+ * **Improved Error Handling:** The chatbot now provides clearer error messages if something goes wrong.
12
+ * **Enhanced System Message Input:** You can now provide multi-line system messages to guide the AI's behavior.
13
+ * **Optimized Temperature Range:** The temperature slider's range has been adjusted for better control over randomness.
14
+ * **Robust Chunk Handling:** The chatbot now handles streamed responses more reliably, even if some chunks are missing content.
15
+ """
16
+
17
  def respond(
18
  message: str,
19
  history: list[tuple[str, str]],
 
24
  ):
25
  """
26
  Generates a response from the AI model based on the user's message and chat history.
27
+
28
  Args:
29
  message (str): The user's input message.
30
  history (list): A list of tuples representing the conversation history (user, assistant).
 
36
  Yields:
37
  str: The AI's response as it is generated.
38
  """
39
+
40
  # Prepare the conversation history for the API call
41
  messages = [{"role": "system", "content": system_message}]
42
+
43
  for user_input, assistant_response in history:
44
+ messages.append({"role": "user", "content": user_input})
45
+ messages.append({"role": "assistant", "content": assistant_response})
 
 
46
 
47
  # Add the latest user message to the conversation
48
  messages.append({"role": "user", "content": message})
49
 
50
  # Initialize an empty response
51
+ streamed_response = ""
52
 
53
  try:
54
  # Generate a response from the model with streaming
55
+ for response in client.chat_completion(
56
  messages=messages,
57
  max_tokens=max_tokens,
58
  stream=True,
59
  temperature=temperature,
60
  top_p=top_p,
61
  ):
62
+ chunk = response.choices[0].delta.get("content", "")
63
+ streamed_response += chunk
64
+ yield streamed_response
65
 
66
  except Exception as e:
67
+ yield f"**Error:** {str(e)}"
68
 
69
 
70
+ # Define the ChatInterface with additional input components
71
  demo = gr.ChatInterface(
72
  fn=respond,
73
  additional_inputs=[
74
+ gr.Textbox(
75
+ value="You are a friendly and helpful assistant.",
76
+ label="System message",
77
+ lines=2
78
+ ),
79
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
80
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
81
  gr.Slider(
82
  minimum=0.1,
83
  maximum=1.0,
 
86
  label="Top-p (nucleus sampling)",
87
  ),
88
  ],
89
+ title="Zephyr 7B Beta Chatbot",
90
+ description="A customizable chatbot interface using Hugging Face's Zephyr 7B Beta model and Inference API.",
91
+ )
92
+
93
+ # Add the "Show Updates" button and output area
94
+ with gr.Row():
95
+ updates_button = gr.Button("Show Latest Updates")
96
+ updates_output = gr.Markdown(visible=False) # Initially hidden
97
+
98
+ # Define the button's click event
99
+ updates_button.click(
100
+ fn=lambda: latest_updates,
101
+ outputs=updates_output,
102
+ show_progress=False
103
  )
104
 
105
  # Launch the Gradio interface
106
  if __name__ == "__main__":
107
+ demo.launch()