wellborgmann commited on
Commit
b29204b
·
verified ·
1 Parent(s): 61f28aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -4,6 +4,7 @@ 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("qwen2.5:0.5b")
8
 
9
 
@@ -30,18 +31,21 @@ def respond(
30
 
31
  try:
32
  # Chama a API de completamento com streaming
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
  # Verifica se a resposta contém o conteúdo esperado
41
- if 'choices' not in message or len(message.choices) == 0 or 'delta' not in message.choices[0]:
42
  raise ValueError("Resposta inesperada do modelo.")
43
 
44
- token = message.choices[0].delta.content
45
  response += token # Acumula o conteúdo
46
 
47
  # Retorna a resposta incrementalmente
@@ -62,6 +66,7 @@ def respond(
62
  """
63
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
64
  """
 
65
  demo = gr.ChatInterface(
66
  respond,
67
  additional_inputs=[
 
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
+ # Criando o cliente para interagir com o modelo no Hugging Face.
8
  client = InferenceClient("qwen2.5:0.5b")
9
 
10
 
 
31
 
32
  try:
33
  # Chama a API de completamento com streaming
34
+ # A API do Hugging Face usa o método `client.chat_completion`.
35
+ response_stream = client.chat_completion(
36
+ messages=messages,
37
  max_tokens=max_tokens,
 
38
  temperature=temperature,
39
  top_p=top_p,
40
+ stream=True
41
+ )
42
+
43
+ for message in response_stream:
44
  # Verifica se a resposta contém o conteúdo esperado
45
+ if 'choices' not in message or len(message['choices']) == 0 or 'delta' not in message['choices'][0]:
46
  raise ValueError("Resposta inesperada do modelo.")
47
 
48
+ token = message['choices'][0]['delta']['content']
49
  response += token # Acumula o conteúdo
50
 
51
  # Retorna a resposta incrementalmente
 
66
  """
67
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
68
  """
69
+ # Criando a interface Gradio
70
  demo = gr.ChatInterface(
71
  respond,
72
  additional_inputs=[