placed processor tokenizer
Browse files
app.py
CHANGED
|
@@ -19,107 +19,50 @@ model = LlavaForConditionalGeneration.from_pretrained(
|
|
| 19 |
model_id,
|
| 20 |
torch_dtype=torch.float16,
|
| 21 |
low_cpu_mem_usage=True
|
| 22 |
-
)
|
| 23 |
|
| 24 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
@spaces.GPU(duration=120)
|
| 29 |
-
def krypton(input,
|
| 30 |
-
history):
|
| 31 |
-
"""
|
| 32 |
-
Recieves inputs (prompts with images if they were added),
|
| 33 |
-
the image is formated for pil and prompt is formated for the model,
|
| 34 |
-
to place it's output to the user, these prompts and images are passed in
|
| 35 |
-
the processor and generation of the model, than the output is decoded from the processor,
|
| 36 |
-
onto the UI.
|
| 37 |
-
"""
|
| 38 |
if input["files"]:
|
| 39 |
-
if
|
| 40 |
-
image = input["files"][-1]["path"]
|
| 41 |
-
else:
|
| 42 |
-
image = input["files"][-1]
|
| 43 |
else:
|
| 44 |
-
|
| 45 |
-
# kept inside in tuples, the last one
|
| 46 |
for hist in history:
|
| 47 |
-
if
|
| 48 |
image = hist[0][0]
|
| 49 |
-
try:
|
| 50 |
-
if image is None:
|
| 51 |
-
gr.Error("You need to upload an image please for krypton to work.")
|
| 52 |
-
except NameError:
|
| 53 |
-
# Image is not defined at all
|
| 54 |
-
gr.Error("Uplaod an image for Krypton to work")
|
| 55 |
-
|
| 56 |
-
prompt = ("<|start_header_id|>user<|end_header_id|>\n\n<image>\n{input['text']}<|eot_id|>"
|
| 57 |
-
"<|start_header_id|>assistant<|end_header_id|>\n\n")
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
image = Image.open(image)
|
| 60 |
-
inputs = processor(prompt, image, return_tensors='pt').to(0, torch.float16)
|
| 61 |
-
|
| 62 |
# Streamer
|
| 63 |
-
streamer = TextIteratorStreamer(processor,
|
| 64 |
-
|
| 65 |
# Generation kwargs
|
| 66 |
generation_kwargs = dict(
|
| 67 |
-
inputs=inputs,
|
|
|
|
| 68 |
streamer=streamer,
|
| 69 |
max_new_tokens=1024,
|
| 70 |
do_sample=False
|
| 71 |
)
|
| 72 |
-
|
| 73 |
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 74 |
thread.start()
|
| 75 |
-
|
| 76 |
buffer = ""
|
| 77 |
time.sleep(0.5)
|
| 78 |
for new_text in streamer:
|
| 79 |
-
# find <|eot_id|> and remove it from the new_text
|
| 80 |
-
if "<|eot_id|>" in new_text:
|
| 81 |
-
new_text = new_text.split("<|eot_id|>")[0]
|
| 82 |
buffer += new_text
|
| 83 |
-
|
| 84 |
-
# generated_text_without_prompt = buffer[len(text_prompt):]
|
| 85 |
generated_text_without_prompt = buffer
|
| 86 |
-
# print(generated_text_without_prompt)
|
| 87 |
time.sleep(0.06)
|
| 88 |
-
|
| 89 |
-
yield generated_text_without_prompt
|
| 90 |
-
|
| 91 |
-
chatbot=gr.Chatbot(height=600, label="Krypt AI")
|
| 92 |
-
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter your question or upload an image.", show_label=False)
|
| 93 |
-
with gr.Blocks(fill_height=True) as demo:
|
| 94 |
-
gr.Markdown(DESCRIPTION)
|
| 95 |
-
gr.ChatInterface(
|
| 96 |
-
fn=krypton,
|
| 97 |
-
chatbot=chatbot,
|
| 98 |
-
fill_height=True,
|
| 99 |
-
# additional_inputs_accordion=gr.Accordion(label="βοΈ Parameters", open=False, render=False),
|
| 100 |
-
# additional_inputs=[
|
| 101 |
-
# gr.Slider(minimum=20,
|
| 102 |
-
# maximum=80,
|
| 103 |
-
# step=1,
|
| 104 |
-
# value=50,
|
| 105 |
-
# label="Max New Tokens",
|
| 106 |
-
# render=False),
|
| 107 |
-
# gr.Slider(minimum=0.0,
|
| 108 |
-
# maximum=1.0,
|
| 109 |
-
# step=0.1,
|
| 110 |
-
# value=0.7,
|
| 111 |
-
# label="Temperature",
|
| 112 |
-
# render=False),
|
| 113 |
-
# gr.Slider(minimum=1,
|
| 114 |
-
# maximum=12,
|
| 115 |
-
# step=1,
|
| 116 |
-
# value=5,
|
| 117 |
-
# label="Number of Beams",
|
| 118 |
-
# render=False),
|
| 119 |
-
# ],
|
| 120 |
-
multimodal=True,
|
| 121 |
-
textbox=chat_input,
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
demo.queue(api_open=False)
|
| 125 |
-
demo.launch(show_api=False, share=False)
|
|
|
|
| 19 |
model_id,
|
| 20 |
torch_dtype=torch.float16,
|
| 21 |
low_cpu_mem_usage=True
|
| 22 |
+
)
|
| 23 |
|
| 24 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 25 |
|
| 26 |
+
# Confirming and setting the eos_token_id (if necessary)
|
| 27 |
+
model.generation_config.eos_token_id = processor.tokenizer.eos_token_id
|
| 28 |
|
| 29 |
@spaces.GPU(duration=120)
|
| 30 |
+
def krypton(input, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if input["files"]:
|
| 32 |
+
image = input["files"][-1]["path"] if isinstance(input["files"][-1], dict) else input["files"][-1]
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
+
image = None
|
|
|
|
| 35 |
for hist in history:
|
| 36 |
+
if isinstance(hist[0], tuple):
|
| 37 |
image = hist[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
if not image:
|
| 40 |
+
gr.Error("You need to upload an image for Krypton to work.")
|
| 41 |
+
return
|
| 42 |
+
|
| 43 |
+
prompt = f"user\n\n<image>\n{input['text']}\nassistant\n\n"
|
| 44 |
image = Image.open(image)
|
| 45 |
+
inputs = processor(prompt, images=image, return_tensors='pt').to(0, torch.float16)
|
| 46 |
+
|
| 47 |
# Streamer
|
| 48 |
+
streamer = TextIteratorStreamer(processor.tokenizer, skip_special_tokens=False, skip_prompt=True)
|
| 49 |
+
|
| 50 |
# Generation kwargs
|
| 51 |
generation_kwargs = dict(
|
| 52 |
+
inputs=inputs['input_ids'],
|
| 53 |
+
attention_mask=inputs['attention_mask'],
|
| 54 |
streamer=streamer,
|
| 55 |
max_new_tokens=1024,
|
| 56 |
do_sample=False
|
| 57 |
)
|
| 58 |
+
|
| 59 |
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 60 |
thread.start()
|
| 61 |
+
|
| 62 |
buffer = ""
|
| 63 |
time.sleep(0.5)
|
| 64 |
for new_text in streamer:
|
|
|
|
|
|
|
|
|
|
| 65 |
buffer += new_text
|
|
|
|
|
|
|
| 66 |
generated_text_without_prompt = buffer
|
|
|
|
| 67 |
time.sleep(0.06)
|
| 68 |
+
yield generated_text_without_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|