Erikaww commited on
Commit
4e17468
·
verified ·
1 Parent(s): a6f1f5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -31
app.py CHANGED
@@ -5,8 +5,6 @@ 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("swiss-ai/Apertus-8B-Instruct-2509")
8
- #client = InferenceClient("swiss-ai/Apertus-8B-Instruct-2509")
9
-
10
 
11
  def respond(
12
  message,
@@ -28,53 +26,39 @@ 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
77
- """
78
  demo = gr.ChatInterface(
79
  respond,
80
  additional_inputs=[
@@ -91,6 +75,5 @@ demo = gr.ChatInterface(
91
  ],
92
  )
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("swiss-ai/Apertus-8B-Instruct-2509")
 
 
8
 
9
  def respond(
10
  message,
 
26
 
27
  response = ""
28
 
 
29
  try:
30
+ for chunk in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
33
  stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
  ):
 
 
 
 
37
  try:
38
+ # Primeiro tenta pegar via delta (alguns modelos usam)
39
+ if hasattr(chunk.choices[0], "delta") and chunk.choices[0].delta and getattr(chunk.choices[0].delta, "content", None):
40
+ content = chunk.choices[0].delta.content
41
+ # Se não tiver, tenta via message.content (outros modelos usam)
42
+ elif hasattr(chunk.choices[0], "message") and chunk.choices[0].message and getattr(chunk.choices[0].message, "content", None):
43
+ content = chunk.choices[0].message.content
44
+ else:
45
+ continue # não conteúdo válido, pula
 
 
 
 
 
46
 
47
  response += content
48
  yield response
49
 
50
+ except Exception as e:
51
+ print(f"Erro ao processar chunk: {e}")
52
  continue
53
 
54
  except Exception as e:
55
+ yield f"Erro inesperado: {e}"
 
56
 
 
57
  if response.strip() == "":
58
+ yield "⚠️ O modelo não retornou resposta. Tente ajustar max_tokens, temperature ou escolha outro modelo."
59
 
60
 
61
+ # Interface do Gradio
 
 
62
  demo = gr.ChatInterface(
63
  respond,
64
  additional_inputs=[
 
75
  ],
76
  )
77
 
 
78
  if __name__ == "__main__":
79
  demo.launch()