Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files59 unmodified lines6061626364656667686970717273747576777879808182838485868788899091929394959697989910010110259 unmodified lines# Generate the prompt for the modelprompt = message['text']# Use a streamer to generate the output in a streaming fashionstreamer = []# Define a function to call chat_llava in a separate threaddef generate_output():output = chat_llava(args=args,image_file=image_path,text=prompt,tokenizer=tokenizer,model=llava_model,image_processor=image_processor,context_len=context_len)for new_text in output:streamer.append(new_text)# Start the generation in a separate threadthread = Thread(target=generate_output)thread.start()# Stream the outputbuffer = ""while thread.is_alive() or streamer:while streamer:new_text = streamer.pop(0)buffer += new_textyield buffertime.sleep(0.1)# Ensure any remaining text is yielded after the thread completeswhile streamer:new_text = streamer.pop(0)buffer += new_textyield bufferchatbot = gr.Chatbot(scale=1)chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)59 unmodified lines6061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810959 unmodified lines# Generate the prompt for the modelprompt = message['text']streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)# Set up the generation arguments, including the streamergeneration_kwargs = dict(args=args,image_file=image_path,text=prompt,tokenizer=tokenizer,model=llava_model,streamer=streamerimage_processor=image_processor, # todo: input model name or pathcontext_len=context_len)# Define the function to call `chat_llava` with the given argumentsdef generate_output(generation_kwargs):chat_llava(**generation_kwargs)# Start the generation in a separate threadthread = Thread(target=generate_output, kwargs=generation_kwargs)thread.start()# Initialize a buffer to accumulate the generated textbuffer = ""# Allow the generation to starttime.sleep(0.5)# Iterate over the streamer to handle the incoming text in chunksfor new_text in streamer:# Look for the end of text token and remove itif "<|eot_id|>" in new_text:new_text = new_text.split("<|eot_id|>")[0]# Add the new text to the bufferbuffer += new_text# Remove the prompt from the generated text (if necessary)generated_text_without_prompt = buffer[len(prompt):]# Simulate processing time (optional)time.sleep(0.06)# Yield the current generated text for further processing or displayyield generated_text_without_promptchatbot = gr.Chatbot(scale=1)chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
app.py+36-29