Tigasturned commited on
Commit
e1b0f5b
1 Parent(s): 2613062

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -5,6 +5,7 @@ from huggingface_hub import InferenceClient
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(
@@ -27,6 +28,7 @@ def respond(
27
 
28
  response = ""
29
 
 
30
  try:
31
  for message in client.chat_completion(
32
  messages,
@@ -39,6 +41,36 @@ def respond(
39
  if not message or not isinstance(message, dict):
40
  continue
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -46,7 +78,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
 
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
+ #client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
9
 
10
 
11
  def respond(
 
28
 
29
  response = ""
30
 
31
+
32
  try:
33
  for message in client.chat_completion(
34
  messages,
 
41
  if not message or not isinstance(message, dict):
42
  continue
43
 
44
+ try:
45
+ # Extract content and finish reason
46
+ content = message.choices[0].delta.content
47
+ finish_reason = message.choices[0].finish_reason
48
+
49
+ # Check if the content is empty
50
+ if content.strip() == "":
51
+ # If the finish reason is 'stop', it's expected and we can break the loop
52
+ if finish_reason == "stop":
53
+ print("Stream ended normally.")
54
+ break
55
+ else:
56
+ print("Received unexpected empty content, skipping...")
57
+ continue
58
+
59
+ response += content
60
+ yield response
61
+
62
+ except (AttributeError, IndexError, KeyError) as e:
63
+ print(f"Error processing message: {e}")
64
+ continue
65
+
66
+ except Exception as e:
67
+ print(f"Unexpected error: {e}")
68
+ yield "An error occurred while generating the response."
69
+
70
+ # Final check if the response is empty
71
+ if response.strip() == "":
72
+ yield "No response generated. Please try again or adjust the settings."
73
+
74
 
75
  """
76
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
78
  demo = gr.ChatInterface(
79
  respond,
80
  additional_inputs=[
81
+ gr.Textbox(value="You are a friendly Chatbot. Your name is Juninho.", label="System message"),
82
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
83
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
84
  gr.Slider(