dx2102 commited on
Commit
56eca40
·
verified ·
1 Parent(s): 040724b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -118,14 +118,14 @@ with gr.Blocks() as demo:
118
  prefix = history[-1]["content"]
119
  history.append({"role": "assistant", "content": ""})
120
  history[-1]["content"] += "Generating with the given prefix...\n"
121
- queue = Queue()
122
  class MyStreamer:
123
  def put(self, tokens):
124
  for token in tokens.flatten():
125
  text = pipe.tokenizer.decode(token.item())
126
  if text == '<|begin_of_text|>':
127
  continue
128
- queue.put(text)
129
  def end(self):
130
  queue.put(None)
131
  def background_fn():
@@ -140,7 +140,11 @@ with gr.Blocks() as demo:
140
  print()
141
  Thread(target=background_fn).start()
142
  while True:
143
- text = queue.get()
 
 
 
 
144
  if text is None:
145
  break
146
  history[-1]["content"] += text
 
118
  prefix = history[-1]["content"]
119
  history.append({"role": "assistant", "content": ""})
120
  history[-1]["content"] += "Generating with the given prefix...\n"
121
+ queue = Queue(maxsize=10)
122
  class MyStreamer:
123
  def put(self, tokens):
124
  for token in tokens.flatten():
125
  text = pipe.tokenizer.decode(token.item())
126
  if text == '<|begin_of_text|>':
127
  continue
128
+ queue.put(text, block=True, timeout=5)
129
  def end(self):
130
  queue.put(None)
131
  def background_fn():
 
140
  print()
141
  Thread(target=background_fn).start()
142
  while True:
143
+ try:
144
+ text = queue.get()
145
+ except queue.Full:
146
+ print("Queue is full. Exiting.")
147
+ break
148
  if text is None:
149
  break
150
  history[-1]["content"] += text