n3ffos commited on
Commit
9676026
·
verified ·
1 Parent(s): 21fdbd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -43
app.py CHANGED
@@ -5,8 +5,9 @@ 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
- client = InferenceClient("meta-llama/Llama-3.3-70B-Instruct")
9
-
 
10
 
11
 
12
  def respond(
@@ -29,47 +30,16 @@ def respond(
29
 
30
  response = ""
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
  """
@@ -78,7 +48,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
78
  demo = gr.ChatInterface(
79
  respond,
80
  additional_inputs=[
81
- gr.Textbox(value="You are a friendly Chatbot.", 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(
@@ -93,4 +63,4 @@ demo = gr.ChatInterface(
93
 
94
 
95
  if __name__ == "__main__":
96
- demo.launch()
 
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("meta-llama/Llama-3.2-1B-Instruct")
9
+ #client = InferenceClient("microsoft/Phi-3.5-mini-instruct")
10
+ client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
11
 
12
 
13
  def respond(
 
30
 
31
  response = ""
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ mensagens = client.chat_completion(
35
+ messages,
36
+ max_tokens=max_tokens,
37
+ temperature=temperature,
38
+ top_p=top_p,
39
+ )
40
+ response = mensagens.choices[0].message.content
41
 
42
+ return response
 
 
43
 
44
 
45
  """
 
48
  demo = gr.ChatInterface(
49
  respond,
50
  additional_inputs=[
51
+ gr.Textbox(value="You are a friendly Chatbot. Your name is Juninho.", label="System message"),
52
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
53
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
54
  gr.Slider(
 
63
 
64
 
65
  if __name__ == "__main__":
66
+ demo.launch()