patocolher commited on
Commit
753eb99
·
verified ·
1 Parent(s): 4314fd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -12
app.py CHANGED
@@ -27,17 +27,48 @@ 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
  """
@@ -46,7 +77,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
@@ -61,4 +92,4 @@ demo = gr.ChatInterface(
61
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
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
  """
 
77
  demo = gr.ChatInterface(
78
  respond,
79
  additional_inputs=[
80
+ gr.Textbox(value="You are a friendly Chatbot. Your name is Juninho.", label="System message"),
81
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
82
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
83
  gr.Slider(
 
92
 
93
 
94
  if __name__ == "__main__":
95
+ demo.launch()