inteligenciamilgrau commited on
Commit
3a197f4
·
verified ·
1 Parent(s): 6598be9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -27,12 +27,49 @@ def respond(
27
 
28
  response = ""
29
 
30
- mensagem = client.chat_completion(
31
- messages
32
- )
33
- response = mensagem.choices[0].message.content
34
 
35
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  """
38
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
27
 
28
  response = ""
29
 
 
 
 
 
30
 
31
+ try:
32
+ for message in client.chat_completion(
33
+ messages,
34
+ max_tokens=max_tokens,
35
+ stream=True,
36
+ temperature=temperature,
37
+ top_p=top_p,
38
+ ):
39
+ # Ensure the message has a valid structure
40
+ if not message or not isinstance(message, dict):
41
+ continue
42
+
43
+ try:
44
+ # Extract content and finish reason
45
+ content = message.choices[0].delta.content
46
+ finish_reason = message.choices[0].finish_reason
47
+
48
+ # Check if the content is empty
49
+ if content.strip() == "":
50
+ # If the finish reason is 'stop', it's expected and we can break the loop
51
+ if finish_reason == "stop":
52
+ print("Stream ended normally.")
53
+ break
54
+ else:
55
+ print("Received unexpected empty content, skipping...")
56
+ continue
57
+
58
+ response += content
59
+ yield response
60
+
61
+ except (AttributeError, IndexError, KeyError) as e:
62
+ print(f"Error processing message: {e}")
63
+ continue
64
+
65
+ except Exception as e:
66
+ print(f"Unexpected error: {e}")
67
+ yield "An error occurred while generating the response."
68
+
69
+ # Final check if the response is empty
70
+ if response.strip() == "":
71
+ yield "No response generated. Please try again or adjust the settings."
72
+
73
 
74
  """
75
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface