antoniosp commited on
Commit
8666908
1 Parent(s): a2bdefa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -12
app.py CHANGED
@@ -4,7 +4,8 @@ from huggingface_hub import InferenceClient
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(
@@ -27,17 +28,49 @@ def respond(
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
@@ -45,7 +78,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
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"),
50
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
  gr.Slider(
 
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
+ 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,
35
+ max_tokens=max_tokens,
36
+ stream=True,
37
+ temperature=temperature,
38
+ top_p=top_p,
39
+ ):
40
+ # Ensure the message has a valid structure
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(